How can I change color of particular pixel selected on specific criteria in color image ?

26 views (last 30 days)
I want to change color of pixel to red in color image. The pixel is selected on the basis of specific criteria. The input image is not a simple color image, it is processed image in which mostly pixel have value[255, 255, 255]. I am using following code.............. The code is working but it change color of every pixel to red instead to specific one. thanks ! I = imread('seedpixelofwatershedriver.jpg'); >> [m, n, k]=size(I); >> for i=1:m for j=1:n r=I(i, j, 1); g=I(i, j, 2); b=I(i, j, 3); avg=0; avg=(r+g+b)/3; if(avg<250) I(i,j,1)=255; I(i,j,2)=0; I(i,j,3)=0; % change value to red color end end end >> figure, imshow(I), title('seddedImage')

Accepted Answer

Image Analyst
Image Analyst on 14 Apr 2013
See this demo:
clc; % Clear the command window.
workspace; % Make sure the workspace panel is showing.
clearvars;
format longg;
format compact;
fontSize = 20;
% Read in a standard MATLAB color demo image.
folder = fullfile(matlabroot, '\toolbox\images\imdemos');
baseFileName = 'peppers.png';
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
if ~exist(fullFileName, 'file')
% Didn't find it there. Check the search path for it.
fullFileName = baseFileName; % No path this time.
if ~exist(fullFileName, 'file')
% Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
rgbImage = imread(fullFileName);
% Get the dimensions of the image. numberOfColorBands should be = 3.
[rows, columns, numberOfColorBands] = size(rgbImage);
% Display the original color image.
subplot(2, 2, 1);
imshow(rgbImage);
title('Original Color Image', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
% Find where the average is more than 250.
grayImage = rgb2gray(rgbImage);
% Display the image.
subplot(2, 2, 2);
imshow(grayImage);
title('Gray Image', 'FontSize', fontSize);
% Binarize (threshold) the image to find
% where the average is brighter than some threshold value.
thresholdValue = 200; % Change to whatever value you want.
binaryImage = grayImage > thresholdValue;
% Display the image.
subplot(2, 2, 3);
imshow(binaryImage);
caption = sprintf('Binary Image = gray image > %d', thresholdValue);
title(caption, 'FontSize', fontSize);
% Extract the individual red, green, and blue color channels.
redChannel = rgbImage(:, :, 1);
greenChannel = rgbImage(:, :, 2);
blueChannel = rgbImage(:, :, 3);
% Make binary pixels red.
redChannel(binaryImage) = 255;
greenChannel(binaryImage) = 0;
blueChannel(binaryImage) = 0;
% Get RGB image again.
newRGB = cat(3, redChannel, greenChannel, blueChannel);
% Display the image.
subplot(2, 2, 4);
imshow(newRGB);
title('New RGB Image', 'FontSize', fontSize);
  2 Comments
Rajesh  Gothwal
Rajesh Gothwal on 15 Apr 2013
thanks ! sorry i did not mention at above but i want these red pixel into my original color image from which i obtained this image. means that i want to select pixels in the processed image then those pixel in red color in original color image. how can i do ?
Image Analyst
Image Analyst on 15 Apr 2013
Well I saw in your code how you were averaging the red, green, and blue channels together to form the gray scale version so I followed your example and did the same thing.
If you want to find red in the original color image, see my color segmentation demos in my File Exchange http://www.mathworks.com/matlabcentral/fileexchange/?term=authorid%3A31862

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!