How to rotate objects of a bounding box
Afficher commentaires plus anciens
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 commentaires
Image Analyst
le 5 Oct 2015
Let's say you have an irregularly shaped blob, say one that looks like an asterisk or a splat. What determines "upright"? Do you want the major axis, as determined by the property "Orientation" returned from regionprops(), to be vertical? Or do you have some other definition? And why do you want it upright? Why can't it be analyzed as it is? A picture would be helpful.
Ryan
le 6 Oct 2015
Image Analyst
le 6 Oct 2015
I see only one image.
Ryan
le 6 Oct 2015
Réponses (2)
Image Analyst
le 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 commentaires
Ryan
le 6 Oct 2015
Image Analyst
le 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.
5 commentaires
Walter Roberson
le 6 Oct 2015
Attach the .m file. Image Analyst does not accept email about Questions.
Ryan
le 6 Oct 2015
Ryan
le 6 Oct 2015
Ryan
le 9 Oct 2015
Image Analyst
le 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.
Catégories
En savoir plus sur Image Preview and Device Configuration dans Centre d'aide et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!