首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >关于成帧算法/在Python中提取某个感兴趣的对象

关于成帧算法/在Python中提取某个感兴趣的对象
EN

Stack Overflow用户
提问于 2019-06-28 04:34:16
回答 1查看 95关注 0票数 3

你好,对于一个个人项目,我需要从图像中剔除提取这个水下闸门,并省略除闸门以外的任何东西。图像在这里是彩色的,但我可以假设我收到的门的图像将只是线条,门是白色线条,背景是黑色。有没有人能给我一些关于如何解决这个问题的建议?谈到OpenCV,我是个新手,所以我有点迷茫。

EN

回答 1

Stack Overflow用户

发布于 2019-06-28 05:27:28

大意是这样的

cv2.threshold()

  • Erode提取蓝色channel

  • Threshold图像,去除黑线,用cv2.erode()

  • Find轮廓线隔离门,用cv2.contourArea()

  • Create和cv2.findContours()对门轮廓进行滤波,用cv2.bitwise_and()

对掩模和扩张图像进行

代码语言:javascript
复制
import cv2
import numpy as np

# Load in image and create copy
image = cv2.imread('1.png')
original = image.copy()

# Gaussian blur and extract blue channel
blur = cv2.GaussianBlur(image, (3,3), 0)
blue = blur[:,:,0]

# Threshold image and erode to isolate gate contour
thresh = cv2.threshold(blue,135, 255, cv2.THRESH_BINARY_INV)[1]
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3,3))
erode = cv2.erode(thresh, kernel, iterations=4)

# Create a mask and find contours
mask = np.zeros(original.shape, dtype=np.uint8)
cnts = cv2.findContours(erode, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]

# Filter for gate contour using area and draw onto mask
for c in cnts:
    area = cv2.contourArea(c)
    if area > 6000:
        cv2.drawContours(mask, [c], -1, (255,255,255), 2)

# Dilate to restore contour and mask it with original image
dilate = cv2.dilate(mask, kernel, iterations=7)
result = cv2.bitwise_and(original, dilate)

cv2.imshow('thresh', thresh)
cv2.imshow('erode', erode)
cv2.imshow('mask', mask)
cv2.imshow('dilate', dilate)
cv2.imshow('result', result)
cv2.waitKey()
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56798152

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档