change the color of the pixels that are present in an image
8 views (last 30 days)
Show older comments
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 Comments
Accepted Answer
DGM
on 27 Oct 2022
Edited: DGM
on 27 Oct 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 Comments
DGM
on 28 Oct 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.
More Answers (1)
KALYAN ACHARJYA
on 27 Oct 2022
Edited: KALYAN ACHARJYA
on 27 Oct 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 Comments
See Also
Categories
Find more on Annotations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!