如何在数字所在的确切位置裁剪图像?
How could I crop image in exact location where numbers are situated?
我有我的代码原型:
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)
原图:
它 return 这张图片:
而且我需要裁剪此图像并仅显示数字:
能不能帮我看看,这个位置怎么剪啊?
然后,我如何识别数字并将其从图像提取为文本?
试试这个:
这个解决方案的基本思路是,执行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)
输出:
我有我的代码原型:
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)
输出: