How to rotate objects of a bounding box
Mostrar comentarios más antiguos
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 comentarios
Image Analyst
el 5 de Oct. de 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
el 6 de Oct. de 2015
Image Analyst
el 6 de Oct. de 2015
I see only one image.
Ryan
el 6 de Oct. de 2015
Respuestas (2)
Image Analyst
el 6 de Oct. de 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 comentarios
Ryan
el 6 de Oct. de 2015
Image Analyst
el 6 de Oct. de 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 comentarios
Walter Roberson
el 6 de Oct. de 2015
Attach the .m file. Image Analyst does not accept email about Questions.
Ryan
el 6 de Oct. de 2015
Ryan
el 6 de Oct. de 2015
Ryan
el 9 de Oct. de 2015
Image Analyst
el 9 de Oct. de 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.
Categorías
Más información sobre Image Preview and Device Configuration en Centro de ayuda y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!