【问题标题】:Detect angle of text in image with horizontal and rotate in matlab [closed]检测图像中文本的水平角度并在matlab中旋转[关闭]
【发布时间】:2014-01-15 05:19:30
【问题描述】:

见下图:

正如您在图像中看到的那样,书面文字旋转了 90 度角,我只想将文字旋转为水平。无论旋转什么角度,我都想让它水平如下:

我不想旋转完整的图像。我希望仅对文本进行限制,并测量文本旋转的角度,然后按该角度旋转以使其水平。

你能建议我这样做,然后在文本上放置一个椭圆吗?

谢谢。

【问题讨论】:

标签: image matlab image-processing computer-vision text-rotation


【解决方案1】:

首先,让我们找到所有暗像素的 x-y 坐标

bw = imread('http://i.imgur.com/0LxC6bd.png');
bw = min( bw, [], 3 ) < 50 ; % dark pixels - intensity lower than 50
[y x] = find( bw ); % note that find returns row-col coordinates.

计算文本坐标的协方差矩阵

mx = mean(x);
my = mean(y);
C = [ mean( (x-mx).^2 ),     mean( (x-mx).*(y-my) );...
      mean( (x-mx).*(y-my) ) mean( (y-my).^2 ) ];

可以从C的特征向量和特征值得到椭圆的方向:

[V D] = eig( C );
figure; imshow( bw ); hold on;
quiver( mx([1 1]), my([1 1]), (V(1,:)*D), (V(2,:)*D), .05 );  

看特征向量和特征值:

V =
-0.9979   -0.0643
-0.0643    0.9979

D = 
1.0e+003 *
0.1001         0
     0    1.3652

您可以看到特征向量(V 的列)大致指向-X 方向(第一列)和Y 方向(第二列)。检查特征值(D 的对角线),您可以看到第二个特征值比第一个大得多 - 这是椭圆的主轴。现在您可以恢复椭圆的方向:

[~, mxi] = max(diag(D)); % find major axis index: largest eigen-value

从对应的特征向量中恢复角度

or = atan2( V(2,mxi), V(1,mxi) ) * 180/pi ; % convert to degrees for readability
or =
93.6869

如您所见,椭圆的长轴与地平线几乎成 90 度角。 您可以将图像旋转回来

imrotate( bw, -or );


给定协方差矩阵画一个椭圆:

th = linspace(0, 2*pi, 500 );
xy = [cos(th);sin(th)];
RR = chol( C ); % cholesky decomposition
exy = xy'*RR; %//'
figure;imshow( bw ); hold on;
plot( 2*exy(:,1)+mx, 2*exy(:,2)+my, 'r', 'LineWidth', 2 );

【讨论】:

  • 我理解了大部分内容,但是 MATLAB 中有一个函数 'quiver',我使用的是 MATLAB R2012b,我没有找到类似的函数,我得到了类似 'quiver' mathworks.in/help/matlab/ref/quiver.html
  • @Ritz 我的错字 - 已更正。谢谢。
  • 您好先生,我使用了您的代码并将其组装在一起pastebin.com/aPVzQEdp 但我没有得到所需的输出:imgur.com/TjFK2lq
  • @Ritz,通过将所有代码放在一起而不是将其分离到函数/块中,您运行了 mx 的存储值 - x 坐标的平均值(在函数 [mx mxi] = max( diag(D) ); . 恢复mx 的值,你应该没问题。
  • 我现在完美地得到了椭圆,但是我仍然没有得到旋转的文本。能否请您将它们分成模块并将.m 文件分别邮寄给我。我的邮件 ID 是 firunscall@gmail.com
【解决方案2】:

我使用了这个解决方案:

bw = im2;
bw = sum((1-im2).^2, 3) > .5; 
%bw = min( bw, [], 3 ) < 50 ; % dark pixels - intensity lower than 50
[y x] = find( bw ); % note that find returns row-col coordinates.

mx = mean(x);
my = mean(y);
C = [ mean( (x-mx).^2 ),     mean( (x-mx).*(y-my) );...  
      mean( (x-mx).*(y-my) ) mean( (y-my).^2 ) ];
[V D] = eig( C );

quiver( mx([1 1]), my([1 1]), (V(1,:)*D), (V(2,:)*D), .05 );
[~,mxi] = max(diag(D)); % find major axis index: largest eigen-value
or = atan2( V(2,mxi), V(1,mxi) ) * 180/pi ; % convert to degrees for readability
rotate = imrotate( im2, or-180 );

axes(handles.axes2);

imshow( rotate );  
set(handles.text3, 'String',or-180);

【讨论】:

    猜你喜欢
    • 2018-03-25
    • 1970-01-01
    • 2023-03-31
    • 2012-07-02
    • 1970-01-01
    • 2014-10-02
    • 2018-04-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多