问题遇到的现象和发生背景
问题相关代码,请勿粘贴截图
import cv2
import numpy as np
imagepath = 'E:/149_v4.png'
# imagepath = ''
img = cv2.imread(imagepath, 1)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# 二值化
ret, thresh = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)
# 图片轮廓
image, contours, hierarchy = cv2.findContours(thresh, 2, 1)
print(len(contours))
for i in range(len(contours)):
if len(contours[i]) < 10: # 删除图中可能存在的独立小凸包
contours.remove(contours[i])
for j in contours:
cnt = j
# 寻找凸包并绘制凸包(轮廓)
hull = cv2.convexHull(cnt)
# hull = np.asarray(hull)
length = len(hull)-1
# print(length)
dis_list = []
for k in range(length):
dis = np.linalg.norm(hull[k] - hull[k + 1])
if dis < 100:
dis_list.append(hull[k])
length -= 1
# new_hull = np.delete(hull, k + 1, axis=0)
# print(dis)
# dis_list.append(dis)
dis_list = np.asarray(dis_list)
temp = np.zeros(img.shape, np.uint8) # 创建一个黑色的背景图
cv2.polylines(temp, [dis_list], True, (255, 255, 2), 2)
# cv2.polylines(temp, [s_dis_list], True, (255, 5, 255), 2)
# cv2.polylines(img, [hull], True, (0, 255, 0), 2)
# 显示图片
cv2.imshow('line', temp)
cv2.waitKey()
运行结果及报错内容
我的解答思路和尝试过的方法
我想要达到的结果
如何能让两个轮廓在一起显示