give colour to a particular pixel of an image
Mostrar comentarios más antiguos
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
1 comentario
UNHAS HASANUDDIN
el 15 de Ag. de 2011
Respuesta aceptada
Más respuestas (1)
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
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
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
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
el 6 de Sept. de 2011
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.
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!