这是我得到的收据图像,我已使用matplotlib对其进行了绘制,如果您看到该图像,则其中的文本不直。我该如何消除偏斜并进行修复?
from skimage import io
import cv2
# x1, y1, x2, y2, x3, y3, x4, y4
bbox_coords = [[20, 68], [336, 68], [336, 100], [20, 100]]
image = io.imread('https://i.ibb.co/3WCsVBc/test.jpg')
gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
fig, ax = plt.subplots(figsize=(20, 20))
ax.imshow(gray, cmap='Greys_r')
# for plotting bounding box uncomment the two lines below
#rect = Polygon(bbox_coords, fill=False, linewidth=1, edgecolor='r')
#ax.add_patch(rect)
plt.show()
print(gray.shape)
(847, 486)
我认为,如果我们想先倾斜,就必须找到边缘,因此我尝试使用canny算法找到边缘,然后获得如下轮廓。
from skimage import filters, feature, measure
def edge_detector(image):
image = filters.gaussian(image, 2, mode='reflect')
edges = feature.canny(image)
contours = measure.find_contours(edges, 0.8)
return edges, contours
fig, ax = plt.subplots(figsize=(20, 20))
ax.imshow(gray, cmap='Greys_r');
gray_image, contours = edge_detector(gray)
for n, contour in enumerate(contours):
ax.plot(contour[:, 1], contour[:, 0], linewidth=2)
从上面的代码中获得的边缘是每个文本的边缘,但这不是我所需要的。我需要正确的收据边缘吗?
我还需要一种方法来使图像倾斜(即拉直图像)后获得新的边界框坐标吗?
如果有人解决过类似的问题,请帮帮我吗?谢谢。
python大神给出的解决方案
这是Projection Profile Method的修改后的实现,用于校正偏斜的图像,如Projection profile based skew estimation algorithm for JBIG
compressed images中所述。获得二进制图像后,其想法是将图像旋转各种角度并在每次迭代中生成像素的直方图。要确定偏斜角,我们比较峰之间的最大差异,并使用此偏斜角旋转图像以校正偏斜。可以通过delta
值来调整要确定的峰数量,Δ越低,将检查更多的峰,但需要权衡取舍,因为此过程将花费更长的时间。
之前->
之后
码
import cv2
import numpy as np
from scipy.ndimage import interpolation as inter
def correct_skew(image, delta=.1, limit=5):
def determine_score(arr, angle):
data = inter.rotate(arr, angle, reshape=False, order=0)
histogram = np.sum(data, axis=1)
score = np.sum((histogram[1:] - histogram[:-1]) ** 2)
return histogram, score
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blur = cv2.medianBlur(gray, 3)
thresh = cv2.threshold(blur, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]
scores = []
angles = np.arange(-limit, limit + delta, delta)
for angle in angles:
histogram, score = determine_score(thresh, angle)
scores.append(score)
best_angle = angles[scores.index(max(scores))]
(h, w) = image.shape[:2]
center = (w // 2, h // 2)
M = cv2.getRotationMatrix2D(center, best_angle, 1.0)
rotated = cv2.warpAffine(image, M, (w, h), flags=cv2.INTER_CUBIC, \
borderMode=cv2.BORDER_REPLICATE)
return best_angle, rotated
if __name__ == '__main__':
image = cv2.imread('1.jpg')
angle, rotated = correct_skew(image)
print(angle)
cv2.imshow('rotated', rotated)
cv2.imwrite('rotated.png', rotated)
cv2.waitKey()
注意:另请参阅rotated skewed image to upright position的另一种方法
如何在Matplotlib条形图后面绘制网格线 - pythonx = ['01-02', '02-02', '03-02', '04-02', '05-02'] y = [2, 2, 3, 7, 2] fig, ax = plt.subplots(1, 1) ax.bar(range(len(y)), y, width=…
子条件的python条件覆盖 - python我试图找到一个python代码覆盖率工具,该工具可以衡量语句中是否包含子表达式:例如,我想看看下面的示例是否涵盖了condition1 / condition2 / condtion3?if condition1 or condition2 or condition3: x = true_value python大神给出的解决方案 对此的唯一合理答案是:当前…
USB设备发行 - python我目前正在使用PyUSB。由于我不熟悉USB,所以我不知道如何执行以下操作。我已经从Python PyUSB成功连接到我的USB设备硬件。在代码中,我需要重置USB设备硬件。通过向硬件发送命令来完成。现在,在硬件重置后,我想从Python PyUSB释放当前的USB设备。然后,我想在重置后将其重新连接到USB设备硬件。请让我知道,如何释放USB设备连接和接口…
Python-熊猫描述了抛出错误:无法散列的类型“ dict” - python更新:我正在使用“ Socrata开源API”中的一些示例代码。我在代码中注意到以下注释:# First 2000 results, returned as JSON from API / converted to Python # list of dictionaries by sodapy. 我不熟悉JSON。我已经下载了一个数据集,并创建了一个包含大量…
对于DataFrame的每一行,在给定条件的情况下获取第一列的索引到新列中 - python这是我的数据框的一部分。data = [ ['1245', np.nan, np.nan, 1.0, 1.0, ''], ['1246', np.nan, 1.0, 1.0, 1.0, ''], ['1247', 1.0, 1.0, 1.0, 1.0, …