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.

enter image description here

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]

Mathematica graphics


2 Answers 2


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]

Mathematica graphics

ColorCombine[{img, i}]

Mathematica graphics

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

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

Mathematica graphics

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

Mathematica graphics

  • $\begingroup$ Funny.When the MorphologicalTransform[pic//Binarize,RotationTransform[Pi/2,1/.ComponentMeasurements[pic,"Centroid"]]] comes to me. $\endgroup$
    – yode
    Feb 24, 2016 at 16:52
  • $\begingroup$ @yode Does that work? $\endgroup$ Feb 24, 2016 at 19:03
  • $\begingroup$ As your second case,I give my solution with Image-Tools to select the component that we wanna rotate following. $\endgroup$
    – yode
    Feb 26, 2016 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

enter image description here

Then Copy as Marker Image.We get enter image description here

Hightligt the components we selected

img = Binarize@Import@"https://i.stack.imgur.com/4SYJS.png";
part = GeodesicDilation[Image[#], img] & /@ 
   Values[ComponentMeasurements[imgtoolmask, "Mask"]];
HighlightImage[img, ImageAdd[part]]

enter image description here

Then rotate it.

partrote = 
      1 /. ComponentMeasurements[#, "Centroid"]], 
     DataRange -> Full] & /@ part;
ImageAdd[ImageSubtract[img, Sequence @@ part], Sequence @@ partrote]

enter image description here

  • $\begingroup$ +1 Have you seen this? :) $\endgroup$ Feb 28, 2016 at 2:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.