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

조회 수: 6(최근 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');

채택된 답변

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
  댓글 수: 5
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 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!

Community Treasure Hunt

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

Start Hunting!

Translated by