How to rotate objects of a bounding box
6 views (last 30 days)
Show older comments
Hi everybody,
I have an image with three cards that I've processed to the point where I'm creating bounding boxes for every object detected. My question is, how and what can I use to rotate the objects determined by the bounding boxes so that they are in an upright position, mainly being the outline of the entire card? Here is the code I have so far that is creating the bounding boxes, and the image I have. The image isn't the best, but just so you guys have an idea of what I'm trying do.
%e
imshow(J)
%stats = regionprops(J)
stats = regionprops(J,'BoundingBox','Area');
AreaOb = regionprops(J,'Area')
PerOB = regionprops(J,'Perimeter')
[B,L] = bwboundaries(J, 'noholes');
figure; imshow(J); hold on;
for k = 1:length(B),
boundary = B{k};
plot(boundary(:,2),boundary(:,1),'g','LineWidth',2);
end
5 Comments
Answers (2)
Image Analyst
on 6 Oct 2015
Since you seem to have a complete rectangular outline, fill the cards with imfill.
binaryImage = imfill(edgeImage, 'holes');
Then get the orientation with regionprops(). The orientation for an "upright" card of a certain aspect ratio should be some known angle (not 0 or 90 though since the major axis may not align with the card edges). So then, knowing that, you can figure out the angle to rotate the cropped card subimage.
Put regionprops in a for loop where you're using ismember() to extract out each card one at a time, then use imcrop to crop it out. Untested code follows:
[labeledImage, numBlobs] = bwlabel(binaryImage);
for k = 1 : numBlobs
thisBlob = ismember(labeledImage, k);
measurements = regionprops(thisBlob, 'Orientation', 'BoundingBox');
croppedImage = imcrop(rgbImage, measurements.BoundingBox);
% Compute angle from measurements.Orientation
angle = .............
% Rotate image
uprightImage = imrotate(croppedImage, angle);
end
2 Comments
Image Analyst
on 6 Oct 2015
No. You're filling the edge detection image so we can determine the angle. Then, note how we're cropping the original RGB image, not the binary image, so you're getting the original color image of just one card.
Ryan
on 6 Oct 2015
Edited: Ryan
on 6 Oct 2015
5 Comments
Image Analyst
on 9 Oct 2015
I don't think Walter has the Image Acquisition Toolbox. I do, but I don't know when or if I'll ever get enough time to spend on completing your project for you. It looks like it will take more than 5 minutes, which is usually about all I'll spend on consulting free for someone. Perhaps if you ask smaller, more targeted questions that can be quickly answered.
See Also
Categories
Find more on Computer Vision with Simulink 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!