Identify colour of each pixel using iterative statement

Hi Everyone,
Absolute begginer here.
I am looking to write an iterative statement (loop function) to identify pixels that are 'red', and change them to 'green' in a new image. Pixels that are not identified as 'red' need to be left alone. I understand this is not the best way to do this, but I need to explore the limitations of this process before moving to more advance image processing tools. Keep in mind that I don't have impixel() function as I don't have the image processing toolbox yet. I have a RGB 512 x 512 image.
My theshold would be that the pixel value is:
red > 180 && green < 50 (I'm not concerned with blue at the moment)
Logically the statement would read as follows:
If pixel red value > 180 and pixel green value < 50, then multiply red value by 0.5 and green value by 2.0.
The part I am having trouble with is actually building this into the code and creating a function that goes through pixel by pixel and creates a new image were the 'red' pixels are changed to 'green'
Thank you all in advance.
What I have so far is below:
% % % Code to import image and modify colour
% File name to call
filename = 'pepper.bmp';
% Function to call filename
OriginalImage = imread(filename);
% image(OriginalImage) % Displays Original Image (Discovered 'image'
% fucntion does not display as clear of a picture as 'imshow'
% Extract individual RGB colour channels
redChannel = OriginalImage(:, :, 1);
greenChannel = OriginalImage(:, :, 2);
blueChannel = OriginalImage(:, :, 3);
% Construct new image by analysing pixel by pixel
for
????
end
% Display both images side by side
subplot(1,2,1), imshow(OriginalImage)
title('Original Image');
subplot(1,2,2), imshow(ModifiedImage)
title('Modfied Image');

 Akzeptierte Antwort

Ameer Hamza
Ameer Hamza am 19 Mär. 2020
Bearbeitet: Ameer Hamza am 19 Mär. 2020
In MATLAB, you don't need to write for loops to do pixel manipulation. Following code shows one of the way
im = imread('peacock.jpg');
red = im(:,:,1);
green = im(:,:,2);
blue = im(:,:,3);
mask = (red > 180) & (green < 50);
red(mask) = red(mask)*0.5;
green(mask) = green(mask)*2;
im_new(:,:,1) = red;
im_new(:,:,2) = green;
im_new(:,:,3) = blue;

3 Kommentare

>> 2 > 1 * 3
ans =
0
Image Analyst
Image Analyst am 19 Mär. 2020
Bearbeitet: Image Analyst am 19 Mär. 2020
You can stay using logicals and must cast back to the original class (assuming uint8):
mask = (red > 180) & (green < 50);
red(mask) = uint8(red(mask)*0.5);
green(mask) = uint8(green(mask)*2);
Thanks for pointing out. I was actually trying different things and accidently pasted the above version instead of and (&) version.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Kyle Robertson
Kyle Robertson am 11 Apr. 2020
Thank you so so much to everyone that contributed! This worked great! The code that I ended up with is below:
I do know that iterative statements were not nessesary but that was part of the excersie.
Best Wishes!!
% % % Code to import image and modify colour
% File name to call
filename = 'pepper.bmp';
OriginalImage = imread(filename); %stored as a 512x512x3 matrix array % 3 dimensional array
ModifiedImage = OriginalImage;
%Condition Statement for which pixels to process
for i = 1:512 %iterate over each row
for j = 1:512 %iterate over each column
if OriginalImage(i,j,1)>OriginalImage(i,j,2)*1.15
ModifiedImage(i,j,1) = (OriginalImage(i,j,2)*0.90)+60; %Swap Red and Green Channels and apply linear scaling
ModifiedImage(i,j,2) = (OriginalImage(i,j,1)*1.08)-50;
ModifiedImage(i,j,3) = (OriginalImage(i,j,3)*0.90)+50; %Scale the Blue Channel for more accurate appearance
%Scale Brightness of processed area. Also useful for debugging selection area by setting to 0
ModifiedImage(i,j,:) = (ModifiedImage(i,j,:)*1.2)-30;
end
end
end
% Display both images side by side
figure('Name','Peppers','WindowState','Maximized')
subplot(1,2,1), imshow(OriginalImage)
title('Original Image');
subplot(1,2,2), imshow(ModifiedImage)
title('Modfied Image');

Community Treasure Hunt

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

Start Hunting!

Translated by