Finding pixel numbers for certain colors
5 views (last 30 days)
Show older comments
Hi,
I want to be able to find which pixels are red which pixels are blue but with an extra issue. If a pixel is at the margin between red and blue, I want to find those pixels and group those pixels with any pixel that is two pixels away from it. I am attaching both the image I am working on and the one explaining the pixel issue. For the circled pixel in the image 'pixel selection', I need the pixel numbers of other marked ones. In the end since there will be coinciding pixel numbers (when it is applied for all pixels at the red-blue interface) I will eliminate the repeating ones directly. In the end I only need the pixel numbers for red, blue and red-blue interface.
Thanks in advance.
2 Comments
Image Analyst
on 13 Sep 2023
Edited: Image Analyst
on 13 Sep 2023
Not sure of your definition of "pixel numbers" or "coinciding pixel numbers". Do you want the RGB histogram ( a count of the numbers of pixels with each unique color)?
DGM
on 13 Sep 2023
Edited: DGM
on 13 Sep 2023
The goal as described seems to be a list of the indices (or subscripts) of the 24 pixels surrounding each pixel in the transition area. That is, for subscripts, a big 600x1000x24x2 array (or some equivalent shape). How that would be used, I don't know.
That seems pretty questionable to me, so I'm just waiting to see if and why the direct use of the masks won't suffice.
Accepted Answer
DGM
on 12 Sep 2023
Edited: DGM
on 12 Sep 2023
That depends entirely on how you want to make the distinction between "red" or "blue" and colors which are in-between, because there are probably more colors in-between than what you think there are.
% the regions you think are uniform red/blue
% are not uniform at all since this is a JPG
inpict = imread('https://www.mathworks.com/matlabcentral/answers/uploaded_files/1480206/fullarea.jpg');
CT = [254 0 0; 0 0 254]; % the two color classes
% create masks
redmask = all(inpict == permute(CT(1,:),[1 3 2]),3);
blumask = all(inpict == permute(CT(2,:),[1 3 2]),3);
% the transition area is probably a lot larger
% and more irregular than you expect because of that damage
F = imfuse(redmask,blumask); % visualze both masks together
imshow(F,'border','tight')
% generate transition mask
rbmask = ~redmask & ~blumask; % these are the pixels in-between
rbmask = imdilate(rbmask,ones(5)); % pixels within 2px of in-between pixels
imshow(rbmask,'border','tight')
That's probably not very useful. Since we need something other than an exact match, it's probably easier to approach this from hue alone.
inpict = imread('https://www.mathworks.com/matlabcentral/answers/uploaded_files/1480206/fullarea.jpg');
% start by discriminating in H
% rotate H to avoid dealing with a split histogram
[H,~,~] = rgb2hsv(inpict);
H = mod(H-2/3+0.1,1);
% generate red/blue masks
H0 = [0.1 0.43]; % the target hues (shifted)
halfw = 0.05; % the tolerance
redmask = abs(H-H0(2)) <= halfw;
blumask = abs(H-H0(1)) <= halfw;
F = imfuse(redmask,blumask);
imshow(F,'border','tight')
% generate transition mask
rbmask = ~redmask & ~blumask;
rbmask = imdilate(rbmask,ones(5));
imshow(rbmask,'border','tight')
Note that the dilated rbmask overlaps both redmask and blumask now. If you need to remove the overlap area from redmask and blumask, just do
redmask = redmask & ~rbmask;
blumask = blumask & ~rbmask;
Similarly, the same thing could be accomplished by eroding those masks prior to generating rbmask, thereby eliminating the need to dilate rbmask.
0 Comments
More Answers (0)
See Also
Categories
Find more on Convert Image Type in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!