图像轮廓1

 @Fu Xianjun. All Rights Reserved.

查找轮廓

import cv2
import numpy as np
img = 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()

逐个绘制一幅图像内的边缘信息

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])   #显示轮廓
cv2.waitKey()
cv2.destroyAllWindows()

实物轮廓检测

import cv2
import numpy as np
img = cv2.imread('xz.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)  #生成黑背景,即全为0
mask=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 cv2
import numpy as np
img = 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()

计算轮廓面积

import cv2
import numpy as np
img = 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}")

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值