简体   繁体   中英

Opencv: Remove background and draw bounding box around the ID Card

I have some images from Google, where I want to remove the background from the images and draw the bounding box around the ID cards.

Here is the input image:

输入图像

Here is my code:

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

image = cv2.imread('/content/f6644ae09eb9acfc6a71f01115f917f9.jpg')
result = image.copy()
gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]

cnts = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
for c in cnts:
        cv2.drawContours(thresh, [c], -1, (255,255,255), 5)
plt.imshow(thresh,'gray')

This code is used to threshold the image. The output is like this:

阈值图像

How can I crop only the ID card and remove the background from the image?

You can crop out the ID in the following way. See inline comments for explanation

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

gray = cv.cvtColor(img,cv.COLOR_BGR2GRAY)

## use cv.THRESH_BINARY instead of ..._INV
## this will set the background black instead of white
## and the foreground to white instead of black
thresh = cv.threshold(gray, 0, 255, cv.THRESH_BINARY + cv.THRESH_OTSU)[1]
##                                  ^^^^^^^^^^^^^^^^^

cnts,_ = cv.findContours(thresh, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE)

## sort the contours from largest to smallest.
## the largest will contain your image
cnts = sorted(cnts, key=lambda x:-len(x))

## place a rectangle around the largest contour
rect = cv.boundingRect(cnts[0])
x = rect[1] # column coordinate of top left corner of rectangle
y = rect[0] # row coordinate of top left corner of rectangle
width = rect[2] # width of rectangle
height = rect[3] # height of rectangle
crop = img[y:y+height, x:x+width]
plt.imshow(crop)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM