give colour to a particular pixel of an image

hy there,,, i need help to solve my problem about give colour to a particular pixel of an image,,,
i've done thresholding at my image,, after that i want to find the coordinates anywhere that is worth 1 on taht image. Having obtained the coordinates, original RGB image was split into three matrix of matrix R, G and B is the matrix components RGB image forming. After that step, The next is to give a value of 255 on that coordinates for the matrix R, the value 0 the coordinates skin on the matrix G and value 0 on the coordinates on the matrix B, so that in the original image (RGB) area that i want to colouring will be red.
how can i do that??? need help please,, is there anybody have code for this problem?? thanks. Gbu

 Respuesta aceptada

Walter Roberson
Walter Roberson el 15 de Ag. de 2011
If you have already split your original matrix in to R, G, and B, then if ImgT is the threshholded binary image, you would simply use
ColoredImage = R .* ImgT;
ColoredImage(1,1,3) = 0;

7 comentarios

UNHAS HASANUDDIN
UNHAS HASANUDDIN el 15 de Ag. de 2011
I had to classify the images before and I want to continue to give red color to the coordinat I meant, was that the coordinates of value 1 but on the original image (RGB image), how can i do that??? here is the code for classification
% function classification(varargin,luaspiksel)
X = readimage(varargin);
clear all;
close all;
clc;
P = imread('1color.jpg');
X = imresize(P,[600 600]);
% imshow(X),title('Citra Asli');
Y = X;
[x1,x2,x3] = size(X);
for k=1:x3
cacah1=0;
cacah2=0;
for i=1:x1
for j=1:x2
if((X(i,j,1)>=46)&(X(i,j,1)<=136)&(X(i,j,2)>=48)&(X(i,j,2)
<=174)&(X(i,j,3)>=35)&(X(i,j,3)<=89))
X(i,j,1)=0;
X(i,j,2)=105;
X(i,j,3)=62;
M(i,j,k)=X(i,j,k);
cacah1 = cacah1+1;
else %end
X(i,j,1)=245;
X(i,j,2)=201;
X(i,j,3)=211;
M(i,j,k)=X(i,j,k);
cacah2 = cacah2+1;
end
end
end
end
I = rgb2gray(M);
threshold = graythresh(I);
BW = im2bw(I,threshold);
% imshow
figure
imshow (BW), title('BW Image')
figure
imshow(M),title('Classification Image')
figure
imshow(I),title('Classification Image Gray')
figure,
imshow(Y),title('Original Image')
UNHAS HASANUDDIN
UNHAS HASANUDDIN el 15 de Ag. de 2011
Mr. walter help me please,, the code u give doesn't work,,
Walter Roberson
Walter Roberson el 15 de Ag. de 2011
It will take me some time to read your code and try to understand the difference between what you are asking and what I had previously understood. It will likely be several hours before I have time to work on this.
UNHAS HASANUDDIN
UNHAS HASANUDDIN el 18 de Ag. de 2011
I mean like the example below, after doing the classification process as shown above, the after classification the image into two color then doing threshold on it, and then we make the white area become red in the original image,,, like the example below ,, the white area is returned to the original picture and make it red . Can I do it on RGB images? Can you help me do that?
I found the code below on matlab newsreader,, can I do it to RGB image?, if it possible to do,, can you help me how to fill contour with specified color on an RGB image??? need help,,,urgent,,,
here is the code:
fontSize = 15;
% Read in a standard MATLAB gray scale demo image.
folder = fullfile(matlabroot, '\toolbox\images\imdemos');
baseFileName = 'cameraman.tif';
fullFileName = fullfile(folder, baseFileName);
% 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
grayImage = imread(fullFileName);
% Get the dimensions of the image. numberOfColorBands should be = 1.
[rows columns numberOfColorBands] = size(grayImage);
% Display the original gray scale image.
subplot(2, 2, 1);
imshow(grayImage, []);
title('Original Grayscale Image', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'Position', get(0,'Screensize'));
set(gcf,'name','Demo by ImageAnalyst','numbertitle','off')
% Let's compute and display the histogram.
[pixelCount grayLevels] = imhist(grayImage);
subplot(2, 2, 2);
bar(pixelCount);
title('Histogram of original image', 'FontSize', fontSize);
xlim([0 grayLevels(end)]); % Scale x axis manually.
% Get a thresholded, binary image.
binaryImage = grayImage < 80;
% Display the binary image.
subplot(2, 2, 3);
imshow(binaryImage, []);
title('Binary "Mask" Image', 'FontSize', fontSize);
% Average it with the image and put into the red channel
averageImage = (256 * single(binaryImage) + single(grayImage))/2;
redChannel = grayImage; % Initialize to same as gray.
% Make it reddish just where the mask is.
redChannel(binaryImage) = averageImage(binaryImage);
% Create the RGB image.
coloredImage = cat(3, redChannel, grayImage, grayImage);
% Display the binary image.
subplot(2, 2, 4);
imshow(coloredImage, []);
title('Colored Image', 'FontSize', fontSize);
UNHAS HASANUDDIN
UNHAS HASANUDDIN el 18 de Ag. de 2011
Mr. Walter,, Please help me,,,
Walter Roberson
Walter Roberson el 18 de Ag. de 2011
Sorry, I'm still having trouble understanding the question, so this will have to wait until I have some spare time. (If someone else understands the back-and-forth of the question, feel free to jump in with a solution!)
Note: merely saying that something is "urgent" doesn't work. If it really is Urgent, have one of your ambassadors talk to the Canadian Ambassador to your country and make a presentation as to why it is in the interest of both of our countries that I be temporary taken off of my regular work to solve your technical difficulty. The Canadian Ambassadors are trained in how to contact me.
Image Analyst
Image Analyst el 18 de Ag. de 2011
He posted my code that I gave him or someone here or in the newsgroup. I ended by saying that the extension to RGB images is straightforward - just extract the colors and do the same thing this code does: mask the arrays and recombine. Apparently it's far more challenging than I would have thought (since this is his third original Answers post on the same topic). So I guess it's up to me to make the adaptation. Look in an upcoming answer (rather than this comment).

