Rotate an object in image about its centroid
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);
ir = ImageForwardTransformation[Image[mask], RotationTransform[Pi/2, center],
DataRange -> Full];
ImageAdd[ImageMultiply[img, ColorNegate@Image@mask], ir]
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] & /@
Values[ComponentMeasurements[imgtoolmask, "Mask"]];
HighlightImage[img, ImageAdd[part]]
Then rotate it.
partrote =
ImageForwardTransformation[#,
RotationTransform[Pi/2,
1 /. ComponentMeasurements[#, "Centroid"]],
DataRange -> Full] & /@ part;
ImageAdd[ImageSubtract[img, Sequence @@ part], Sequence @@ partrote]