regionprops Bounding box property returns values outside image dimensions
조회 수: 29 (최근 30일)
이전 댓글 표시
Hi,
I am using Matlab R2022a. I am trying to draw bounding boxes to an image considering English alphabets. I am extremely new to Matlab so I apologize if my question or technique is wrong.
I considered using thresholding after binarizing the image and then using connected components to find the bounding boxes. Here is my code:
image = imread(image_path);
image = im2double(imcomplement(image));
image1 = imbinarize(image_comp);
connected = bwconncomp(image1);
imshow(image1);
props = regionprops(connected, 'BoundingBox');
for i = 1:connected.NumObjects
if(props(i).BoundingBox(1) > connected.ImageSize(1))
rectangle('Position', props(i).BoundingBox,'EdgeColor', [0, 1, 0], 'LineWidth', 2)
continue;
end
rectangle('Position', props(i).BoundingBox,'EdgeColor', [1, 0, 0], 'LineWidth', 2)
end
I noticed that some of the x values in the bounding boxes returned were outside the dimensions of the actual image (drawn in green colour). So I am unable to crop these regions of the image.
Here is the output I receive:
Please advice on what I am doing wrong.
댓글 수: 1
Image Analyst
2022년 9월 21일
Not sure what you mean. All boxes, red or green, completely contain one blob. None of the bounding box x values of the left edge should be outside the image (below a value of 1).
props = regionprops(connected, 'BoundingBox');
boundingBoxes = vertcat(props.BoundingBox); % List of [xLeft, yTop, width, height] values. Each row is a different blob.
채택된 답변
Image Analyst
2022년 9월 22일
이동: Image Analyst
2022년 9월 23일
OK @sarvan_keep you haven't answered either of us so I guess you must be having great difficulty. So attached is a full demo that identifies the letters, put boxes around them, and also crops out the bounding boxes into new sub images. You can then process each sub image (like do OCR on it) or save it to disk, whatever you want.
Another thing left to do is to merge bounding boxes of small blobs, like dots above the i, into the nearest other box. But I'm sure you can do that. There is a variety of ways to do that.
Please respond.
댓글 수: 11
Dhuwaragish Ravichandrakumar
2022년 12월 21일
@Image Analyst Can I get your help with understanding on how to plot a 1D graph which shows the variations of the black and white stripes within the boundingbox
Image Analyst
2022년 12월 21일
I can try. Let's start a new discussion thread so we don't hijack @sarvan_keep's thread and bombard him with unrelated emails. I'll let you post the completely new question in your own thread. If the title is something image related, I'll see it.
If you have any more questions, then attach your data and code to read it in with the paperclip icon after you read this:
추가 답변 (1개)
Matt J
2022년 9월 21일
편집: Matt J
2022년 9월 21일
You should be comparing BoundingBox(1) with ImageSize(2)
image = imread('https://www.mathworks.com/matlabcentral/answers/uploaded_files/1132160/image.jpeg');
image_comp = im2gray(imcomplement(image));
image1 = imbinarize(image_comp);
connected = bwconncomp(image1);
imshow(image1);
props = regionprops(connected, 'BoundingBox');
for i = 1:connected.NumObjects
if(props(i).BoundingBox(1) > connected.ImageSize(2))
rectangle('Position', props(i).BoundingBox,'EdgeColor', [0, 1, 0], 'LineWidth', 2)
continue;
end
rectangle('Position', props(i).BoundingBox,'EdgeColor', [1, 0, 0], 'LineWidth', 2)
end
댓글 수: 3
Matt J
2022년 9월 23일
You're welcome, but please Accept-click the answer to indicate that it addressed your question.
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!