Iniciar sesión para comentar.

Más respuestas (1)

Image Analyst
Image Analyst el 18 de Ag. de 2011
OK, here's your new code. I adapted it from my code in your comment above. Copy, paste, run, then stand back and admire. :-) It looks longer and harder than it really is - just take it one commented step at a time. ImageAnalyst
% Demo to turn white parts of an image red.
% By ImageAnalyst
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
imtool close all; % Close all imtool figures.
clear; % Erase all existing variables.
workspace; % Make sure the workspace panel is showing.
fontSize = 15;
% Check that user has the Image Processing Toolbox installed.
hasIPT = license('test', 'image_toolbox');
if ~hasIPT
% User does not have the toolbox installed.
message = sprintf('Sorry, but you do not seem to have the Image Processing Toolbox.\nDo you want to try to continue anyway?');
reply = questdlg(message, 'Toolbox missing', 'Yes', 'No', 'Yes');
if strcmpi(reply, 'No')
% User said No, so exit.
return;
end
end
% Read in a standard MATLAB gray scale demo image.
folder = fullfile(matlabroot, '\toolbox\images\imdemos');
baseFileName = 'peppers.png';
fullFileName = fullfile(folder, baseFileName);
% 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 = 1.
[rows columns numberOfColorBands] = size(rgbImage);
% Display the original color image.
subplot(3, 4, 1);
imshow(rgbImage, []);
title('Original Color Image', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'Position', get(0,'Screensize'));
set(gcf,'name','Demo by ImageAnalyst','numbertitle','off')
% Extract the individual red, green, and blue color channels.
redChannel = rgbImage(:, :, 1);
greenChannel = rgbImage(:, :, 2);
blueChannel = rgbImage(:, :, 3);
% Display the individual color channels
subplot(3, 4, 2);
imshow(redChannel);
title('Red Channel', 'FontSize', fontSize);
subplot(3, 4, 3);
imshow(greenChannel);
title('Green Channel', 'FontSize', fontSize);
subplot(3, 4, 4);
imshow(blueChannel);
title('Blue Channel', 'FontSize', fontSize);
% Create a binary mask.
mask = redChannel > 160 & greenChannel > 150 & blueChannel > 100;
subplot(3, 4, 5);
imshow(mask);
title('Mask', 'FontSize', fontSize);
% Apply masks to the channels. Make the mask pixels red.
maskedRed = redChannel;
maskedGreen = greenChannel;
maskedBlue = blueChannel;
maskedRed(mask) = 255;
maskedGreen(mask) = 0;
maskedBlue(mask) = 0;
% Display the individual color channels
subplot(3, 4, 6);
imshow(maskedRed);
title('Masked Red Channel', 'FontSize', fontSize);
subplot(3, 4, 7);
imshow(maskedGreen);
title('Masked Green Channel', 'FontSize', fontSize);
subplot(3, 4, 8);
imshow(maskedBlue);
title('Masked Blue Channel', 'FontSize', fontSize);
% Combine the new color channels into our new RGB image.
rgbImage = cat(3, redChannel, greenChannel, blueChannel);
% Create the RGB image.
coloredImage = cat(3, maskedRed, maskedGreen, maskedBlue);
% Display the new, colored image.
subplot(3,4,10);
imshow(coloredImage, []);
title('New Colored Image', 'FontSize', fontSize);

