gpt4 book ai didi

python - 关于分帧算法/提取Python中某个感兴趣的对象

转载 作者:太空宇宙 更新时间:2023-11-03 21:10:09 24 4
gpt4 key购买 nike

This is the image

您好,对于个人项目,我需要从图像中裁剪出这个水下门,并保留除门以外的任何内容。图像在这里是彩色的,但我可以假设我收到的门的图像只会有内衬,门是白线,背景是黑色。谁能给我任何有关如何解决此解决方案的建议?我是 OpenCV 的新手,所以我有点迷茫。

最佳答案

这是主要思想

  • 高斯模糊图像并提取蓝色 channel
  • 使用 cv2.threshold() 阈值图像
  • 使用 cv2.erode()
  • 进行腐 eclipse 以去除黑线并隔离门
  • 使用 cv2.findContours()cv2.contourArea()
  • 查找轮廓并过滤门轮廓
  • 使用 cv2.dilate() 创建 mask 和膨胀图像
  • 使用 cv2.bitwise_and() 裁剪门
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()

关于python - 关于分帧算法/提取Python中某个感兴趣的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56798152/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com