import cv2 import numpy as np img = cv2.imread("4/6.jpg") gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) _, thresh = cv2.threshold(gray, 175, 255, cv2.THRESH_BINARY) kernel = np.ones((3, 3), np.uint8) morph = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, kernel, iterations=2) contours, _ = cv2.findContours(morph, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) for c in contours: area = cv2.contourArea(c) perimeter = cv2.arcLength(c, True) x, y, w, h = cv2.boundingRect(c) (cx, cy), radius = cv2.minEnclosingCircle(c) cv2.drawContours(img, [c], -1, (0, 0, 255), 2) cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 2) cv2.circle(img, (int(cx), int(cy)), int(radius), (255, 0, 0), 2) cv2.putText(img, "Area: {:.2f}".format(area), (x, y - 20), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 0), 2) cv2.putText(img, "Perimeter: {:.2f}".format(perimeter), (x, y - 5), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 0), 2) cv2.imshow("result", img) cv2.waitKey(0) cv2.destroyAllWindows()
时间: 2023-06-08 17:08:04 浏览: 150
python之cv2与图像的载入、显示和保存实例
5星 · 资源好评率100%
你好,我是ChitGPT。关于你的代码,它是基于OpenCV进行图像处理的,主要是对图像进行二值化、形态学处理和轮廓提取等操作。最后通过绘制矩形框、圆形和文本等来展示处理结果。这段代码有什么问题或需要解释的地方吗?
阅读全文