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

1 Ansicht (letzte 30 Tage)
Emre Havan
Emre Havan am 3 Apr. 2019
Kommentiert: DGM am 5 Mär. 2023
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 Kommentare
Emre Havan
Emre Havan am 3 Apr. 2019
Bearbeitet: Emre Havan am 3 Apr. 2019
This is the original image, it was noised and I pre-processed it to remove noise.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Image Analyst
Image Analyst am 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 Kommentare
Image Analyst
Image Analyst am 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 am 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

Melden Sie sich an, um zu kommentieren.

Produkte


Version

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by