我正在尝试将imrotate应用于图像,但图像的角度未知,并随每张图像而变化。
如果我已将图像转换为二进制,那么可以找到最低y位置'1'和最左边'1'并使用它们之间的渐变/角度作为我图像旋转的角度?
使用这两个位置之间的角度并将其与x轴对齐?
当前进度 - 转换为二进制并使边缘更具可辨性:
% convert to binary
greyImage = rgb2gray(C); % greyscale
cannyImage = edge(greyImage, 'canny'); % canny edge detection
% fill the gaps in the shape
se = strel('disk',2);
bw = imclose(cannyImage, se);
filled = imfill(bw, 'holes');
imshow(filled);
[~,lowerMostCol] = max(cumsum(sum(filled,2)));
[~,leftMostRow] = max(sum(filled,1)==1);
答案 0 :(得分:1)
方法#1
使用a
作为二进制图像,您可以执行以下操作 -
[~,lowermost] = max(cumsum(sum(a,2)));
lowermostpt = [lowermost,find(a(lowermost,:),1,'first')]
[~,rightmost] = max(cumsum(sum(a,1)));
rightmostpt = [find(a(:,rightmost),1,'first'),rightmost]
[~,topmost] = max(sum(a,2)==1);
topmostpt = [topmost,find(a(topmost,:),1,'first')]
[~,leftmost] = max(sum(a,1)==1);
leftmostpt = [find(a(:,leftmost),1,'first'),leftmost]
为了提高性能,最好将总结存储一次,然后再重复使用。
示例运行 -
a =
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 1 0 0 0 0 0
0 0 0 0 0 0 0 0 1 1 1 0 0 0 0
0 0 0 0 0 0 0 1 1 1 1 1 0 0 0
0 0 0 0 0 0 1 1 1 1 1 1 1 0 0
0 0 0 0 0 1 1 1 1 1 1 1 1 1 0
0 0 0 0 0 0 1 1 1 1 1 1 1 0 0
0 0 0 0 0 0 0 1 1 1 1 1 0 0 0
0 0 0 0 0 0 0 0 1 1 1 0 0 0 0
0 0 0 0 0 0 0 0 0 1 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
lowermostpt =
12 10
rightmostpt =
8 14
topmostpt =
4 10
leftmostpt =
8 6
方法#2 使用图像处理工具箱中的bwboundaries
-
idx = cell2mat(bwboundaries(a))
[~,p1] = min(idx(:,1))
topmostpt = idx(p1,:)
[~,p2] = max(idx(:,1))
lowermostpt = idx(p2,:)
[~,p3] = min(idx(:,2))
leftmostpt = idx(p3,:)
[~,p4] = max(idx(:,2))
rightmostpt = idx(p4,:)