# How can i make automatic rotation using PCA to set the galaxies oriantation to be horizontal?

8 visualizaciones (últimos 30 días)
Aya Ahmed el 7 de Feb. de 2020
How can i make automatic rotation using PCA to set the galaxies oriantation to be horizontal and cropping the image?
Knowing that the galaxies in the original images have different directions
I searched a lot and could not find any code
##### 1 comentarioMostrar -1 comentarios más antiguosOcultar -1 comentarios más antiguos
Adam Danz el 7 de Feb. de 2020
If you're working with image data you could use the Orientation property of the regionprops() function to get the angle between the x-axis and the major axis of the galaxy ellipse.

Iniciar sesión para comentar.

Adam Danz el 7 de Feb. de 2020
This approach uses the Orientation property of the regionprops() function to get the angle between the x-axis and the major axis of the galaxy ellipse. It uses the orientation angle to rotate the image so the galaxy is horizontal.
There are several plots produced to show the steps of this process. They can be commented out in your final function/script. The image file I'm analyzing in this script is attached.
See the inline comments for detail.
% Identify file
file = 'PGC0001862.png'; % full path is better; see fullfile()
% Binarize the image
BW = imbinarize(rgb2gray(I));
% Filter out noise by isolating the object with the largest perimeter
BWfilter = bwpropfilt(BW,'perimeter',1);
% Show orginal and binary images for a sanity check
clf()
imshowpair(I,BWfilter,'montage')
% Get the orientation of the main object
stats = regionprops(BWfilter,'Orientation'); % 56.981 for this image
% rotate the image
IRot = imrotate(I, -stats.Orientation);
% Show image before/after rotation for sanity check
clf()
subplot(1,2,1)
imshow(I)
subplot(1,2,2)
imshow(IRot)
linkaxes(); % to match aspect ratios
% Crop the rotated image based on the limits of the binary image
BWfilterRot = imrotate(BWfilter, -stats.Orientation);
[rows, cols] = find(BWfilterRot);
BWcrop = BWfilterRot(min(rows):max(rows), min(cols):max(cols)); % only needed for sanity-check
Icrop = IRot(min(rows):max(rows), min(cols):max(cols),:);
% Show the cropped RGB and BW images
clf()
subplot(1,2,1)
imshow(Icrop)
subplot(1,2,2)
imshow(BWcrop)
linkaxes(); % to match aspect ratios
% Show original and final image
clf()
subplot(1,2,1)
imshow(I)
subplot(1,2,2)
imshow(Icrop)
linkaxes(); % to match aspect ratios
##### 5 comentariosMostrar 3 comentarios más antiguosOcultar 3 comentarios más antiguos
Aya Ahmed el 16 de Feb. de 2020
Editada: Aya Ahmed el 16 de Feb. de 2020
Thank you very very much, it finally working
I applied it to all kinds of galaxies I have, and it worked
Adam Danz el 16 de Feb. de 2020
Thanks for the feedback!

Iniciar sesión para comentar.

### Más respuestas (1)

Image Analyst el 10 de Feb. de 2020
One way that does not depend on selecting a threshold for your image is to use the radon transform. Look how I used it to rotate the football to the two image axes:
Basically the radon transform gets the projection of the image along a line perpendicular to the projection direction. This is the concept of CT - computed tomgraphy - and MRI. You can just read off the angle by looking at the maximum of the radon transform, then call imrotate() to do the rotation by that angle. See attached demo script. Adapt as needed.
##### 2 comentariosMostrar NingunoOcultar Ninguno
Adam Danz el 10 de Feb. de 2020
This is one of my favorite things about volunteering here. This morning I spent some time reading about the Radon transfer and it's definitely one I'll keep in mind. Thanks for sharing, Image Analyst! I applied your demo to the galaxy image and it worked well. The transfer in the demo works on the grayscaled image which is the red component of the RGB image. Since the galaxy backgrounds are all black, I suppose it wouldn't matter which RGB value was extracted (it certainly didn't matter for the image I tested). I wonder how it would perform in the galaxies listed in the bottom row of the image in the question.
Aya Ahmed el 16 de Feb. de 2020
Thank you very much @Image Analyst , I applied your demo to the galaxy image and it worked well.

Iniciar sesión para comentar.

### Categorías

Más información sobre Morphological Operations en Help Center y File Exchange.

### Community Treasure Hunt

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

Start Hunting!

Translated by