change the color of the pixels that are present in an image

2 次查看(过去 30 天)
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);

采纳的回答

DGM
DGM 2022-10-27
编辑:DGM 2022-10-27
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 个评论
Alberto Acri
Alberto Acri 2022-10-28
编辑:Alberto Acri 2022-10-29
Thank you for the explanation. I had not used the ColorThresholder app yet.
I wanted more help to improve the pink pixel capture because for example with the image "ABC.jpg" I happened to lose pixels:
I tried to make some changes to the row values:
L = [0.617 0.024; 0.081 0.657; 0 1]; % for pink, purple, blue
but I was unsuccessful in the task.
DGM
DGM 2022-10-28
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.

请先登录,再进行评论。

更多回答(1 个)

KALYAN ACHARJYA
KALYAN ACHARJYA 2022-10-27
编辑:KALYAN ACHARJYA 2022-10-27
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!

类别

Help CenterFile Exchange 中查找有关 Get Started with Image Processing Toolbox 的更多信息

产品


版本

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by