Identify colour of each pixel using iterative statement
15 views (last 30 days)
Show older comments
Kyle Robertson
on 19 Mar 2020
Answered: Kyle Robertson
on 11 Apr 2020
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');
0 Comments
Accepted Answer
Ameer Hamza
on 19 Mar 2020
Edited: Ameer Hamza
on 19 Mar 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 Comments
Image Analyst
on 19 Mar 2020
Edited: Image Analyst
on 19 Mar 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);
Ameer Hamza
on 19 Mar 2020
Thanks for pointing out. I was actually trying different things and accidently pasted the above version instead of and (&) version.
More Answers (1)
See Also
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!