(1)安装代码 pip install opencv-python

pip install opencv-python

(2)典型的几个功能

2.1 面部检测

面部检测,使用cv2.CascadeClassifier()

示例代码如下

import numpy as np
import cv2
 
face_cascade = cv2.CascadeClassifier('C:\\Users\\XXXXXXXX\\Anaconda3\\envs\\OpenCV\\Lib\\site-packages\\cv2\\data\\haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier('C:\\Users\\XXXXXXXX\\Anaconda3\\envs\\OpenCV\\Lib\\site-packages\\cv2\\data\\haarcascade_eye.xml')
 
img = cv2.imread('shutterstock_1055756639.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
 
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
for (x,y,w,h) in faces:
    img = cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
    roi_gray = gray[y:y+h, x:x+w]
    roi_color = img[y:y+h, x:x+w]
    eyes = eye_cascade.detectMultiScale(roi_gray)
    for (ex,ey,ew,eh) in eyes:
        cv2.rectangle(roi_color,(ex,ey),(ex+ew,ey+eh),(0,255,0),2)
 
cv2.imshow('img',img)
cv2.waitKey(0)
cv2.destroyAllWindows()

原图片
在这里插入图片描述
检测结果
在这里插入图片描述
有关具体的Tutorial 参考链接: link.

2.2 轮廓检测

轮廓检测,使用cv2.findContours()
代码第6行,[88]代表亮度的临界值,调整临界值可以改变轮廓的形状

import numpy as np
import cv2
 
im = cv2.imread('shutterstock_401797897.jpg')
imgray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
ret,thresh = cv2.threshold(imgray,88,255,0)
img, contours, hierarchy = cv2.findContours(thresh,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
 
img = cv2.drawContours(im, contours, -1, (0,0,255), 3)
 
cv2.imshow('im',im)
cv2.waitKey(0)
cv2.destroyAllWindows()

原图
在这里插入图片描述
检测出的轮廓(红色)
在这里插入图片描述
关具体的Tutorial 参考链接: link

2.3 Canny方法边缘检测

cv2.Canny() 调整第二个和第三个的参数,轮廓会发生变化

import cv2
import numpy as np
 
img = cv2.imread('py_back.jpg',0)
 
edges = cv2.Canny(img,100,200)
 
cv2.imshow('edges',edges)
cv2.waitKey(0)
cv2.destroyAllWindows()

原图
在这里插入图片描述
检测之后
在这里插入图片描述
关具体的Tutorial 参考链接: link

2.4 求图片的histogram

cv2.calcHist()
分别有R(红色),G(绿色),B(蓝色)三个频道,横坐标为亮度(0~255),纵坐标为像素。
histogram的可视化,要安装matplotlib,安装命令为

pip install matplotlib

安装之后示例代码

import cv2
import numpy as np
from matplotlib import pyplot as plt
 
img = cv2.imread('shutterstock_1055756639.jpg')
color = ('b','g','r')
for i,col in enumerate(color):
   histr = cv2.calcHist([img],[i],None,[256],[0,256])
   plt.plot(histr,color = col)
   plt.xlim([0,256])
plt.show()

原图
在这里插入图片描述
图为
在这里插入图片描述

2.5 检测圆

圆的检测用,cv2.HoughCircles()
把「param1=540」的值调小,可以检测出更多的圆

代码为

import cv2
import numpy as np
 
img_org = cv2.imread('shutterstock_1055756639.jpg')
imgray = cv2.cvtColor(img_org,cv2.COLOR_BGR2GRAY)
imgray = cv2.medianBlur(imgray,5)
 
circles = cv2.HoughCircles(imgray,cv2.HOUGH_GRADIENT,1,20,
                           param1=540,param2=30,minRadius=0,maxRadius=0)
 
circles = np.uint16(np.around(circles))
for i in circles[0,:]:
   # draw the outer circle
   cv2.circle(img_org,(i[0],i[1]),i[2],(0,255,0),2)
   # draw the center of the circle
   cv2.circle(img_org,(i[0],i[1]),2,(0,0,255),3)
 
cv2.imshow('detected circles',img_org)
cv2.waitKey(0)
cv2.destroyAllWindows()

原图
在这里插入图片描述
圆的检测(绿色为圆,红点为圆心)
在这里插入图片描述
关具体的Tutorial 参考链接: link

Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