python - 裁剪功能后字母模糊/模糊

标签 python opencv crop

尝试通过将坐标列表保存到数组来在多个位置裁剪我的图像后,裁剪区域中的字母变得非常模糊,我无法弄清楚原因。

原图看起来像

1

裁剪后的图像看起来像

2

题中代码如下:

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))

如有任何帮助,我们将不胜感激!

最佳答案

import numpy as np
import cv2
import matplotlib.pyplot as plt

im2 = cv2.imread('norm1_zps89266edb.jpg')
im = im2.copy()

gray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray,(5,5),0)
ret3,thresh = cv2.threshold(blur,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)

#we ony want the external contours
contours,hierarchy = cv2.findContours(thresh.copy(),cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE) 
#extract the countours with area > 50
squares = [cnt for cnt in contours if cv2.contourArea(cnt) > 50]

#mask array with the same shape as img (but only 1 channel)
mask = np.zeros((im.shape[0], im.shape[1]))
#draw the contours filled with 255 values. 
cv2.drawContours(mask,squares,-1,255,-1)

newImage = np.where(mask==255, thresh, 255)

plt.imshow(newImage)
plt.show()

cv2.imwrite("cropped.jpg", newImage)

输出:
http://i44.tinypic.com/8yak93.jpg

关于python - 裁剪功能后字母模糊/模糊,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21153466/

相关文章:

python - 如何让x轴标签适合图表的宽度?

python - Django select_related 不返回外表数据

android - 上传前从相机裁剪图像(Phonegap)

c++ - 如何在 OpenCV 中裁剪圆圈(通过 Hough 变换找到)​​?

python - 使用 Python 正则表达式拆分非重叠字符组(ORF 发现)

c++ - 当我尝试访问 CvMat 数据结构成员的第一个元素时,为什么会得到这个 ^@

opencv - 分配像素值的安全方法

python - OpenCV 相机校准的均方根太高

swift - 是否没有用于选择图像并从该图像中选择正方形裁剪的库?

Python:显式退出和简单地让执行到达文件末尾有什么区别?