[英]How to extract ID document region of interest from image with OpenCV?
I am trying to segment this image because I need only to obtain the document.我正在尝试分割此图像,因为我只需要获取文档。
Applying some filters I got this result:应用一些过滤器我得到了这个结果:
I am trying to get the outline of the white rectangle but I get this result:我正在尝试获取白色矩形的轮廓,但得到的结果是:
Anyone have any idea how to do better?任何人都知道如何做得更好?
this is my code:/这是我的代码:/
import cv2
image = cv2.imread('roberto.jpg')
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
_, binary = cv2.threshold(gray, 225, 255, cv2.THRESH_BINARY_INV)
cv2.imshow('test', binary)
cv2.waitKey(0)
contours, hierarchy = cv2.findContours(binary, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)[-2:]
idx = 0
for cnt in contours:
idx += 1
x,y,w,h = cv2.boundingRect(cnt)
roi=binary[y:y+h,x:x+w]
cv2.rectangle(image,(x,y),(x+w,y+h),(200,0,0),2)
cv2.imshow('img',image)
cv2.waitKey(0)
You're almost there, you just need to obtain the x,y,w,h
bounding rectangle coordinates using cv2.boundingRect
then you can extract/save the ROI using Numpy slicing.你几乎就在那里,你只需要使用
cv2.boundingRect
获取x,y,w,h
边界矩形坐标,然后你可以使用 Numpy 切片提取/保存 ROI。 Here's the result这是结果
import cv2
# Load image, grayscale, threshold
image = cv2.imread('1.jpg')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray, (3,3), 0)
thresh = cv2.threshold(gray, 225, 255, cv2.THRESH_BINARY_INV)[1]
# Get bounding box and extract ROI
x,y,w,h = cv2.boundingRect(thresh)
ROI = image[y:y+h, x:x+w]
cv2.imshow('thresh', thresh)
cv2.imshow('ROI', ROI)
cv2.waitKey()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.