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()
python opencv(四)图像的腐蚀和膨胀操作
于 2023-03-23 18:26:29 首次发布