import cv2 import numpy as np original = cv2.imread(r'E:\opencv\er_888gai.jpg') print(original.shape) # 查找物体轮廓 def findcontour(img): gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # 图像灰度化 ret, binary = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY) # 图像二值化 image, contours, hierarchy = cv2.findContours(binary, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) # 查找物体轮廓 return image, contours, hierarchy image, contours, hierarchy = findcontour(original) nums = len(contours) color = [(255, 0, 0), (0, 255, 0), (0, 0, 255)] contourssplit=[] for i in range(nums): temp = np.zeros(original.shape, np.uint8) contourssplit.append(temp) contourssplit[i] = cv2.drawContours(contourssplit[i], contours, i, color[i], 10) a = cv2.moments(contours[i]) print("轮廓" + str(i) + "的面积:%d" % cv2.moments(contours[i])['m00']) print("轮廓" + str(i) + "的重心:%d" % int(cv2.moments(contours[i])['m10']/cv2.moments(contours[i])['m00']), int(cv2.moments(contours[i])['m01']/cv2.moments(contours[i])['m00'])) print("轮廓" + str(i) + "的长度:%d" % cv2.arcLength(contours[i], True))
时间: 2023-05-24 11:06:25 浏览: 143
这段代码是通过Python语言中的cv2模块和numpy模块导入相关的函数和类。其中,cv2.imread函数用来读取名为"E:\opencv\er_888gai.jpg"的图片,将其存储在original变量中。最后输出图片的高、宽、通道数等信息(即图片的形状),并打印在屏幕上。
阅读全文