[英]Detecting Credit Card bounding box from images having thumb holding it
我想检测图像中信用卡周围矩形的边界框。 信用卡将被握在手中,因此任务变得有点复杂,因为我得到的矩形是不完整的。 我使用了 HoughLinesP,然后扩展检测到的线以增加它们的长度。 然后我正在检测面积最大的矩形。
直到现在我才发布我的代码。 请审查它-
import cv2
import math
import numpy as np
from scipy.spatial import distance
from skimage.transform import hough_line, hough_line_peaks
img = cv2.imread('car.jpg')
nimg = np.zeros([img.shape[0],img.shape[1],3],dtype=np.uint8)
nimg1 = np.zeros([img.shape[0],img.shape[1],3],dtype=np.uint8)
nimg2 = np.zeros([img.shape[0],img.shape[1],3],dtype=np.uint8)
nimg.fill(255)
nimg1.fill(255)
nimg2.fill(255)
print(img.shape[0])
#img = cv2.resize(img, (img.shape[0]//3,img.shape[1]//3))
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray, (5,5),0)
dst = cv2.Canny(blur, 200, 50, None, 3)
dst1= cv2.Canny(blur, 200, 50, None, 3)
#cv2.imshow('sds',dst)
cn = cv2.dilate(dst, cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (11, 11)))
cn = cv2.erode(cn, cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (15, 15)))
cn = cv2.dilate(cn, cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5, 5)))
dst -= cn
dst[dst < 127] = 0
cv2.imshow('sds',dst)
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (1,1))
dilate = cv2.dilate(dst, kernel, iterations=1)
cv2.imshow('sdsss',dilate)
lines = cv2.HoughLinesP(dilate, 0.5, np.pi/720, 30, minLineLength=20, maxLineGap=10)
# Draw lines on the image
max = 0
if lines is not None:
for i in range(0, len(lines)):
l = lines[i][0]
dist= distance.euclidean((l[0],l[1]),(l[2],l[3]))
if dist>max:
max = dist
linesP = []
if lines is not None:
for i in range(0, len(lines)):
l = lines[i][0]
print(lines)
dist = distance.euclidean((l[0], l[1]), (l[2], l[3]))
if (dist >= max // 6):
linesP.append(l)
if lines is not None:
for i in range(0, len(linesP)):
l = linesP[i]
dist = distance.euclidean((l[0], l[1]), (l[2], l[3]))
C=[200,200]
D=[200,200]
A=(l[0], l[1])
B=(l[2], l[3])
#print(l)
lenAB = math.sqrt(math.pow(A[0] - B[0], 2.0) + math.pow(A[1] - B[1], 2.0))
C[0] = int(B[0] + (B[0] - A[0]) / lenAB * 220)
D[0] = int(A[0] + (A[0] - B[0]) / lenAB * 220)
C[1] = int(B[1] + (B[1] - A[1]) / lenAB * 220)
D[1] = int(A[1] + (A[1] - B[1]) / lenAB * 220)
cv2.line(nimg, tuple(D), tuple(C), (0,0,0),2, 1)
#print(lines)
gray = cv2.cvtColor(nimg, cv2.COLOR_BGR2GRAY)
gray = cv2.bilateralFilter(gray, 11, 17, 17)
thresh = cv2.adaptiveThreshold(gray,255,1,1,11,2)
kernel = np.ones((5,5),np.uint8)
erosion = cv2.erode(gray,kernel,iterations = 1)
kernel = np.ones((4,4),np.uint8)
dilation = cv2.dilate(erosion,kernel,iterations = 1)
edged = cv2.Canny(dilation, 50, 50)
nimg = cv2.bitwise_not(edged)
cv2.imshow('edged',thresh)
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
rects = [cv2.boundingRect(cnt) for cnt in contours]
ma = 0
for c in contours:
x,y,w,h = cv2.boundingRect(c)
area = w * h
if (area > ma and (x != 0 and y != 0) and (x + w != img.shape[1] and y + h != img.shape[0])):
ma = area
mx, my, mw, mh = x, y, w, h
rect = cv2.minAreaRect(c)
box = cv2.boxPoints(rect)
box = np.int0(box)
cv2.drawContours(img,[box],0,(0,255,0),thickness=cv2.FILLED)
cv2.imshow('cropped', nimg)
cv2.imshow('abcd',img)
cv2.waitKey(0)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.