필터 지우기
필터 지우기

Detecting the position of a previously colored rectangle in an image

조회 수: 1 (최근 30일)
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

채택된 답변

Guillaume
Guillaume 2018년 4월 19일

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
Guillaume
Guillaume 2018년 4월 19일
편집: Guillaume 2018년 4월 19일
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 2018년 4월 24일

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!

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Gopichandh Danala
Gopichandh Danala 2018년 4월 18일

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');

카테고리

Help CenterFile Exchange에서 Images에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by