我想裁剪出图像中非矩形、非多边形的部分,其中包含文字,然后我将使用OCR进行处理
我需要得到尽可能精确的裁剪结果,因此我不能使用近似多边形或矩形的方法
以下是输入图像和代码:
import cv2
import numpy as np
import imutils
shape = cv2.imread("testImg.jpg")
resized = imutils.resize(shape, width=600)
gray = cv2.cvtColor(resized,cv2.COLOR_BGR2GRAY)
th3 = cv2.threshold(gray, 100, 255, cv2.THRESH_BINARY)[1]
cv2.imshow("th3",th3)
#find contours of thresh
contours, hierarchy = cv2.findContours(th3, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
print("Number of Contours found = " + str(len(contours)))
#a black screen
out = np.zeros_like(resized)
#copy of thresh
copy=th3.copy()
#find largest contour area and draw it
cntArea = []
for c in contours:
cntArea.append(cv2.contourArea(c))
maxArea = max(cntArea)
maxIndex=cntArea.index(max(cntArea))
maxCnts=contours[maxIndex]
cv2.drawContours(out, maxCnts, -1, (0,255,0), 2)
cv2.imshow("out",out)
cv2.waitKey(0)
cv2.destroyAllWindows
我尝试过清除背景轮廓,但由于我的OCR检测方法,它无法工作(下面的图像无法通过我的OCR代码)
final_img = mask * original_img
相关问题 更多 >
编程相关推荐