import cv2
import numpy as np
np.set_printoptions(suppress=True)
img=cv2.imread('/home/gg/Pictures/jt.jpg')
print('img:',type(img),img.shape,img.dtype)
cv2.imshow('img',img)
hsv=cv2.cvtColor(img,cv2.COLOR_BGR2HSV)
cv2.imshow('hsv',hsv)
blue_lower=np.array([100,50,50])
blue_upper=np.array([124,255,255])
mask=cv2.inRange(hsv,blue_lower,blue_upper)
print('mask',type(mask),mask.shape)
cv2.imshow('mask',mask)
blurred=cv2.blur(mask,(9,9))
cv2.imshow('blurred',blurred)
ret,binary=cv2.threshold(blurred,127,255,cv2.THRESH_BINARY)
cv2.imshow('blurred binary',binary)
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (21, 7))
closed = cv2.morphologyEx(binary, cv2.MORPH_CLOSE, kernel)
cv2.imshow('closed',closed)
erode=cv2.erode(closed,None,iterations=4)
cv2.imshow('erode',erode)
dilate=cv2.dilate(erode,None,iterations=4)
cv2.imshow('dilate',dilate)
contours, hierarchy = cv2.findContours(dilate.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
image = cv2.findContours(dilate.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
print('number',len(contours))
i=0
res=img.copy()
for con in contours:
rect=cv2.minAreaRect(con)
box=np.int0(cv2.cv.BoxPoints(rect))
cv2.drawContours(res,[box],-1,(0,0,255),2)
print([box])
h1=max([box][0][0][1],[box][0][1][1],[box][0][2][1],[box][0][3][1])
h2=min([box][0][0][1],[box][0][1][1],[box][0][2][1],[box][0][3][1])
l1=max([box][0][0][0],[box][0][1][0],[box][0][2][0],[box][0][3][0])
l2=min([box][0][0][0],[box][0][1][0],[box][0][2][0],[box][0][3][0])
print('h1',h1)
print('h2',h2)
print('l1',l1)
print('l2',l2)
if h1-h2>0 and l1-l2>0:
temp=img[h2:h1,l2:l1]
i=i+1
cv2.imshow('sign'+str(i),temp)
cv2.imshow('res',res)
cv2.waitKey(0)
cv2.destroyAllWindows()
搬的网上的一个代码
背景不复杂的时候识别率高,复杂情况下,还是会有误识别,占坑,日后修改。
import cv2
filename = '/home/gg/Pictures/cxy.jpg'
def detect(filename):
face_cascade = cv2.CascadeClassifier('./home/gg/PycharmProjects/untitled/venv/haarcascade_frontalface_default.xml')
# face_cascade.load('/home/gg/PycharmProjects/untitled/venv/haarcascade_frontalface_default.xml')
img = cv2.imread(filename)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
face = face_cascade.detectMultiScale(gray, 1.3, 5)
for(x, y, w, h) in face:
img = cv2.rectangle(img, (x, y), (x+w, y+h), (0, 255, 0), 2)
cv2.namedWindow('detected')
cv2.imshow('detected', img)
cv2.imwrite('./233.jpg', img)
cv2.waitKey(0)
detect(filename)