Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

share|improve this question
up vote 16 down vote accepted

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@"http://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

share|improve this answer
    
Funny.When the MorphologicalTransform[pic//Binarize,RotationTransform[Pi/2,1/.ComponentMeasure‌​ments[pic,"Centroid"]]] comes to me. – yode Feb 24 at 16:52
    
@yode Does that work? – Dr. belisarius Feb 24 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 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@"http://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 = 
  ImageForwardTransformation[#, 
     RotationTransform[Pi/2, 
      1 /. ComponentMeasurements[#, "Centroid"]], 
     DataRange -> Full] & /@ part;
ImageAdd[ImageSubtract[img, Sequence @@ part], Sequence @@ partrote]

enter image description here

share|improve this answer
    
+1 Have you seen this? :) – Dr. belisarius Feb 28 at 2:49

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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