5 comentarios

Walter Roberson
Walter Roberson el 18 de Ag. de 2011
What is the purpose of ripping apart the channels of rgbImage and then near the end putting them back together again (unmodified) back in to rgbImage and not even displaying that?
You have called your threshholded color image "mask", whereas I called it ImgT in mine. It appears to me that the difference between your code and my code is just that my code uses the _original_ red color value at the positions remaining, whereas yours uses 255 at those positions. Using the original red rather than the constant 255 would, it seems to me, be more consistent with the stated requirement that "I want to continue to give red color to the coordinat I meant, was that the coordinates of value 1 but on the original image (RGB image)"
Anyhow, it seems to me that if 255 is to be given for the red channels, and "mask" is already calculated, then the code can be trimmed down to
ColoredImage = 255*typecast(mask,class(rgbImage));
ColoredImage(1,1,3) = 0;
But what do I know? I'm just told that the code I gave doesn't work, which I am expected to decode without any description of the difference between the image my code produces and the desired image.
Image Analyst
Image Analyst el 19 de Ag. de 2011
You're right Walter - that was left over boilerplate code that I thought I had ripped out. He can ignore the line that says rgbImage = cat(3,.....
And what I called mask is the same as your imgT, as you say.
I had earlier given code to "tint" the mask area red but I'm not sure from his description if he wanted a red tint, or if he wanted it solid red. The English is not standard and somewhat ambiguous and hard to decypher. So now he has examples both ways and can pick - tinted or solid.
Your first line of code gives an error:
"??? Error using ==> typecast
The first input argument must be a full, non-complex numeric value." I tried it with cast() and it worked though.
I'm not sure what your second line of code does other than to set the upper left pixel's blue component to 0. Your code (with cast()) gives the binary mask as all red and everything else black, whereas my code give the mask as all red and everything else (not masked) as the original colors. Undoubtedly my code could be trimmed down and made more compact, but I just do extra stuff like showing the individual color channels, giving titles to the various images, etc. just for tutorial purposes. Of course it's not necessary to do all that stuff if they're not wanted.
We waste a lot of time trying to figure out what the poster really means.
Walter Roberson
Walter Roberson el 19 de Ag. de 2011
Ah yes, it should have been cast() instead of typecast()
Setting ColoredImage(1,1,3) = 0 extends the array in to the third dimension everywhere, with all of the unmentioned values set to 0.
Red only at the masked points... that would make sense. I got confused by the wording of the question.
UNHAS HASANUDDIN
UNHAS HASANUDDIN el 6 de Sept. de 2011
i want to say a lot of thank you for Mr.ImageAnalyst and Mr.Walter Roberson. Now it works and i've got what i want with little modification with that code above. thank you so much.
now i have another problem,,
I have some satellite image that I downloaded from USGS. I want to doing cropping to the part of that image that unnecessary and take just the areas that I need. I want to ask how to cut the unnecessary part of that image and leaving only the parts that I need? and how to keep cropping I did, have same result for every image? because I need the same cropping area for each image.
can you help me??
thanks for all
God bless you
Walter Roberson
Walter Roberson el 6 de Sept. de 2011
Please do not post unrelated questions in an existing topic; most people will not notice them.
You posted your cropping question as a new topic already, and I answered there.

Iniciar sesión para comentar.

Categorías

Más información sobre Convert Image Type en Centro de ayuda y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by