Detecting the position of a previously colored rectangle in an image

4 views (last 30 days)
Hello,
do you think I can use MatLab for detecting the position of a previously colored rectangle in an image? I am new at MatLab and currently thinking about if it is possible to use this software for my purpose. Do you know any similar projects or tutorials, which could help me further?
PS: I attached an example image, e. g. how can I get the position of all blue rectangles?
Best regards Max

Accepted Answer

Guillaume
Guillaume on 19 Apr 2018

Since your image is indexed, with only 4 colours (0 = reddish, 1 = bluish, 2 = yellowish and 3 = white) it's actually trivial to detects the pixels of each colour:

[img, map] = imread('example.png');
bluepixels = img == 1;

You can then use regionprops to get the pixels, bounding box, perimeter, etc. of the rectangles:

props = regionprops(bluepixels, 'basic');  %will return the bounding box, area and centroid of each blue object.
  3 Comments
Guillaume
Guillaume on 19 Apr 2018
Edited: Guillaume on 19 Apr 2018
The first thing to establish is whether all the images are indexed images, as your example is, or if some are rgb.
If they are all indexed, then the only thing you have to do is identify which index correspond to the colour you want. Is the colour palette that is used to create your images fixed?
Maximilian Stopfer
Maximilian Stopfer on 24 Apr 2018

Update: following code works to 95%

ImA = imread('image.png');
ImA1 = 255 - ImA(:,:,1);
th = 0.98;
ImA2 = imbinarize(ImA1, th);
out = regionprops(ImA2);
writetable(struct2table(out), 'out.xlsx');

I changed my strategy and marked the specific areas (with a greyish background) just with green rectangles, then ran the code and exported it as an excel sheet. Since there are some non-sense values I want to add a filter to delete all values between two limits to exclude these values. But this is another task. Thank you for your help!

Sign in to comment.

More Answers (1)

Gopichandh Danala
Gopichandh Danala on 18 Apr 2018

You can use thresholding, I am using hsv to fo this.

[img, map] = imread('example.png');
if ~isempty(map)
    colorImg = ind2rgb(img,map);
end
% Convert RGB image to HSV
hsvImage = rgb2hsv(colorImg);
% Extract out the H, S, and V images individually
hImage = hsvImage(:,:,1);
sImage = hsvImage(:,:,2);
vImage = hsvImage(:,:,3);
% filter using hImage
filterImage = hImage > 0.6; % find this value from hImage
blueMap = imfill(filterImage,'holes');
figure, 
subplot(131), imshow(colorImg), title('Orig color image');
subplot(132), imshow(filterImage), title('Filtered image');
subplot(133), imshow(blueMap), title('Blue map image');

Categories

Find more on Images 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!