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


大意是这样的
用cv2.threshold()
cv2.erode()
cv2.contourArea()
cv2.findContours()对门轮廓进行滤波,用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()https://stackoverflow.com/questions/56798152
复制相似问题