OpenCV目标识别

一 图像轮廓

具有相同颜色或强度的连续点的曲线。
在这里插入图片描述
图像轮廓的作用
可以用于图像分析
物体的识别与检测

注意

为了检测的准确性,需要先对图像进行二值化或Canny操作。
画轮廓时会修改输入的图像。

轮廓查找的API

findContours(img,mode,ApproximationMode,...)
两个返回值,contours和hierarchy
mode
RETR_EXTERNAL=0,表示只检测外轮廓。
RETR_LIST=1,检测的轮廓不建立等级关系
RETR_CCOMP=2,每层最多两级
RETR_TREE=3,桉树形存储轮廓

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

ApproximationMode
CHAIN_APPROX_NONE,保存所有轮廓上的点
CHAIN_APPROX_SIMPLE,只保存角点
import cv2
import numpy as np

img=cv2.imread('./9615.jpg')
print(img.shape)

#转变成单通道
gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
print(gray.shape)

#二值化
ret,binary=cv2.threshold(gray,150,255,cv2.THRESH_BINARY)

#轮廓查找
contours,hierarchy=cv2.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 np

img=cv2.imread('./9615.jpg')
print(img.shape)

#转变成单通道
gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
print(gray.shape)

#二值化
ret,binary=cv2.threshold(gray,150,255,cv2.THRESH_BINARY)

#轮廓查找
contours,hierarchy=cv2.findContours(binary,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
print(contours)

#绘制轮廓
img1=cv2.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 np

img=cv2.imread('./9615.jpg')
print(img.shape)

#转变成单通道
gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
print(gray.shape)

#二值化
ret,binary=cv2.threshold(gray,150,255,cv2.THRESH_BINARY)

#轮廓查找
contours,hierarchy=cv2.findContours(binary,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
print(contours)

#绘制轮廓
img1=cv2.drawContours(img,contours,0,(0,0,255),5)

#计算面积
area=cv2.contourArea(contours[0])
print("area=%d"%(area))

# 计算周长
len=cv2.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 np

def drawShape(src,points):
    i=0
    while i<len(points):
        if(i==len(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)
        i=i+1

img=cv2.imread('./palm.png')
#print(img.shape)

#转变成单通道
gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
print(gray.shape)

#二值化
ret,binary=cv2.threshold(gray,150,255,cv2.THRESH_BINARY_INV)

#轮廓查找
contours,hierarchy=cv2.findContours(binary,cv2.RETR_CCOMP,cv2.CHAIN_APPROX_NONE)
#print(contours)

#绘制轮廓
img1=cv2.drawContours(img,contours,-1,(0,255,0),5)

e=20

approx=cv2.approxPolyDP(contours[0],e,True)

drawShape(img,approx)

hull=cv2.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 np

def drawShape(src,points):
    i=0
    while i<len(points):
        if(i==len(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)
        i=i+1

img=cv2.imread('./Hello.png')
#print(img.shape)

#转变成单通道
gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
print(gray.shape)

#二值化
ret,binary=cv2.threshold(gray,150,255,cv2.THRESH_BINARY)
print(binary.shape)
#轮廓查找cv2.RETR_CCOMP CHAIN_APPROX_NONE
contours,hierarchy=cv2.findContours(binary,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
#print(contours)

#绘制轮廓
#img=cv2.drawContours(img,contours,-1,(0,255,0),2)

r=cv2.minAreaRect(contours[1])
box=cv2.boxPoints(r)
box=np.int32(box)

cv2.drawContours(img,[box],0,(0,0,255),5)

x,y,w,h=cv2.boundingRect(contours[1])

cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)


cv2.imshow('img',img)

cv2.waitKey(0)


在这里插入图片描述

六 车辆统计

窗口的展示
图像/视频的加载
基本图形的绘制
车辆识别
基本图像运算与处理
形态学
轮廓查找

1 涉及到的知识点

加载视频
通过形态学识别车辆
对车辆进行统计
显示车辆统计信息

import cv2
import numpy as np

cap=cv2.VideoCapture('video.mp4')

while True:
   ret,frame= cap.read()

   if(ret==True):
       cv2.imshow('video',frame)

   key=cv2.waitKey(1)
   if(key==27)
       break
cap.release()
cv2.destroyAllWindows()

形态学处理

import cv2
import numpy as np

cap=cv2.VideoCapture('video.mp4')

bgsubmog=cv2.bgsegm.createBackgroundSubtractorMOG()

#形态学kernel
kernel=cv2.getStructuringElement(cv2.MORPH_RECT,(5,5))

while True:
   ret,frame= cap.read()

   if(ret==True):
       #灰度
       cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
       #去噪(高斯)
       blu=cv2.GaussianBlur(frame,(3,3),5)
       #去背影
       mask=bgsubmog.apply(blu)

       #腐蚀
       erode=cv2.erode(mask,kernel)

       #膨胀
       dilate=cv2.dilate(mask,kernel,iterations=2)

       #闭操作,去掉物体内部的小块
       close=cv2.morphologyEx(dilate,cv2.MORPH_CLOSE,kernel)

       cnts,h=cv2.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),(x+w,y+h),(0,255,0),2)

       cv2.imshow('mask',mask)
       cv2.imshow('erode',erode)
       cv2.imshow('video',frame)

   key=cv2.waitKey(1)
   if(key==27):
       break
cap.release()
cv2.destroyAllWindows()

相关推荐

  1. OpenCV 实时目标检测

    2024-06-17 01:18:03       12 阅读
  2. opencv进行人脸识别

    2024-06-17 01:18:03       27 阅读
  3. opencv动态识别人脸

    2024-06-17 01:18:03       10 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-06-17 01:18:03       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-06-17 01:18:03       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-06-17 01:18:03       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-06-17 01:18:03       18 阅读

热门阅读

  1. Opencv无法自动补全

    2024-06-17 01:18:03       6 阅读
  2. 15分钟面试被5连CALL,你扛得住么?

    2024-06-17 01:18:03       7 阅读
  3. SSH error : no kex alg message

    2024-06-17 01:18:03       8 阅读
  4. Spring (60)Spring WebFlux

    2024-06-17 01:18:03       9 阅读
  5. 数据结构之B树的原理与业务场景

    2024-06-17 01:18:03       8 阅读
  6. Autosar实践——诊断配置(DaVinci Configuration)

    2024-06-17 01:18:03       6 阅读
  7. 2024.06.16 刷题日记

    2024-06-17 01:18:03       4 阅读
  8. linux发展历程

    2024-06-17 01:18:03       6 阅读
  9. atcoder ABC 358-B题详解

    2024-06-17 01:18:03       7 阅读
  10. Qt中的事件循环

    2024-06-17 01:18:03       6 阅读
  11. Linux各目录的作用

    2024-06-17 01:18:03       7 阅读
  12. MySQL 保姆级教程(四):过滤数据

    2024-06-17 01:18:03       6 阅读