From 4e9b6a18b7b3908a79520b75a8ce354c0a591ec3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Python=E5=B0=8F=E7=99=BD=E8=BF=9B=E9=98=B6?= <860365566@qq.com> Date: Thu, 9 Dec 2021 15:30:22 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=8A=E4=BC=A0=E6=96=B0=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../5.\350\275\256\345\273\223/Contours.md" | 92 +++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 "data/1.OpenCV\345\210\235\351\230\266/2.\344\272\214\345\200\274\345\233\276\345\203\217\345\244\204\347\220\206/5.\350\275\256\345\273\223/Contours.md" diff --git "a/data/1.OpenCV\345\210\235\351\230\266/2.\344\272\214\345\200\274\345\233\276\345\203\217\345\244\204\347\220\206/5.\350\275\256\345\273\223/Contours.md" "b/data/1.OpenCV\345\210\235\351\230\266/2.\344\272\214\345\200\274\345\233\276\345\203\217\345\244\204\347\220\206/5.\350\275\256\345\273\223/Contours.md" new file mode 100644 index 0000000..4816925 --- /dev/null +++ "b/data/1.OpenCV\345\210\235\351\230\266/2.\344\272\214\345\200\274\345\233\276\345\203\217\345\244\204\347\220\206/5.\350\275\256\345\273\223/Contours.md" @@ -0,0 +1,92 @@ +# 寻找图像的轮廓 + +轮廓是由连续的点组成的曲线。轮廓与边缘很相似,但轮廓是连续的,边缘不一定都连续。 + +轮廓反映了物体的基本外形,常用于形状分析和物体的检测和识别。 + +OpenCV 提供函数 **cv.findContours()** 对二值图像寻找轮廓,函数 **cv2.drawContours()** 绘制轮廓。 + +**函数说明:** + +``` +cv.boxFilter(src, ddepth, ksize[, dst[, anchor[, normalize[, borderType]]]]) → dst +``` + +我们从康熙御笔书法中提取轮廓,据说这是一种经典的书画复制技法。下图从上到下分别是: +* 康熙御笔原图 +* 康熙御笔碑帖图 +* 康熙御笔轮廓图 + +![](./ContoursOutput.jpg) + +下面对康熙御笔寻找轮廓实现代码正确的是? + +## 答案 + +``` +import cv2 as cv + +if __name__ == '__main__': + img = cv.imread("Contours.jpg", flags=1) + imgGray = cv.cvtColor(img, cv.COLOR_BGR2GRAY) + + ret, thresh = cv.threshold(imgGray, 127, 255, cv.THRESH_BINARY_INV) + image, contours, hierarchy = cv.findContours(thresh, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE) + contourPic = cv.drawContours(img, contours, -1, (0, 0, 255), 2) + + cv.imshow("ContourPicture", contourPic) + cv.waitKey(0) +``` + +## 选项 + +### 使用了灰度图而不是二值图来查找轮廓 + +``` +import cv2 as cv + +if __name__ == '__main__': + img = cv.imread("Contours.jpg", flags=1) + imgGray = cv.cvtColor(img, cv.COLOR_BGR2GRAY) + + ret, thresh = cv.threshold(imgGray, 127, 255, cv.THRESH_BINARY_INV) + image, contours, hierarchy = cv.findContours(imgGray, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE) + contourPic = cv.drawContours(img, contours, -1, (0, 0, 255), 2) + + cv.imshow("ContourPicture", contourPic) + cv.waitKey(0) +``` + +### 阈值处理时的阈值设置过大 + +``` +import cv2 as cv + +if __name__ == '__main__': + img = cv.imread("Contours.jpg", flags=1) + imgGray = cv.cvtColor(img, cv.COLOR_BGR2GRAY) + + ret, thresh = cv.threshold(imgGray, 205, 255, cv.THRESH_BINARY_INV) + image, contours, hierarchy = cv.findContours(thresh, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE) + contourPic = cv.drawContours(img, contours, -1, (0, 0, 255), 2) + + cv.imshow("ContourPicture", contourPic) + cv.waitKey(0) +``` + +### 函数 findContours 缺少输出参数 image + +``` +import cv2 as cv + +if __name__ == '__main__': + img = cv.imread("Contours.jpg", flags=1) + imgGray = cv.cvtColor(img, cv.COLOR_BGR2GRAY) + + ret, thresh = cv.threshold(imgGray, 127, 255, cv.THRESH_BINARY_INV) + contours, hierarchy = cv.findContours(thresh, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE) + contourPic = cv.drawContours(img, contours, -1, (0, 0, 255), 2) + + cv.imshow("ContourPicture", contourPic) + cv.waitKey(0) +``` -- GitLab