网站后台管理系统的重要技术指标,百度网页入口,慕课网电子商务网站开发,销售管理系统需求分析一 图像轮廓
具有相同颜色或强度的连续点的曲线。 图像轮廓的作用 可以用于图像分析 物体的识别与检测
注意
为了检测的准确性#xff0c;需要先对图像进行二值化或Canny操作。 画轮廓时会修改输入的图像。
轮廓查找的API
findContours(img,mode,ApproximationMode,...)…一 图像轮廓
具有相同颜色或强度的连续点的曲线。 图像轮廓的作用 可以用于图像分析 物体的识别与检测
注意
为了检测的准确性需要先对图像进行二值化或Canny操作。 画轮廓时会修改输入的图像。
轮廓查找的API
findContours(img,mode,ApproximationMode,...)
两个返回值contours和hierarchy
mode
RETR_EXTERNAL0,表示只检测外轮廓。
RETR_LIST1检测的轮廓不建立等级关系
RETR_CCOMP2,每层最多两级
RETR_TREE3,桉树形存储轮廓ApproximationMode
CHAIN_APPROX_NONE,保存所有轮廓上的点
CHAIN_APPROX_SIMPLE只保存角点import cv2
import numpy as npimgcv2.imread(./9615.jpg)
print(img.shape)#转变成单通道
graycv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
print(gray.shape)#二值化
ret,binarycv2.threshold(gray,150,255,cv2.THRESH_BINARY)#轮廓查找
contours,hierarchycv2.findContours(binary,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
print(contours)
cv2.imshow(img,img)
cv2.waitKey(0)二 绘制轮廓
如何绘制轮廓
drawContours(img,contours,contourIdx,color,thickness...)
contourIdx,-1表示绘制所有轮廓
color,颜色(0,0,255)
thickness,线宽-1是全部填充import cv2
import numpy as npimgcv2.imread(./9615.jpg)
print(img.shape)#转变成单通道
graycv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
print(gray.shape)#二值化
ret,binarycv2.threshold(gray,150,255,cv2.THRESH_BINARY)#轮廓查找
contours,hierarchycv2.findContours(binary,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
print(contours)#绘制轮廓
img1cv2.drawContours(img,contours,0,(0,0,255),5)#cv2.imshow(img,img)
cv2.imshow(img1,img1)
cv2.waitKey(0)三 轮廓的面积和周长
1 轮廓的面积
contourArea(contour)
contour轮廓2 轮廓的周长
arcLength(curve,closed)
curve:轮廓
closed:是否是闭合的轮廓import cv2
import numpy as npimgcv2.imread(./9615.jpg)
print(img.shape)#转变成单通道
graycv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
print(gray.shape)#二值化
ret,binarycv2.threshold(gray,150,255,cv2.THRESH_BINARY)#轮廓查找
contours,hierarchycv2.findContours(binary,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
print(contours)#绘制轮廓
img1cv2.drawContours(img,contours,0,(0,0,255),5)#计算面积
areacv2.contourArea(contours[0])
print(area%d%(area))# 计算周长
lencv2.arcLength(contours[0],True)
print(len%d%(len))
#cv2.imshow(img,img)
cv2.imshow(img1,img1)
cv2.waitKey(0)四 多边形逼近与凸包 1 多边形逼近
approxPolyDP(curve,epsilon,closed)
curve:轮廓
epsilon
closed:是否是闭合的轮廓2 凸包
convexHull(points,clockwise,...)
points:轮廓
clockwise:顺时针绘制轮廓查找
import cv2
import numpy as npdef drawShape(src,points):i0while ilen(points):if(ilen(points)-1):x, y points[i][0]x1, y1 points[0][0]cv2.line(src, (x, y), (x1, y1), (0, 0, 255), 1)else:x, y points[i][0]x1, y1 points[i 1][0]cv2.line(src, (x, y), (x1, y1), (0, 0, 255), 1)ii1imgcv2.imread(./palm.png)
#print(img.shape)#转变成单通道
graycv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
print(gray.shape)#二值化
ret,binarycv2.threshold(gray,150,255,cv2.THRESH_BINARY_INV)#轮廓查找
contours,hierarchycv2.findContours(binary,cv2.RETR_CCOMP,cv2.CHAIN_APPROX_NONE)
#print(contours)#绘制轮廓
img1cv2.drawContours(img,contours,-1,(0,255,0),5)e20approxcv2.approxPolyDP(contours[0],e,True)drawShape(img,approx)hullcv2.convexHull(contours[0])
drawShape(img1,hull)
#cv2.imshow(img,img)
cv2.imshow(img1,img1)
cv2.waitKey(0)凸包绘制 五 外接矩形
最小外接矩形 最大外接矩形 外接矩形 最小外接矩形
minAreaRect(points)
points:轮廓
返回值RotatedRect
RotatedRect
x,y
width,height
angle最大外接矩形
boundingRect(array)
array轮廓
返回值Rect import cv2
import numpy as npdef drawShape(src,points):i0while ilen(points):if(ilen(points)-1):x, y points[i][0]x1, y1 points[0][0]cv2.line(src, (x, y), (x1, y1), (0, 0, 255), 1)else:x, y points[i][0]x1, y1 points[i 1][0]cv2.line(src, (x, y), (x1, y1), (0, 0, 255), 1)ii1imgcv2.imread(./Hello.png)
#print(img.shape)#转变成单通道
graycv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
print(gray.shape)#二值化
ret,binarycv2.threshold(gray,150,255,cv2.THRESH_BINARY)
print(binary.shape)
#轮廓查找cv2.RETR_CCOMP CHAIN_APPROX_NONE
contours,hierarchycv2.findContours(binary,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
#print(contours)#绘制轮廓
#imgcv2.drawContours(img,contours,-1,(0,255,0),2)rcv2.minAreaRect(contours[1])
boxcv2.boxPoints(r)
boxnp.int32(box)cv2.drawContours(img,[box],0,(0,0,255),5)x,y,w,hcv2.boundingRect(contours[1])cv2.rectangle(img,(x,y),(xw,yh),(255,0,0),2)cv2.imshow(img,img)cv2.waitKey(0) 六 车辆统计
窗口的展示 图像/视频的加载 基本图形的绘制 车辆识别 基本图像运算与处理 形态学 轮廓查找
1 涉及到的知识点
加载视频 通过形态学识别车辆 对车辆进行统计 显示车辆统计信息
import cv2
import numpy as npcapcv2.VideoCapture(video.mp4)while True:ret,frame cap.read()if(retTrue):cv2.imshow(video,frame)keycv2.waitKey(1)if(key27)break
cap.release()
cv2.destroyAllWindows()形态学处理
import cv2
import numpy as npcapcv2.VideoCapture(video.mp4)bgsubmogcv2.bgsegm.createBackgroundSubtractorMOG()#形态学kernel
kernelcv2.getStructuringElement(cv2.MORPH_RECT,(5,5))while True:ret,frame cap.read()if(retTrue):#灰度cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)#去噪(高斯)blucv2.GaussianBlur(frame,(3,3),5)#去背影maskbgsubmog.apply(blu)#腐蚀erodecv2.erode(mask,kernel)#膨胀dilatecv2.dilate(mask,kernel,iterations2)#闭操作去掉物体内部的小块closecv2.morphologyEx(dilate,cv2.MORPH_CLOSE,kernel)cnts,hcv2.findContours(close,cv2.RETR_TREE,cv2.CHAIN_APPROX_NONE)for(i,c)in enumerate(cnts):(x,y,w,h)cv2.boundingRect(c)cv2.rectangle(frame,(x,y),(xw,yh),(0,255,0),2)cv2.imshow(mask,mask)cv2.imshow(erode,erode)cv2.imshow(video,frame)keycv2.waitKey(1)if(key27):break
cap.release()
cv2.destroyAllWindows()