我不明白為什么轉換不起作用,輸出影像是:


所以閾值操作作業正常,但我似乎沒有注意到轉換程序中一定存在某種問題。提前致謝!
import cv2
import numpy as np
#read the image, do binary processing
im = cv2.imread('scanned.png')
grey = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
cv2.imshow('gray', grey)
#the pixels are reversed to white and black
# gray = cv2.bitwise_not(gray)
ret, thresh = cv2.threshold(grey, 0, 255, cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)
cv2.imshow('thresh', thresh)
#calculate the minimum border that contains rotated text
coords = np.column_stack(np.where(thresh > 0))
print(coords)
#unction gives the rectangle border containing the whole text area, and the rotation angle of this border is the same as that of the text in the figure
angle = cv2.minAreaRect(coords)[-1]
print(angle)
#adjust the angle
if angle < -45:
angle = -(90 angle)
else:
angle = -angle
#transformation
h, w = im.shape[:2]
center = (w//2, h//2)
print(angle)
M = cv2.getRotationMatrix2D(center, angle, 1.0)
rotated = cv2.warpAffine(im, M, (w, h), flags=cv2.INTER_CUBIC, borderMode=cv2.BORDER_REPLICATE)
cv2.putText(rotated, 'Angle: {:.2f} degrees'.format(angle), (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2)
print('[INFO] angle :{:.3f}'.format(angle))
cv2.imshow('Input', im)
cv2.imshow('Rotated', rotated)
cv2.waitKey(0)
cv2.destroyAllWindows()
uj5u.com熱心網友回復:
應用 5x5 形態學膨脹會產生大斑點,可以非常精確地找到其方向。嘗試使用最大的。

轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/386693.html
上一篇:Android 常見記憶體泄漏總結、避免踩坑、提供解決方案。
下一篇:將枕頭影像轉為灰度的伽馬校正
