银行卡数字排序剪切提取
import cv2
import numpy as np
def cv_show(name,img):
cv2.imshow(name,img)
cv2.waitKey(0)
cv2.destroyAllWindows()
src = cv2.imread("E:/ocr_a_reference.png",cv2.IMREAD_COLOR)
if src is None:
print("could not load the picture...")
exit()
cv_show("src",src)
gray = cv2.cvtColor(src,cv2.COLOR_BGR2GRAY)
if gray is None:
print('could not translate to gray')
exit()
cv_show('gray',gray)
thresh = cv2.threshold(gray,127,255,cv2.THRESH_BINARY_INV)[1]
if thresh is None:
print('the thresh load is fail..')
exit()
cv_show('thresh',thresh)
contours = cv2.findContours(thresh,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)[0]
if contours is None:
print('The contours is Null...')
exit()
dst = src.copy()
cv2.drawContours(dst,contours,-1,(0,255,0),2)
cv_show('dst',dst)
dst = cv2.cvtColor(dst,cv2.COLOR_BGR2GRAY)
loc = gray.copy()
boundingBoxes = [cv2.boundingRect(c) for c in contours]
(contours, boundingBoxes) = zip(*sorted(zip(contours, boundingBoxes),
key=lambda b: b[1]))
print(len(contours))
digi={}
for (i,c) in enumerate(contours):
(x,y,w,h) = cv2.boundingRect(c)
group = gray[y-5:y+h+5,x-5:x+w+5]
roi = cv2.resize(group, (57, 88))
print(roi.shape)
cv_show("group",group)
digi[i] = roi
print(len(digi))
去除噪点,删除小面积
import cv2
import numpy as np
image = cv2.imread("D:/2.jpg")
img = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
ret,thresh = cv2.threshold(img,30,255,cv2.THRESH_BINARY)
contours= cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)[1]
src = image.copy()
src1 = image.copy()
for i in range(len(contours)):
area = cv2.contourArea(contours[i])
if area<100:
continue
cv2.drawContours(src,[contours[i]],-1,(255,0,0),2)
cv2.drawContours(src1,contours,-1,(0,255,0),2)
cv2.imshow("src",src)
cv2.imshow("src1",src1)
cv2.waitKey(0)
cv2.destroyAllWindows()
找出轮廓并画出轮廓
import cv2
import numpy as np
image = cv2.imread("D:/2.jpg")
gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
ret,thresh =cv2.threshold(gray,50,255,cv2.THRESH_BINARY)
cv2.imshow("thresh",thresh)
binary,contours,hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
draw_img = image.copy()
res = cv2.drawContours(draw_img,contours,-1,(0,0,255),1)
cv2.imshow("res",res)
cv2.waitKey(0)
cv2.destroyAllWindows()
找出轮廓去除小面积
import cv2
import numpy as np
camera= cv2.VideoCapture("111.mp4")
cv2.namedWindow("Video",0);
cv2.resizeWindow("Video", 800, 600);
cv2.namedWindow("BW-Video",0);
cv2.resizeWindow("BW-Video", 640, 480);
cv2.namedWindow("DI-Video",0);
cv2.resizeWindow("DI-Video", 640, 480);
firstframe=None
while (camera.isOpened()):
ret,frame = camera.read()
if not ret:
break
gray=cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
gray=cv2.GaussianBlur(gray,(21,21),0)
if firstframe is None:
firstframe=gray
continue
frameDelta = cv2.absdiff(firstframe,gray)
thresh = cv2.threshold(frameDelta, 65, 255, cv2.THRESH_BINARY)[1]
thresh = cv2.dilate(thresh, None, iterations=2)
cnts, hierarchy, rr = cv2.findContours(thresh.copy(),cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
for c in hierarchy:
if cv2.contourArea(c) <= 8000:
continue
x,y,w,h = cv2.boundingRect(c)
frame=cv2.rectangle(frame,(x,y),(x+w,y+h),(0,0,255),3)
cv2.imshow(u"Video", frame)
cv2.imshow(u"BW-Video", thresh)
cv2.imshow(u"DI-Video", frameDelta)
key = cv2.waitKey(1)
if key == ord("q"):
break
camera.release()
cv2.destroyAllWindows()
import cv2 as cv
import numpy as np
src = cv.imread(r"D:\test5.jpg")
ROI = np.zeros(src.shape, np.uint8)
gray = cv.cvtColor(src, cv.COLOR_BGR2GRAY)
ret, binary = cv.threshold(gray,
0, 255,
cv.THRESH_BINARY_INV | cv.THRESH_TRIANGLE)
out_binary, contours, hierarchy = cv.findContours(binary,
cv.RETR_EXTERNAL,
cv.CHAIN_APPROX_SIMPLE)
for cnt in range(len(contours)):
epsilon = 0.01 * cv.arcLength(contours[cnt], True)
approx = cv.approxPolyDP(contours[cnt], epsilon, True)
mm = cv.moments(contours[cnt])
if mm['m00'] != 0:
cx = int(mm['m10'] / mm['m00'])
cy = int(mm['m01'] / mm['m00'])
color = src[cy][cx]
color_str = "(" + str(color[0]) + ", " + str(color[1]) + ", " + str(color[2]) + ")"
p = cv.arcLength(contours[cnt], True)
area = cv.contourArea(contours[cnt])
corners = len(approx)
if corners == 3 and (color[2]>=150 or color[0]>=150) and area>1000:
cv.drawContours(ROI, contours, cnt, (255, 255, 255), -1)
imgroi = ROI & src
cv.imshow("ROI", imgroi)
cv.imwrite(r"D:\ROI.jpg")
if corners >= 10 and (color[2]>=150 or color[0]>=150) and area>1000:
cv.drawContours(ROI, contours, cnt, (255, 255, 255), -1)
imgroi = ROI & src
cv.imshow("ROI",imgroi)
cv.imwrite(r"D:\ROI.jpg")
cv.waitKey(0)
cv.destroyAllWindows()
KNN跟踪(效果较好)
import numpy as np
import cv2
def detect_video(video):
camera = cv2.VideoCapture(video)
history = 500
bs = cv2.createBackgroundSubtractorKNN(detectShadows=True)
bs.setHistory(history)
frames = 0
while True:
res, frame = camera.read()
if not res:
break
fg_mask = bs.apply(frame)
if frames < history:
frames += 1
continue
th = cv2.threshold(fg_mask.copy(), 244, 255, cv2.THRESH_BINARY)[1]
th = cv2.erode(th, cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3, 3)), iterations=2)
dilated = cv2.dilate(th, cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (8, 3)), iterations=2)
image, contours, hier = cv2.findContours(dilated, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
tempjs = 0
for c in contours:
x, y, w, h = cv2.boundingRect(c)
area = cv2.contourArea(c)
if 500 < area:
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
tempjs = tempjs + 1
print(tempjs)
cv2.imshow("detection", frame)
cv2.imshow("back", dilated)
if cv2.waitKey(110) & 0xff == 27:
break
camera.release()
if __name__ == '__main__':
detect_video("E:/test.avi")