Coloring pixels with a specific color

Is it possible to create an image (like the original) in which only the desired pixels are colored?
For example, I want to color white only the pixels with the numbers 25 to 27 (present inside 'grayLevel'). Any ideas?
rgb = imread('example.jpg');
%% Matrix of pixels
Number_Of_Rows = height(rgb);
Number_Of_Columns = width(rgb);
grayLevel = zeros(Number_Of_Rows,Number_Of_Columns);
for Row = 1:Number_Of_Rows
for Column = 1:Number_Of_Columns
grayLevel(Row,Column) = rgb(Row, Column);
end
end

 Respuesta aceptada

Image Analyst
Image Analyst el 24 de En. de 2023
You need to somehow get the gray scale image from the RGB image though, maybe like
grayImage = rgb2gray(rgbImage);
Then you can use imoverlay
mask = (grayImage >= 25) & (grayImage <= 27);
rgbImage = imoverlay(grayImage, mask, 'Color', 'w');

8 comentarios

Alberto Acri
Alberto Acri el 25 de En. de 2023
Thank you for your reply. How can I create mask in case I want to use values inside a vector?
For example: v = [25; 26; 27; 30]
Image Analyst
Image Analyst el 25 de En. de 2023
Editada: Image Analyst el 25 de En. de 2023
mask = (grayImage == v(1)) | (grayImage == v(2)) | (grayImage == v(3)) | (grayImage == v(4));
rgbImage = imoverlay(grayImage, mask, 'Color', 'w');
Alberto Acri
Alberto Acri el 25 de En. de 2023
Is there any way to compact the line with mask?
You could put it into a loop
mask = grayImage == v(1);
for k = 2 : length(v)
mask = mask | (grayImage == v(k));
end
rgbImage = imoverlay(grayImage, mask, 'Color', 'w');
Alberto Acri
Alberto Acri el 26 de En. de 2023
Thanks for the answer @Image Analyst! Whereas, if I have a V matrix of the type 3x2 where each row represents the coordinate of the pixel I want to color white? How can I do that?
Try this
for k = 1 : size(V, 1)
row = V(k, 1);
column = V(k, 2);
grayImage(row, column) = 255; % Or 65535 for uint16 images.
end
Alberto Acri
Alberto Acri el 26 de En. de 2023
And how can I visualize them? Using for example 'imoverlay'?
No, if you have a gray scale image and you just want white, not a "color", then you don't need imoverlay. You can just use imshow:
imshow(grayImage, []);

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Images en Centro de ayuda y File Exchange.

Productos

Versión

R2021b

Preguntada:

el 24 de En. de 2023

Comentada:

el 26 de En. de 2023

Community Treasure Hunt

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

Start Hunting!

Translated by