实现功能
1, 在同一张图上画两个相交的ROI图形
2,求两个ROI的交并补等操作
3,求一个被填充的ROI的面积(像素点个数) 及 所有像素点
在同一张图上画两个相交的ROI图形:
# 求两个ROI的交并补等操作
def or_and_not_xor_main():
#1, 创建第一个image与mask
img_zero = np.zeros((900, 1200, 3), dtype=np.uint8)
img_zero_sample = img_zero.copy()
img_zero_2 = img_zero.copy()
print(img_zero.shape)
# points = np.array([[101, 101], [110, 101], [101, 110], [110, 110]])
# points_ROI = np.array([[101, 101], [110, 101], [110, 110], [101, 110]])
points_ROI = np.array([[101, 101], [300, 101], [300, 300], [101, 300]])
# 画顶点
# for i in range(len(points)):
# cv2.circle(img_zero, tuple(points[i]), 5, (0,255,255), -1)
# 填充mask
# cv2.fillPoly(img_zero, [points], (0, 0, 255)) # 任意多边形
cv2.fillConvexPoly(img_zero, points_ROI, (255, 255, 255)) # 凸包
img_gray_roi_mask = cv2.cvtColor(img_zero, cv2.COLOR_RGB2GRAY)
# img_gray_roi_mask中mask的面积,即为白色的点
mask = img_gray_roi_mask[img_gray_roi_mask > 0]
print(len(mask)) # 100个点
cv2.imshow('img_gray_roi_mask A', img_gray_roi_mask)
#2, 创建第二个image与mask
points_ROI_triangle = np.array([[201, 100], [400, 100], [300, 300]])
# 填充mask
cv2.fillConvexPoly(img_zero_2, points_ROI_triangle, (0, 255, 255)) # 凸包
img_gray_roi_mask_triangle = cv2.cvtColor(img_zero_2, cv2.COLOR_RGB2GRAY)
#灰度化后的图转化为白色255
img_gray_roi_mask_triangle[img_gray_roi_mask_triangle > 0] = 255
cv2.imshow('img_gray_roi_mask_triangle B', img_gray_roi_mask_triangle)
#3.1, 求 img_gray_roi_mask(named: A) 与 img_gray_roi_mask_triangle(named: B) 的并集
A_or_B = cv2.bitwise_or(img_gray_roi_mask, img_gray_roi_mask_triangle)
cv2.imshow('A_or_B', A_or_B)
#3.2, 求 img_gray_roi_mask(named: A) 与 img_gray_roi_mask_triangle(named: B) 的交集
A_and_B = cv2.bitwise_and(img_gray_roi_mask, img_gray_roi_mask_triangle)
cv2.imshow('A_and_B', A_and_B)
# 3.3, 求属于A,但不属于B的部分:A-AB
A_sub_AB = cv2.bitwise_xor(img_gray_roi_mask, A_and_B)
cv2.imshow('A_sub_AB', A_sub_AB)
# 3.4, 求A与B的并集关于整张图片的补集,即求整张图片扣去A和B
A_or_B_not = cv2.bitwise_not(A_or_B)
cv2.imshow('A_or_B_not', A_or_B_not)
cv2.waitKey(0)
cv2.destroyAllWindows()
# 求一个被填充的ROI的面积(像素点个数) 及 所有像素点
def get_roi_info_main1():
# 创建第一个image
img_zero = np.zeros((900, 1200, 3), dtype=np.uint8)
# points_ROI = np.array([[101, 101], [300, 101], [300, 300], [101, 300]])
points_ROI = np.array([[101, 101], [110, 101], [110, 110], [101, 110]])
# 画顶点
# for i in range(len(points)):
# cv2.circle(img_zero, tuple(points[i]), 5, (0,255,255), -1)
# 填充mask
# cv2.fillPoly(img_zero, [points], (0, 0, 255)) # 任意多边形
cv2.fillConvexPoly(img_zero, points_ROI, (255, 255, 255)) # 凸包
img_gray_roi_mask = cv2.cvtColor(img_zero, cv2.COLOR_RGB2GRAY)
# img_gray_roi_mask中mask的面积,即为白色的点
# mask = img_gray_roi_mask[img_gray_roi_mask > 0]
# print(len(mask)) # 100个点
# print(mask) # 100个点
# 取坐标 方法1
points = np.where(img_gray_roi_mask)
# print(type(points)) # <class 'tuple'>
# print(len(points)) # 2
# print('points: ', points)
xy_axis = np.array(points).transpose()
print('xy_axis: ', xy_axis)
points_cnt = xy_axis.shape[0]
print('points_cnt: ', points_cnt)
# # 取坐标 方法2
# list_xy_axis = list(map(lambda x: x.tolist(), points))
# print('list_xy_axis', list_xy_axis)
# xy_axis_method2 = np.array(list_xy_axis).transpose()
# print('xy_axis_method2: ', xy_axis_method2)
# cv2.imshow('img_gray_roi_mask', img_gray_roi_mask)
cv2.waitKey(0)
# 画图:A B A∩B
def draw_pic():
img_zero = np.zeros((900, 1200, 3), dtype=np.uint8)
# ROI
points_ROI_A = np.array([[101, 101], [300, 101], [300, 300], [101, 300]])
points_ROI_B = np.array([[201, 100], [400, 100], [300, 300]])
points_ROI_AB = np.array([[201, 100], [300, 100], [300, 300]])
# 填充多边形
cv2.fillConvexPoly(img_zero, points_ROI_A, (255, 255, 0))
cv2.fillConvexPoly(img_zero, points_ROI_B, (0, 255, 255))
cv2.fillConvexPoly(img_zero, points_ROI_AB, (255, 255, 255))
# 画ROI边界
cv2.polylines(img_zero, [points_ROI_A], True, (255, 255, 0), 2)
cv2.polylines(img_zero, [points_ROI_B], True, (0, 255, 255), 2)
# 添加说明文字
cv2.putText(img_zero, 'A', (150, 200), cv2.FONT_HERSHEY_COMPLEX, 2, (0, 0, 255), 2)
cv2.putText(img_zero, 'A&B', (230, 150), cv2.FONT_HERSHEY_COMPLEX, 1, (0, 0, 255), 2)
cv2.putText(img_zero, 'B', (300, 200), cv2.FONT_HERSHEY_COMPLEX, 2, (0, 0, 255), 2)
cv2.imshow('AB:', img_zero)
cv2.waitKey(0)
if __name__ == '__main__':
# draw_pic()
# or_and_not_xor_main()
get_roi_info_main1()
求一个被填充的ROI的面积(像素点个数) 及 所有像素点 (结果):
points:
(array([101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 102, 102, 102,
102, 102, 102, 102, 102, 102, 102, 103, 103, 103, 103, 103, 103,
103, 103, 103, 103, 104, 104, 104, 104, 104, 104, 104, 104, 104,
104, 105, 105, 105, 105, 105, 105, 105, 105, 105, 105, 106, 106,
106, 106, 106, 106, 106, 106, 106, 106, 107, 107, 107, 107, 107,
107, 107, 107, 107, 107, 108, 108, 108, 108, 108, 108, 108, 108,
108, 108, 109, 109, 109, 109, 109, 109, 109, 109, 109, 109, 110,
110, 110, 110, 110, 110, 110, 110, 110, 110], dtype=int64), array([101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 101, 102, 103,
104, 105, 106, 107, 108, 109, 110, 101, 102, 103, 104, 105, 106,
107, 108, 109, 110, 101, 102, 103, 104, 105, 106, 107, 108, 109,
110, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 101, 102,
103, 104, 105, 106, 107, 108, 109, 110, 101, 102, 103, 104, 105,
106, 107, 108, 109, 110, 101, 102, 103, 104, 105, 106, 107, 108,
109, 110, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 101,
102, 103, 104, 105, 106, 107, 108, 109, 110], dtype=int64))
xy_axis:
[[101 101]
[101 102]
[101 103]
[101 104]
[101 105]
[101 106]
[101 107]
[101 108]
[101 109]
[101 110]
[102 101]
[102 102]
[102 103]
[102 104]
[102 105]
[102 106]
[102 107]
[102 108]
[102 109]
[102 110]
[103 101]
[103 102]
[103 103]
[103 104]
[103 105]
[103 106]
[103 107]
[103 108]
[103 109]
[103 110]
[104 101]
[104 102]
[104 103]
[104 104]
[104 105]
[104 106]
[104 107]
[104 108]
[104 109]
[104 110]
[105 101]
[105 102]
[105 103]
[105 104]
[105 105]
[105 106]
[105 107]
[105 108]
[105 109]
[105 110]
[106 101]
[106 102]
[106 103]
[106 104]
[106 105]
[106 106]
[106 107]
[106 108]
[106 109]
[106 110]
[107 101]
[107 102]
[107 103]
[107 104]
[107 105]
[107 106]
[107 107]
[107 108]
[107 109]
[107 110]
[108 101]
[108 102]
[108 103]
[108 104]
[108 105]
[108 106]
[108 107]
[108 108]
[108 109]
[108 110]
[109 101]
[109 102]
[109 103]
[109 104]
[109 105]
[109 106]
[109 107]
[109 108]
[109 109]
[109 110]
[110 101]
[110 102]
[110 103]
[110 104]
[110 105]
[110 106]
[110 107]
[110 108]
[110 109]
[110 110]]
points_cnt: 100