如何在python中使用opencv校正图像? [英] How to deskew of an image using opencv in python?

查看:514
本文介绍了如何在python中使用opencv校正图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的图片将说明我想要的.

The following picture will illustrate what i want.

图像包含4个参考矩形,这是图像去偏斜的基础.假设,图像包含一些其他信息,例如文本,圆形和矩形.现在,我想编写一个脚本来基于四个矩形拉直图像.我得到的图像应该拉直.这样,我就可以在校正偏斜后提取一些信息. 我正在使用OpenCV python,请告诉我一种实现此目的的方法. 请显示一些代码作为OpenCV Python的示例.在此处输入图像说明,之后必须就像在此处输入图片描述

Image contain 4 reference rectangle which is base for image deskewing. Assume, Image contain some other information like text, circle and rectangle. Now, I want to write a script to straighten the image based on four rectangle.my resultant image should be straighten. So i can extract some information after deskewing it. I am using OpenCV python, please tell me a way to accomplish this. Kindly show some code as examples of OpenCV Python.enter image description here after that it must be like enter image description here

推荐答案

我终于解决了.

contours, hierarchy = cv2.findContours(img_dilate.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

points=[]
i=0
for contour in contours:
    area = cv2.contourArea(contour)
    if area > 10 and area < 800:
        x,y,w,h = cv2.boundingRect(contour)
        cv2.rectangle(img_dilate,(x,y),(x+w,y+h),(0,255,0),2)
        center = (int(x),int(y))
        position = (center[0], center[1])
        points.append(position)
        print position,i
        text_color = (255,0,255)
        cv2.putText(resized,str(i+1), position, cv2.FONT_HERSHEY_SIMPLEX, 0.5, text_color, 2)
        i=i+1
pnts = np.array(points)
rect = cv2.minAreaRect(pnts)
box = cv2.cv.BoxPoints(rect)
box = np.int0(box)
cv2.drawContours(resized,[box],0,(0,255,0),2)

root_mat = cv2.getRotationMatrix2D(rect[0], angle , 1)
rotated = cv2.warpAffine(resized, root_mat, dim, flags=cv2.INTER_CUBIC)
cv2.getRectSubPix(rotated, dim, center) 

这篇关于如何在python中使用opencv校正图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