试图通过坐标列表保存到一个数组来修剪长在几个地方我的形象后的裁剪区域的字母变得非常模糊,我想不通为什么。
原始图像看起来像
裁剪后的图像看起来像
在问题的代码如下:
import numpy as np
import cv2
im2 = cv2.imread('1.jpg')
im = im2.copy()
gray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray,(5,5),0)
thresh = cv2.adaptiveThreshold(blur,255,1,1,11,2)
contours,hierarchy = cv2.findContours(thresh,cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)
squares = []
for cnt in contours:
if cv2.contourArea(cnt)>50:
[x,y,w,h] = cv2.boundingRect(cnt)
if h>28 and h<34:
rect = (cv2.rectangle(im,(x,y),(x+w,y+h),(255,255,255),3))
squares.append(cv2.boundingRect(cnt))
cv2.imwrite('norm1.jpg',im)
crop_img = [[[255, 255, 255] for x in xrange(377)] for x in xrange(377) ]
for s in squares:
x = s[0]
y = s[1]
w = s[2]
h = s[3]
img = im[y:y+h,x:x+w]
for col in range(y,y+h):
for row in range(x,x+w):
if img[col - y][row - x].tolist() == [0,0,0]:
crop_img[col][row] = [0,0,0]
cv2.imwrite("cropped.jpg", np.array(crop_img))
任何帮助将不胜感激!