Python中的Radon变换

6

这里是一个虚拟代码:

def radon(img):
    theta = np.linspace(-90., 90., 180, endpoint=False)
    sinogram = skimage.transform.radon(img, theta=theta, circle=True)
    return sinogram
# end def

我希望能够在不使用skimage的情况下获取此代码输出的正弦图。但是,我无法在Python中找到任何实现。您能否提供一个仅使用OpenCV、numpy或其他轻量级库的实现? 编辑:我需要这个来获取图像的主导角度。我正在尝试在OCR系统的字符分割之前修复倾斜。以下是示例:

enter image description here

左侧是输入,右侧是期望的输出。
编辑2:如果您能提供任何其他获得此输出的方法,也会有所帮助。
编辑3:一些示例图像: https://drive.google.com/open?id=0B2MwGW-_t275Q2Nxb3k3TGg4N1U

你想从正弦图中检测出什么? - zindarod
我需要获取图像的主导角度。我正在尝试在OCR系统的字符分割之前修正倾斜。 - Dipu
发布一张图片和可能的期望输出。 - zindarod
1
伙计,发原始图片让我们可以测试一些东西,不要发这个缩放/合并的版本。 - zindarod
我已经添加了一些示例图片。如果您需要更多,请告诉我。 - Dipu
1个回答

9

嗯,我曾经遇到过类似的问题。在花费了一些时间谷歌搜索后,我找到了一个解决方法,并且它对我有效。希望它能帮到你。

import numpy as np
import cv2

from skimage.transform import radon


filename = 'your_filename'
# Load file, converting to grayscale
img = cv2.imread(filename)
I = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
h, w = I.shape
# If the resolution is high, resize the image to reduce processing time.
if (w > 640):
    I = cv2.resize(I, (640, int((h / w) * 640)))
I = I - np.mean(I)  # Demean; make the brightness extend above and below zero
# Do the radon transform
sinogram = radon(I)
# Find the RMS value of each row and find "busiest" rotation,
# where the transform is lined up perfectly with the alternating dark
# text and white lines
r = np.array([np.sqrt(np.mean(np.abs(line) ** 2)) for line in sinogram.transpose()])
rotation = np.argmax(r)
print('Rotation: {:.2f} degrees'.format(90 - rotation))

# Rotate and save with the original resolution
M = cv2.getRotationMatrix2D((w/2, h/2), 90 - rotation, 1)
dst = cv2.warpAffine(img, M, (w, h))
cv2.imwrite('rotated.jpg', dst)

测试:
原始图像:
输入图片描述

旋转后的图像: (旋转角度为-9°)
输入图片描述

致谢:
使用Radon变换检测文本页面图像的旋转和行间距

问题是在旋转图像后,会出现一些黑边。就您的情况而言,我认为这不会影响OCR处理。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接