python - 如何在数字所在的确切位置裁剪图像?
问题描述
我有我的代码原型:
import cv2
import numpy as np
img = cv2.imread('/home/follia/Pictures/scan.jpg')
h, w, k = img.shape
M = cv2.getRotationMatrix2D((w / 2, h / 2), 15.5, 1)
img = cv2.warpAffine(img, M, (w, h))
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
edges = cv2.Canny(gray, 100, 200, apertureSize=3)
lines = cv2.HoughLinesP(edges, 1, np.pi / 180, 80)
for x in range(0, len(lines)):
for x1,y1,x2,y2 in lines[x]:
cv2.line(img,(x1,y1),(x2,y2),(0,0,255),2)
cv2.imshow("origin", img)
cv2.waitKey(0)
你能帮我吗,我怎么能剪掉这个位置?然后,我如何识别数字并将其从图像提取到文本?
解决方案
尝试这个:
该方案的基本思想是,执行后得到图像的轮廓,threshold()
并检测轮廓中最大的轮廓。
输入:
代码:
import cv2
image = cv2.imread("test.jpg", 1)
h, w, k = image.shape
M = cv2.getRotationMatrix2D((w / 2, h / 2), 15.5, 1)
image = cv2.warpAffine(image, M, (w, h), cv2.INTER_LINEAR, cv2.BORDER_CONSTANT, borderValue=(255, 255, 255))
img = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
threshold = 80
cv2.threshold(img,threshold,255,cv2.THRESH_BINARY,img)
cv2.bitwise_not(img,img)
cv2.imshow("Result", img)
cv2.waitKey(0)
im2, contours, hier = cv2.findContours(img, cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE)
if len(contours) != 0:
#find the biggest area
c = max(contours, key = cv2.contourArea)
x,y,w,h = cv2.boundingRect(c)
cv2.rectangle(image,(x,y),(x+w,y+h),(0,255,0),2)
crop_img = image[y:y + h, x:x + w]
cv2.imshow("Result", crop_img)
cv2.waitKey(0)
cv2.imshow("Result", image)
cv2.waitKey(0)
输出:
推荐阅读
- excel - 登录后获取IE,url改变
- ios - watchOS 5 开发套件的 SIP 库
- javascript - 基于角色的重定向
- reactjs - 致命:身份验证失败
不要求凭据 - node.js - MSB6006:node.exe 在 VS2019 中以代码 1 退出
- python-3.x - 如何在windows上的anaconda中安装opencv
- mongodb - 规划器返回错误:无法在连接中找到 $geoNear 查询的索引
- javascript - 如何为我的打字稿类实例创建一个共享服务来与之交谈?
- mongodb - 查询以排除空值,但我得到了那些
- python - 在 Selenium 中可视化焦点和交互