# Rotate an object in image about its centroid

I want to rotate an object in a image about its centroid. here is my code but the result is not as expected. Normally the centroid of the object does not change, except the case when the object is very close to the edge of the image.

edge = MorphologicalPerimeter[img] // DeleteSmallComponents;

center = Round[ComponentMeasurements[edge, "Centroid"][[1, 2]]]
(*we can use directly the image instead of the edge.*)
im2 = ImageForwardTransformation[img,N[RotationMatrix[90 Degree].(# - center)] &,
DataRange -> Full]


Those damn coordinate systems! :)

You can see a small part of the object at the lower left corner of your result image. That's because you forgot to add the center vector after the rotation to translate it to the right position:

i = ImageForwardTransformation[img, center + RotationMatrix[Pi/2].(# - center) &,
DataRange -> Full]


or better still:

i = ImageForwardTransformation[img, RotationTransform[Pi/2, center], DataRange -> Full]


ColorCombine[{img, i}]


As a simple application, let's rotate the center component (only) in the following image:

img = Binarize@Import@"http://i.stack.imgur.com/4SYJS.png"


cms = ComponentMeasurements[img, {"Centroid", "Mask"}];
centerComp = SortBy[cms, Norm[ImageDimensions@img /2 - #[[2, 1]]] &][[1, 1]];
{center, mask} = (centerComp /. cms);
DataRange -> Full];


• Funny.When the MorphologicalTransform[pic//Binarize,RotationTransform[Pi/2,1/.ComponentMeasurements[pic,"Centroid"]]] comes to me. – yode Feb 24 '16 at 16:52
• @yode Does that work? – Dr. belisarius Feb 24 '16 at 19:03
• As your second case,I give my solution with Image-Tools to select the component that we wanna rotate following. – yode Feb 26 '16 at 19:14

As the @Dr. belisarius edited his answer just now,I wanna post a method to solve it about his second case.

First,Use the Image-Tools to select one or more components to transform

Then Copy as Marker Image.We get

Hightligt the components we selected

img = Binarize@Import@"http://i.stack.imgur.com/4SYJS.png";
part = GeodesicDilation[Image[#], img] & /@

partrote =