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

77 views (last 30 days)
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 Comments
Emre Havan
Emre Havan on 3 Apr 2019
Edited: Emre Havan on 3 Apr 2019
This is the original image, it was noised and I pre-processed it to remove noise.

Sign in to comment.

Answers (1)

Image Analyst
Image Analyst on 3 Apr 2019
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 Comments
Image Analyst
Image Analyst on 11 Apr 2020
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 on 15 Apr 2020
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

Sign in to comment.

Categories

Find more on Image Processing Toolbox in Help Center and File Exchange

Products


Release

R2019a

Community Treasure Hunt

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

Start Hunting!