MATLAB Answers

Rotate individual labelled/segmented characters about its centroid

6 views (last 30 days)
Ronald Lim
Ronald Lim on 9 Sep 2020
Edited: Ronald Lim on 10 Jan 2021
Hi ,
How do I rotate the individual characters anti-clockwise 90 degrees about their own centroid?
I have obtained their centroids and other stuff:
Character # Mean Intensity Centroid(X) Centroid(Y) Area
# 1 54.8 16.7 57.6 109.0
# 2 62.8 33.5 57.6 136.0
# 3 79.5 51.5 57.3 106.0
# 4 66.1 59.9 28.1 145.0
# 5 62.4 68.1 56.7 100.0
# 6 65.6 77.2 29.0 100.0
# 7 55.4 85.6 55.2 56.0
# 8 55.5 94.9 26.0 50.0
# 9 67.3 100.2 56.9 118.0
#10 76.5 118.1 27.0 117.0
#11 65.7 116.6 56.4 118.0
#12 71.1 132.8 25.8 139.0
#13 59.9 130.7 53.2 102.0
#14 81.9 150.1 55.5 115.0
Things I have tried:
1.) tried cropping and rotate but could not crop individual characters and rotate
2.) Tried using BW2 = bwareafilt(BW,range) and rotate but the character rotate about the image centroid instead
3.) Wanted to tried bounded box but do not know how to write those codes
My codes
clear all;
I=imread('charact2.jpg');
J=imrotate(I,-180);
%Upright image
subplot(3, 3, 1);
imshow(J);
% Convert to binary image
J = im2bw(J,0.34);
%Remove symbol (row,column), convert to 1
J(6:42,7:41)=0;
subplot(3, 3, 2);
imshow(J)
% Identify each character by seeing which pixels are connected to each other.
labeledImage = bwlabel(J,8);
coloredLabels = label2rgb (labeledImage, 'hsv', 'k', 'shuffle');
subplot(3, 3, 3);
imshow(coloredLabels)
% Get all the char properties.
Measurements = regionprops(labeledImage,J,'all');
numberOfchar = size(Measurements, 1);
%get the centroids of ALL the char
fprintf(1,'Character # Mean Intensity Centroid(X) Centroid(Y) Area\n')
for k = 1 : numberOfchar
xCenter = Measurements(k).Centroid(1);
yCenter = Measurements(k).Centroid(2);
Pixels = Measurements(k).PixelIdxList;
Intensity = mean(I(Pixels));
CharArea = Measurements(k).Area;
charOrientation = Measurements(k).Orientation;
fprintf(1,'#%2d %18.1f %13.1f %12.1f %12.1f\n', k,Intensity, xCenter, yCenter, CharArea);
end

Accepted Answer

Madhav Thakker
Madhav Thakker on 16 Sep 2020
Hi Ronald,
I understand that you want to rotate the letters 90 degrees anticlockwise. You are correct about extracting bounding boxes. You can extract bounding boxes, rotate them, and paste them on a new image.
I am attaching a snippet that can help you.
another = zeros(size(J));
for k =1:numberOfchar
croppedImage = imcrop(J, Measurements(k).BoundingBox);
angle = 90;
rotated = imrotate(croppedImage, angle);
center_x = Measurements(k).Centroid(1);
center_y = Measurements(k).Centroid(2);
[rows, cols] = size(croppedImage);
prev_x = round(center_y - cols/2);
prev_y = round(center_x - rows/2);
another(prev_x:prev_x+cols-1,prev_y:prev_y+rows-1) = rotated;
end
imshow(another)
Hope this helps.
  6 Comments
Image Analyst
Image Analyst on 25 Oct 2020
You forgot to attach your m-file. Also since some characters are taller than wide, if you rotate them in place, the characters would be overlapped. Why do you want to do that? Why not just crop each character out to its own subimage to deal with it?

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!