How to rotate objects of a bounding box

6 views (last 30 days)
Ryan
Ryan on 5 Oct 2015
Commented: Image Analyst on 9 Oct 2015
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
Ryan
Ryan on 6 Oct 2015
They're the same image, I just tried to include one if the other wasn't showing.

Sign in to comment.

Answers (2)

Image Analyst
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
Ryan
Ryan on 6 Oct 2015
Thank you, that will help out me out a lot. The only thing is, if I use the imfill() function, won't that completely get rid of the suit and rank information in the corners of the cards? Is there any way to 'unfill' the card after the rotation?
Image Analyst
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.

Sign in to comment.


Ryan
Ryan on 6 Oct 2015
Edited: Ryan on 6 Oct 2015
Image Analyst, is there any way I might be able to send you what I have on my m-file so you could check everything out. I've got all the preprocessing done, but I'm really having a hard time understanding and implementing this part with my code, even with the help you gave me. I know it's asking a lot, but if you could add any additional code to help me solve this, or just add some more notes/advice on how I can do it directly to my code I would appreciate it very much. This is frustrating me more than anything, and I feel like I keep going backwards with everything I try.
  5 Comments
Ryan
Ryan on 9 Oct 2015
Did you think you could help at all with any of these files Walter? I'm still having trouble trying to solve this all.
Image Analyst
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.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!