
Binary to RGB image with specific colour in specific area
3 views (last 30 days)
Show older comments
I need to convert a binary image to RGB image with specific colour in three specfic area. Actually this discrimination is needed to separate three areas. I am attaching two images (one binary and one RGB) to show how the RGB image should look like.
0 Comments
Accepted Answer
yanqi liu
on 20 Jan 2022
yes,sir,may be use regionprops to compute region property,and make rule to find first、second、third class,use findpeaks to segment rectangle block,such as

but the segment method should be consider,because the segment locating may be not match
More Answers (2)
Image Analyst
on 20 Jan 2022
Well, I could do it, but not within the few minutes I typically donate to people. There are basically 2 ways: the traditional shape analysis way, and the deep learning way.
If you want deep learning I suggest you use SegNet and have lots of training images.
If you want to do the traditional way, what I'd do is to use regionprops to find the centroid of all the blobs
props = regionprops(mask, 'Centroid');
xy = vertcat(props.Centroid);
and find all blobs whose centroids are with some distance of the center of the image, like 10%
x = xy(:, 1);
[rows, columns, numberOfColorChannels] = size(mask)
middleColumn = columns/2;
inMiddle = find((x > 0.45 * middleColumn) & (x < 0.55 * middleColumn));
[labeledImage, numBlobs] = bwlabel(mask);
blueBlobs = ismember(labeledImage, inMiddle);
Now to find the green balls at the end of the zipper teeth is trickier. I might start by taking each blob and getting the boundary with bwboundaries(). Then get the coordinates that are the leftmost 10 columns (for the right zipper) or rightmost 10 columns (for the left zipper). Then take those and fit a circle to them with the FAQ:
Then use the center and radius (gotten from the fit) to create x and y for a perfect circle. Now use poly2mask to create a perfect circle mask. Do that for each zipper tooth. So now you have an array of balls.
greenBlobs = false(rows, columns);
for k = 1 : numBlobs
thisBlob = ismember(labeledImage, k);
% Get boundary
boundary = bwboundaries(thisBlob);
b = boundary{1}; % Pull out of cell array.
% Get x and y coordinates.
xb = b(:, 2);
yb = b(:, 1);
% Get centroid
props = regionprops(thisBlob, 'Centroid');
xy = vertcat(props.Centroid);
x = xy(:, 1);
% Determine if the blob is to the right or left.
if x < columns/2
% It's on the left
% Find rightmost coordinates.
maxx = max(x);
indexes = x > maxx - 10; % All coordinates within 10 pixels of the right end.
else
% It's on the right.
end
% Now fit xb and yb to a circle using the FAQ.
% to do, create xc and yc using the FAQ
% Now turn into a mask
circleMask = poly2mask(xCircle, yCircle, rows, columns);
greenBlobs = greenBlobs | circleMask
end
You can use that plus the blueBlobs mask to get the red teeth
redBlobs = mask; % Initialize
redBlobs(~blueBlobs) = false; % Erase where there are blue blobs.
redBlobs(~greenBlobs) = false; % Erase where there are green blobs.
Then you can use bwareaopen() to clean up any little tiny bits left over by erasing the green and blue blobs.
redBlobs = bwareaopen(redBlobs, 50); % Remove litter (small blobs)
I'm not going to do all of it for you, you can do that and learn something, but this is a good start. I've already spent more time than I want to spend tonight on that. This is untested code off the top of my head so expect to do some debugging, because there will be some errors.
0 Comments
See Also
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!