python opencv(四)图像的腐蚀和膨胀操作

这段代码展示了如何使用OpenCV在Python中绘制多边形,进行图像膨胀操作以及计算相交区域的面积。首先,定义了一个函数用于显示图像,然后绘制了多边形的边和填充区域。接着,进行了膨胀操作并计算了两个多边形相交的像素面积。最后,展示了如何合并膨胀后的图像。
摘要由CSDN通过智能技术生成
import cv2 as cv
import numpy as np 
from matplotlib import pyplot as plt

# 显示图像
def cv_show(winame, image):
    cv.imshow(winame, image)
    cv.waitKey(0)
    cv.destroyAllWindows()
    
   
points = [[10,20],[30,50],[40,80],[60,60],[90,10]]
poly = np.array(points)
img = np.zeros([400,400,3])

# 绘制多边形的边
# 此处的边信息必须为三元组 [[  [],[],[],[],[],[]  ]]
cv.polylines(img, [poly], True, (255, 255, 255), 2)     # True表示该图形为封闭图形
cv_show('Polygon', img)


points = [[10,20],[30,50],[40,80],[60,60],[90,10]]
poly = np.array(points)
img = np.zeros([400,400,3])

# 绘制多边形的边
# 此处的边信息必须为三元组 [[  [],[],[],[],[],[]  ]]
cv.polylines(img, [poly], True, (255, 255, 255), 2)     # True表示该图形为封闭图形
cv_show('Polygon', img)


img = np.zeros([400,400,3])
cv.fillPoly(img, [poly], (0, 0, 125))  #img,pts同上,实际代码只需要这三个参数即可,其他均有默认值
# cv_show('Polygon', img)

img = np.zeros([400,400,3])
cv.fillPoly(img, [poly], (0, 0, 125))  #img,pts同上,实际代码只需要这三个参数即可,其他均有默认值
# cv_show('Polygon', img)

kernel = np.ones((20,20),np.uint8)  
# dilate 函数需要有接收值
# 接收值才是最终操作之后的结果信息
dilation = cv.dilate(img,kernel,iterations = 1) 
# cv.polylines(dilation, [poly], True, (255, 255, 255), 2)     # True表示该图形为封闭图形

cv_show('Polygon', dilation)

points = [[10,20],[30,50],[40,80],[60,60],[90,10]]
poly = np.array(points)
img = np.zeros([400,400,3])

img = cv.fillPoly(img, [poly], (0, 0, 125))  #img,pts同上,实际代码只需要这三个参数即可,其他均有默认值

kernel = np.ones((10,10),np.uint8)  
dilation_img = cv.dilate(img,kernel,iterations = 1) 

cv_show('Polygon', dilation_img)



kernel = np.ones((10,10),np.uint8)  

points = [[10,20],[30,50],[40,80],[60,60],[90,10]]
poly = np.array(points)
img = np.zeros([400,400,3])
img = cv.fillPoly(img, [poly], (0, 0, 125))  #img,pts同上,实际代码只需要这三个参数即可,其他均有默认值
dilation_img = cv.dilate(img,kernel,iterations = 1) 


points2 = [[30,20],[30,50],[40,80],[60,90],[90,40]]
poly2 = np.array(points2)
img2 = np.zeros([400,400,3])
img2 = cv.fillPoly(img2, [poly2], (0, 0, 125))  #img,pts同上,实际代码只需要这三个参数即可,其他均有默认值
dilation_img2 = cv.dilate(img2,kernel,iterations = 1) 


img_and = cv.bitwise_and(dilation_img, dilation_img2)
# 根据相交图像中的信息从而得到大于零的像素个数也就是面积信息
and_area =np.sum(np.float32(np.greater(img_and,0)))
print("相交的面积是多少:",and_area)

    

 points = [[10,20],[30,50],[40,80],[60,60],[90,10]]
poly = np.array(points)
img = np.zeros([400,400,3])
img = cv.fillPoly(img, [poly], (0, 0, 125))  #img,pts同上,实际代码只需要这三个参数即可,其他均有默认值
 
img = np.uint8(img)

img = np.uint8(img_and)
gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
ret, thresh = cv.threshold(gray, 1, 255, cv.THRESH_BINARY)
contours, hierarchy = cv.findContours(thresh, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE)


area = cv.contourArea(contours[0])
# 显示计算结果
print('Intersection area:', area)

cv.drawContours(img_and,contours, -1, 255, thickness=-1)
cv_show("相交",img_and)

cv.polylines(img_and, contours, True, (255, 255, 255), 1) 
cv_show("相交",img_and)


img = np.zeros([400,400,3])
cv.polylines(img, contours, True, (255, 255, 255), 1) 
cv.polylines(img, [poly], True, (255, 255, 255), 1)     # True表示该图形为封闭图形
cv_show("circle",img)


# 将膨胀后的图像减去原始图像
result = cv.subtract(dilation, img)
cv_show("result",result)



import cv2 as cv
import numpy as np 
from matplotlib import pyplot as plt

kernel = np.ones((10,10),np.uint8)  

points = [[10,20],[30,50],[40,80],[60,60],[90,10]]
poly = np.array(points)
img = np.zeros([400,400,3])
img = cv.fillPoly(img, [poly], (0, 0, 125))  #img,pts同上,实际代码只需要这三个参数即可,其他均有默认值
dilation_img = cv.dilate(img,kernel,iterations = 1) 
buffer = cv.subtract(dilation_img, img)

points2 = [[30,20],[30,50],[40,80],[60,90],[90,40]]
poly2 = np.array(points2)
img2 = np.zeros([400,400,3])
img2 = cv.fillPoly(img2, [poly2], (0, 0, 125))  #img,pts同上,实际代码只需要这三个参数即可,其他均有默认值
dilation_img2 = cv.dilate(img2,kernel,iterations = 1) 
buffer2 = cv.subtract(dilation_img2, img2)


# img2 = np.zeros([400,400,3])
cv_show("result",buffer2)


# 膨胀之后的相交操作
img_and = cv.bitwise_and(buffer, buffer2)

# 求面积的方法一
# 根据相交图像中的信息从而得到大于零的像素个数也就是面积信息
and_area =np.sum(np.float32(np.greater(img_and,0)))
print("Intersection area:",and_area)


# 将两个图像相加,权重分别为0.5和0.5
result = cv.addWeighted(buffer, 0.5, buffer2, 0.5, 0)
# 显示结果图像
cv.imshow('Merged Image', result)
cv.waitKey(0)
cv.destroyAllWindows()




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值