Images and rotation angles

Hola,
I have a theoretical question as I'm looking for ideas how to solve my current image analysis problem (a field which I am not an expert).
The problem is: I have 40 images of different types of drinking bottles in various rotation angles. Therefore, I am looking for a way how to automate the rotation process so all of them will have the same angle (neck up, base down), any idea how to solve it?
Thank you.

 Risposta accettata

Image Analyst
Image Analyst il 20 Apr 2013

0 voti

Basically you first need to find the bottle, by thresholding or whatever. Then you need to run regionprops and look for the orientation - that will tell you the angle - and the Centroid and MajorAxisLength. It will fit an ellipse to the bottle. Look at the location of the midpoint of the MajorAxis. It will be closer to one end (thick or thin - I'm not sure) of the bottle than the Centroid will be. So, knowing that you'll be able to rotate it so the thick end is down.
Please upload some of your images, including the binary images showing the detected bottle mask.

4 Commenti

Lluis Roca
Lluis Roca il 22 Apr 2013
Hola.
Thanks for the respond (already after threshold).
I uploaded some images:
Does there an example for your explanation in the documents.
What I'd probably do it to call regionprops() and find the orientation (angle).
measurements = regionprops(binaryImage, 'Orientation');
angle = measurements(1).Orientation;
angleToRotateBy = 90 - angle; % or "+" or 180 (instead of 90), you'd have to check.
Then use that in imrotate to rotate the image.
rotatedImage = imrotate(rotatedImage, angleToRotateBy);
After it's been rotated, I'd sum horizontally
verticalProfile = sum(rotatedImage, 2);
and look at the first 50 non-zero sums at the top and the bottom.
firstLine = find(verticalProfile > 0, 1, 'first');
lastLine = find(verticalProfile > 0, 1, 'last');
topSum = sum(verticalProfile(firstLine:firstLine+50));
bottomSum = sum(verticalProfile(lastLine-50:lastLine));
If the topSum is greater than bottomSum, it's upside down and needs to be rotated 180
if topSum > bottomSum
rotatedImage = imrotate(rotatedImage, 180);
end
Lluis Roca
Lluis Roca il 25 Apr 2013
Hola,
Thanks for the script. I will look in to it.
Gracias
Alex Taylor
Alex Taylor il 25 Apr 2013
Lluis,
Anyway you can post a few of the original, non-segmented images that you are interested in?
Thanks,
Alex.

Accedi per commentare.

Più risposte (1)

Anand
Anand il 21 Apr 2013
Another option is to register the images using imregister. If you know that the only deformation between the images is the rotation, you can use the 'rigid' transformation type. If there is a scale change involved, you should use the 'similarity' transformation type.
Here's a small example of how to use this:
% read in a test image
im = imread('cameraman.tif');
% rotate it to create another test image
imRot = imrotate(im,45);
figure; imshowpair(im,imRot,'montage');title('before registration');
% setup intensity-based image registration
[optimizer,metric] = imregconfig('monomodal');
% register imRot with im so that cameraman is upright
imReg = imregister(imRot, im, 'rigid', optimizer, metric);
figure; imshowpair(im,imReg,'montage');title('after registration');

Community Treasure Hunt

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

Start Hunting!

Translated by