How to detect each color square of an image to crop as individual images

1 次查看(过去 30 天)
I need to detect each square region with some kind of rectangle detection, I looked around other questions and there are some solutions with gray images but I couldn't find anything to detect with colorful images.
Ideally I need to detect each square, crop that region as another image, then perhaps add that image to an array, apply this detection, cropping and adding to the array, for each color region (16 regions for my case) for further process with each one of them.
How can I do this? Any suggestions are greatly appreciated.
  3 个评论
Emre Havan
Emre Havan 2019-4-3
编辑:Emre Havan 2019-4-3
This is the original image, it was noised and I pre-processed it to remove noise.

请先登录,再进行评论。

回答(1 个)

Image Analyst
Image Analyst 2019-4-3
Really easy. Here are the steps (untested):
% Convert RGB to HSV color space.
hsvImage = rgb2hsv(rgbImage);
% Extract the Saturation channel and threshold it at, say, 0.25
mask = hsvImage(:,:,2) > 0.25;
% extract the 16 largest blobs
mask = bwareafilt(mask, 16);
% Get bounding boxes
props = regionprops(mask, 'BoundingBox')
% Have a for loop where you call imcrop with the bounding box, then call imwrite() to save the cropped image
for k = 1 : length(props)
thisBB = props(k).BoundingBox;
croppedImage = imcrop(rgbImage, thisBB);
filename = sprintf('Image #%d.png', k);
imwrite(croppedImage, filename);
end
Let me know if you have any trouble.
  4 个评论
Image Analyst
Image Analyst 2020-4-11
Stephen: pretty easy. Start a new question and define EXACTLY what you mean by "colour loactions". Do you want the centroid of the squares and circles? Do you just want, for every pixel in the image, a list of [x, y, r, g, b] in a CSV file? If so, see attached demo. If not, start a new question and I'll answer there.
Stephen Otieno
Stephen Otieno 2020-4-15
ImageAnalyst: I think i used the wrong description by saying "colour location", what i want is the centroid of only squares and I want to select a group of pixels in each sqaure to determine the colour and getting the values in a matrix of 4x4 in a cvs. Only using either lab or hsv.
thanks

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Image Segmentation and Analysis 的更多信息

产品


版本

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by