博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Opencv-轮廓的查找与绘制
阅读量:2050 次
发布时间:2019-04-28

本文共 3384 字,大约阅读时间需要 11 分钟。

作者:安澈澈^
案列:© Fu Xianjun. All Rights Reserved.

一、轮廓的查找与绘制

函数原型:

cv2.findContours(image, mode, method, contours=None, hierarchy=None, offset=None)

查找轮廓

mode:cv2.RETR_EXTERNAL 只检测外轮廓cv2.RETR_LIST检测的轮廓不建立等级关系cv2.RETR_CCOMP建立两个等级的轮廓cv2.RETR_TREE建立一个等级树结构的轮廓method:cv2.CHAIN_APPROX_NONE存储所有的轮廓点cv2.CHAIN_APPROX_SIMPLE压缩水平方向,垂直方向,对角线方向的元素,只保留该方向的终点坐标,例如一个矩形轮廓只需4个点来保存轮廓信息。
import cv2import numpy as npimg = cv2.imread('shape.jpg')    #读取图像gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) #转为灰度值图ret, binary = cv2.threshold(gray,220,255,cv2.THRESH_BINARY) #转为二值图contours, hierarchy = cv2.findContours(binary,cv2.RETR_TREE,\                                       cv2.CHAIN_APPROX_NONE) #寻找轮廓n=len(contours)       #轮廓个数print(n)print(len(contours[0]))       #轮廓0像素数目print(len(contours[1]))       #轮廓1像素数目print(len(contours[2]))       #轮廓2像素数目print(len(contours[3]))       #轮廓3像素数目

绘制轮廓

cv2.imshow("img",img) #显示原图像img2 = cv2.drawContours(img,contours,1,(0,165,255),-1)  #绘制轮廓,1表示绘制第几个轮廓cv2.imshow("contours",img2)   #显示轮廓cv2.waitKey()cv2.destroyAllWindows()

实物轮廓检测

import cv2import numpy as npimg = cv2.imread('pig.jpg')cv2.imshow("img",img)   #显示原图像gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)      #转为灰度图ret, binary = cv2.threshold(gray,245,255,cv2.THRESH_BINARY_INV)  #转为二值图cv2.imshow("binary" ,binary)        #显示二值化结果contours, hierarchy = cv2.findContours(binary,cv2.RETR_LIST,cv2.CHAIN_APPROX_NONE)#寻找轮廓mask=np.zeros(img.shape,np.uint8)  #生成黑背景,即全为0mask=cv2.drawContours(mask,contours,-1,(255,255,255),-1)  #绘制轮廓,形成掩膜cv2.imshow("mask" ,mask)        #显示掩膜result=cv2.bitwise_and(img,mask)   #按位与操作,得到掩膜区域cv2.imshow("result" ,result)     #显示图像中提取掩膜区域cv2.waitKey()cv2.destroyAllWindows()

二、使用矩特征计算轮廓的面积及长度

计算图像的矩特征

import cv2import numpy as npimg = cv2.imread('shape.jpg')    #读取图像gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) #转为灰度值图ret, binary = cv2.threshold(gray,220,255,cv2.THRESH_BINARY) #转为二值图contours, hierarchy = cv2.findContours(binary,cv2.RETR_TREE,\                                       cv2.CHAIN_APPROX_NONE) #寻找轮廓n=len(contours)       #轮廓个数contoursImg=[]for i in range(n):    temp=np.zeros(img.shape,np.uint8) #生成黑背景    contoursImg.append(temp)    contoursImg[i]=cv2.drawContours(contoursImg[i],contours,i,(255,255,255), 3)  #绘制轮廓    cv2.imshow("contours[" + str(i)+"]",contoursImg[i])   #显示轮廓print("计算图像的矩特征:")for i in range(n):    moment=cv2.moments(contours[i])    print(f"轮廓{i}的矩:\n{moment}")cv2.waitKey()cv2.destroyAllWindows()

计算面积

for i in range(n):    area=cv2.moments(contours[i])["m00"]    print(f"轮廓{i}的面积:\n{area}")

计算轮廓面积

import cv2import numpy as npimg = cv2.imread('shape.jpg')    #读取图像gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) #转为灰度值图ret, binary = cv2.threshold(gray,220,255,cv2.THRESH_BINARY) #转为二值图contours, hierarchy = cv2.findContours(binary,cv2.RETR_TREE,\                                       cv2.CHAIN_APPROX_NONE) #寻找轮廓n=len(contours)       #轮廓个数contoursImg=[]for i in range(n):    area = cv2.contourArea(contours[i])    print(f"轮廓{i}的面积:\n{area}")

显示长度小于600的轮廓

for i in range(n):    length = cv2.arcLength(contours[i], True)  #获取轮廓长度    print(f"轮廓{i}的长度:\n{length}")       if length<600:        temp=np.zeros(img.shape,np.uint8) #生成黑背景        contoursImg.append(temp)        contoursImg[i]=cv2.drawContours(contoursImg[i],contours,i,(255,255,255), 3)  #绘制轮廓        cv2.imshow("contours[" + str(i)+"]",contoursImg[i])   #显示轮廓cv2.waitKey()cv2.destroyAllWindows(

转载地址:http://ilgof.baihongyu.com/

你可能感兴趣的文章
Leetcode C++《热题 Hot 100-30》31.下一个排列
查看>>
Leetcode C++《热题 Hot 100-40》64.最小路径和
查看>>
Leetcode C++《热题 Hot 100-41》75.颜色分类
查看>>
Leetcode C++《热题 Hot 100-42》78.子集
查看>>
Leetcode C++《热题 Hot 100-43》94.二叉树的中序遍历
查看>>
Leetcode C++ 《第175场周赛-1 》5332.检查整数及其两倍数是否存在
查看>>
Leetcode C++ 《第175场周赛-2 》5333.制造字母异位词的最小步骤数
查看>>
Leetcode C++ 《第175场周赛-3》1348. 推文计数
查看>>
Leetcode C++《热题 Hot 100-44》102.二叉树的层次遍历
查看>>
Leetcode C++《热题 Hot 100-45》338.比特位计数
查看>>
读书摘要系列之《kubernetes权威指南·第四版》第一章:kubernetes入门
查看>>
Leetcode C++《热题 Hot 100-46》739.每日温度
查看>>
Leetcode C++《热题 Hot 100-47》236.二叉树的最近公共祖先
查看>>
Leetcode C++《热题 Hot 100-48》406.根据身高重建队列
查看>>
《kubernetes权威指南·第四版》第二章:kubernetes安装配置指南
查看>>
Leetcode C++《热题 Hot 100-49》399.除法求值
查看>>
Leetcode C++《热题 Hot 100-51》152. 乘积最大子序列
查看>>
[Kick Start 2020] Round A 1.Allocation
查看>>
[Kick Start 2020] Round A 2.Plates
查看>>
Leetcode C++ 《第181场周赛-1》 5364. 按既定顺序创建目标数组
查看>>