Hi, I tried to divide my file into 2 parallel file: a main file and a function file. Can somebody help me with this?

1 visualización (últimos 30 días)
type('Lab2Part1.m')
a = imread('coloredChips.png'); figure(1), imshow(a); % Creat menu of color disp('Color selection') disp('r for red') disp('b for blue') disp('y for yellow') disp('m for magenta') disp('g for green') disp('c for cyan') disp('o for orange') % Now we want input from user to tell us which color to change x = input(' Please select the color you want to change: ','s'); copy = a; dimension = size(a); row_n = dimension (1); col_n = dimension (2); switch x case 'r' for row = 1:row_n for col = 1:col_n pixel_red = a(row,col,1); pixel_green = a(row,col,2); pixel_blue = a(row,col,3); if (pixel_red >= 200) && (pixel_green <= 60) && (pixel_green >= 5) && (pixel_blue <= 99) && (pixel_blue >=15) copy(row,col,1) = 0; copy(row,col,2) = 0; copy(row,col,3) = 0; end end end case 'b' for row = 1:row_n for col = 1:col_n pixel_red = a(row,col,1); pixel_green = a(row,col,2); pixel_blue = a(row,col,3); if (pixel_red >= 2) && (pixel_green <= 99) && (pixel_green >= 71) && (pixel_blue <= 99) && (pixel_blue >=233) && (pixel_red <= 127) && (pixel_blue <= 254) copy(row,col,1) = 0; copy(row,col,2) = 0; copy(row,col,3) = 0; end end end case 'y' for row = 1:row_n for col = 1:col_n pixel_red = a(row,col,1); pixel_green = a(row,col,2); pixel_blue = a(row,col,3); if (pixel_red >= 170) && (pixel_red <= 255) && (pixel_green >= 170) && (pixel_green <= 255) && (pixel_blue <= 90) copy(row,col,:) = 0; end end end case 'o' for row = 1:row_n for col = 1:col_n pixel_red = a(row,col,1); pixel_green = a(row,col,2); pixel_blue = a(row,col,3); if (pixel_red >= 200) && (pixel_red <= 255) && (pixel_green >= 65) && (pixel_green <= 100) && (pixel_blue >= 0) && (pixel_blue <= 100) copy(row,col,:) = 0; end end end case 'g' for row = 1:row_n for col = 1:col_n pixel_red = a(row,col,1); pixel_green = a(row,col,2); pixel_blue = a(row,col,3); if (pixel_green >= 90) && (pixel_red <= 55) && (pixel_blue >= 55) && (pixel_blue <= 180) copy(row,col,:) = 0; end end end case 'm' for row = 1:row_n for col = 1:col_n pixel_red = a(row,col,1); pixel_green = a(row,col,2); pixel_blue = a(row,col,3); if (pixel_red <= 228) && (pixel_red >= 105) && (pixel_green <= 70 ) && (pixel_blue <= 228) && (pixel_blue >= 60) copy(row,col,1) = 0; copy(row,col,2) = 0; copy(row,col,3) = 0; end end end case 'c' for row = 1:row_n for col = 1:col_n pixel_red = a(row,col,1); pixel_green = a(row,col,2); pixel_blue = a(row,col,3); if (pixel_blue >= 140) && (pixel_blue <=220) && (pixel_red <= 70) && (pixel_red >= 8) && (pixel_green <= 230) && (pixel_green >=185 ) copy(row,col,1) = 0; copy(row,col,2) = 0; copy(row,col,3) = 0; end end end case otherwise disp('Incorrect selection, please choose from the menu'); end % display the modified image figure(2),imshow(copy);
  1 comentario
Dyuman Joshi
Dyuman Joshi el 4 de Abr. de 2024
You should specify what exactly you need help with and provide all relevant information.
And there are no functions in the code above.

Iniciar sesión para comentar.

Respuestas (1)

Gayatri
Gayatri el 4 de Abr. de 2024
Hi Phat,
To divide your script into two parts : a main file and a function file, create a main script that handles user input and image display, and a separate function that processes the image based on the selected color.
  • Main File: colorChangeMain.m. This file will handle user interaction and display the original and modified images.
% Load the image
a = imread('coloredChips.png');
figure(1), imshow(a);
% Display color selection menu
disp('Color selection')
disp('r for red')
disp('b for blue')
disp('y for yellow')
disp('m for magenta')
disp('g for green')
disp('c for cyan')
disp('o for orange')
% Get user input for color selection
x = input('Please select the color you want to change: ', 's');
% Call the function to process the image
copy = processColorChange(a, x);
% Display the modified image
figure(2), imshow(copy);
  • Function File: processColorChange.m. This function will take the original image and the user-selected color as input, process the image and return the modified image.
function copy = processColorChange(a, x)
% Copy the original image
copy = a;
% Get the dimensions of the image
dimension = size(a);
row_n = dimension(1);
col_n = dimension(2);
% Process the image based on the selected color
switch x
case 'r'
% Insert the case 'r' code block here
case 'b'
% Insert the case 'b' code block here
case 'y'
% Insert the case 'y' code block here
case 'o'
% Insert the case 'o' code block here
case 'g'
% Insert the case 'g' code block here
case 'm'
% Insert the case 'm' code block here
case 'c'
% Insert the case 'c' code block here
otherwise
disp('Incorrect selection, please choose from the menu');
end
end
For each color case ('r', 'b', 'y', 'o', 'g', 'm', 'c'), you should insert the respective code block from your original script into the function processColorChange within the appropriate case section.
I hope it helps!

Categorías

Más información sobre Computer Vision with Simulink en Help Center y File Exchange.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by