python中的Radon变换

时间:2017-09-06 21:18:06

标签: python opencv image-processing

这是一个虚拟代码:

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

1 个答案:

答案 0 :(得分:1)

嗯,我遇到了类似的问题。花了一些时间在问题上进行了搜索之后,我找到了一个对我有用的解决方案。希望对您有所帮助。

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, 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,(cols,rows))
cv2.imwrite('rotated.jpg', dst)

测试:
原始图片:
enter image description here

旋转图像:(旋转角度为-9°)
enter image description here

得分:
Detecting rotation and line spacing of image of page of text using Radon transform

问题是旋转图像后,您会得到一些黑色边框。对于您的情况,我认为它不会影响OCR处理。