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

2 ビュー (過去 30 日間)
Alberto Acri
Alberto Acri 2022 年 10 月 27 日
編集済み: Alberto Acri 2022 年 10 月 29 日
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 Center および File ExchangeGet 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