change the color of the pixels that are present in an image
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Alberto Acri
el 27 de Oct. de 2022
Editada: Alberto Acri
el 29 de Oct. de 2022
I would like to change the color of pixels in an image.
Specifically, I want to turn white pixels to black and make the number in the lower right-hand corner disappear as in the figure below. Of course, the size of the figure must remain the same.
I am currently using this code:
Im = imread('./Images/Example.jpg');
figure(1);
imshow(Im);
0 comentarios
Respuesta aceptada
DGM
el 27 de Oct. de 2022
Editada: DGM
el 27 de Oct. de 2022
Updated to accomodate new images and fix the bad masking expression. In this example, I'm going to use this composite image:
% this image is a composite of all the images given so far
inpict = imread('loopall.png');
% select objects to keep
% [Hmin Hmax; Smin Smax; Vmin Vmax]
%L = [0.907 0; 0.101 0.429; 0 1]; % for pink only
L = [0.617 0.024; 0.081 0.657; 0 1]; % for pink, purple, blue
hsvpict = rgb2hsv(inpict);
mask = ((hsvpict(:,:,1) >= L(1,1)) | (hsvpict(:,:,1) <= L(1,2))) & ...
((hsvpict(:,:,2) >= L(2,1)) & (hsvpict(:,:,2) <= L(2,2))) & ...
((hsvpict(:,:,3) >= L(3,1)) & (hsvpict(:,:,3) <= L(3,2)));
% clean up the mask, dilate
mask = bwareaopen(mask,100);
mask = imdilate(mask,strel('disk',3));
% clear areas not covered by the mask
outpict = inpict;
outpict(repmat(~mask,[1 1 3])) = 0;
% display modified image
imshow(outpict)
5 comentarios
Alberto Acri
el 28 de Oct. de 2022
Editada: Alberto Acri
el 29 de Oct. de 2022
DGM
el 28 de Oct. de 2022
That's not an issue with the color-based segmentation part. That's being caused during mask cleanup.
This line
mask = bwareaopen(mask,100);
removes all mask regions with area < 100px. This is there to help eliminate any unattached speckles that might show up near object edges (e.g. due to JPG artifacts, etc).
If you want to preserve small spots, you can either reduce the area parameter or just omit that line. Considering that the spot size is only 7px, there might not be much point trying to filter anything smaller.
Más respuestas (1)
KALYAN ACHARJYA
el 27 de Oct. de 2022
Editada: KALYAN ACHARJYA
el 27 de Oct. de 2022
It can be done in multiple ways, use morphological operations or see the regionprops function (very useful). Easyest way for specific case-
BW2 = bwareaopen(BW1,200);
figure, imshow(BW2);
Ensure that BW2 is a binary image
Hope it Helps!
0 comentarios
Ver también
Categorías
Más información sobre Get Started with Image Processing Toolbox en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!