前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >opencv: 轮廓绘制 详细拆解(图示+源码)

opencv: 轮廓绘制 详细拆解(图示+源码)

作者头像
JNingWei
发布2018-09-28 15:51:56
1.3K0
发布2018-09-28 15:51:56
举报
文章被收录于专栏:JNing的专栏JNing的专栏

过程图解

第一步

读取要进行处理的原图:

代码语言:javascript
复制
origin_pic = './pic/6.jpg'
save_folder = './generated_pics'

img = cv2.imread(origin_pic)

此时原图如下:

这里写图片描述
这里写图片描述

第二步

将原图转换成单通道图(例如本例中用cv2.cvtColor转换成灰度图):

代码语言:javascript
复制
imgray = cv2.cvtColor(src=img, code=cv2.COLOR_BGR2GRAY)

转换后的单通道图如下:

这里写图片描述
这里写图片描述

第三步

对得到的单通道图进行阀值处理:

代码语言:javascript
复制
_, thresh = cv2.threshold(src=imgray, thresh=150, maxval=200, type=0)

得到阀值图片如下:

这里写图片描述
这里写图片描述

第四步

寻找轮廓并记录在返回的 contours列表 中:

代码语言:javascript
复制
image, contours, _1 = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

第五步

根据轮廓数据表,在三通道彩色图片上绘制轮廓:

代码语言:javascript
复制
contoured_colored_img = cv2.drawContours(img, contours, -1, (0, 0, 255), 2)

结果图如下:

这里写图片描述
这里写图片描述

也可以在黑白三通道图片上绘制轮廓:

代码语言:javascript
复制
thresh_expanded = np.expand_dims(thresh, axis=2)
gray_img = np.concatenate([thresh_expanded, thresh_expanded, thresh_expanded], axis=2)
contoured_gray_img = cv2.drawContours(gray_img, contours, -1, (0, 0, 255), 2)

结果图如下:

这里写图片描述
这里写图片描述

完整源码

我自己写的完整源码如下:

代码语言:javascript
复制
# coding=utf-8

import numpy as np
import cv2

origin_pic = './pic/6.jpg'
save_folder = './generated_pics'

import shutil
try:
    shutil.rmtree(save_folder)
except OSError:
    pass
import os
os.makedirs(save_folder)

img = cv2.imread(origin_pic)
imgray = cv2.cvtColor(src=img, code=cv2.COLOR_BGR2GRAY)
_, thresh = cv2.threshold(src=imgray, thresh=150, maxval=200, type=0)
image, contours, _1 = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
thresh_expanded = np.expand_dims(thresh, axis=2)
gray_img = np.concatenate([thresh_expanded, thresh_expanded, thresh_expanded], axis=2)
contoured_gray_img = cv2.drawContours(gray_img, contours, -1, (0, 0, 255), 2)
contoured_colored_img = cv2.drawContours(img, contours, -1, (0, 0, 255), 2)

cv2.imwrite(os.path.join(save_folder, 'imgray.jpg'), imgray)
cv2.imshow('imgray', imgray)
cv2.waitKey(2000)

cv2.imwrite(os.path.join(save_folder, 'thresh.jpg'), thresh)
cv2.imshow('thresh', thresh)
cv2.waitKey(2000)

cv2.imwrite(os.path.join(save_folder, 'image.jpg'), image)
cv2.imshow('image', image)
cv2.waitKey(2000)

cv2.imwrite(os.path.join(save_folder, 'contoured_gray_img.jpg'), contoured_gray_img)
cv2.imshow('contoured_gray_img', contoured_gray_img)
cv2.waitKey(2000)

cv2.imwrite(os.path.join(save_folder, 'contoured_colored_img.jpg'), contoured_colored_img)
cv2.imshow('', contoured_colored_img)
cv2.waitKey(2000)
cv2.destroyAllWindows()
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017年08月31日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 过程图解
    • 第一步
      • 第二步
        • 第三步
          • 第四步
            • 第五步
            • 完整源码
            领券
            问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档