python - 如何检测由较小轮廓组成的较大轮廓?

标签 python opencv image-processing contour

sample image

我对图像处理很陌生。我正在使用 opencv 和 python。 我熟悉查找轮廓的常规方法,但在这种情况下,我想忽略每个正方形作为轮廓,但想要在每个所述形状上绘制轮廓,如图所示。

目前,在阈值化之后,我得到了每个正方形的轮廓。 opencv 库中是否有任何简单的函数来检测这些完整的大形状而不是小方 block 。

最佳答案

因为要提取黄色矩形区域,所以:

1. Read
2. Exrtact green channel 
3. Do morph-close-op and threshold
4. Findcontours and filter by Area

enter image description here


#!/usr/bin/python3
# 2018.01.07 14:04:18 CST
# 2018.01.07 14:20:15 CST

import cv2 

## 1. Read
img = cv2.imread("img01.png")

## 2. Exrtact green channel
g = img[...,1]

## 3. Do morph-close-op and Threshold
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5,5))
morphed = cv2.morphologyEx(g,cv2.MORPH_CLOSE, kernel)

th, threshed = cv2.threshold(morphed, 100, 255, cv2.THRESH_OTSU)

## 4. Findcontours and filter by Area
cnts = cv2.findContours(threshed, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)[-2]

canvas = img.copy()

AREA = img.shape[0]*img.shape[1]/20
for cnt in cnts:
    if cv2.contourArea(cnt) < AREA:
        cv2.drawContours(canvas, [cnt], -1, (0,255,0), 2, cv2.LINE_AA)

## 
cv2.imwrite("res.png", canvas)

关于python - 如何检测由较小轮廓组成的较大轮廓?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48134525/

相关文章:

image - 在“处理”中为单个草图创建多个窗口

Python - 如何使用其他 numpy 数组正确索引 numpy 数组,类似于 MATLAB

python - 如何在 Python 中使用循环创建单选按钮

javascript - 我的 javascript 代码有什么问题还是 opencv.js?

opencv - 无法在64位Windows 7上运行Opencv GPU

javascript - 如何使用 Google 自定义搜索 API 下载 100 张图像以用于处理?

python - 在数据帧的两列之间进行插值

python - 使用 octave 和 python 生成的图像的不同文件大小

c - 对运动 vector 进行分组

image - 在 Matlab 中将灰度图像转换为彩色图像