简体   繁体   English

填充过滤轮廓的内部 OpenCV Python

[英]Fill inside of an filtered contour OpenCV Python

I have an image that has green borders which encapsulates the items that has to be deleted.我有一个带有绿色边框的图像,它封装了必须删除的项目。 I have filtered the only green part of the image so my next step is to fill inside of the filtered contour with a different color then I will remove that part from the image?我已经过滤了图像中唯一的绿色部分,所以下一步是用不同的颜色填充过滤后的轮廓内部,然后我将从图像中删除该部分? I am very open the any suggestions.我对任何建议都很开放。 Here is my code这是我的代码

import cv2
import numpy as np
import imageio

## Read
img = cv2.imread("test.png",-1)
img2 = imageio.imread("test.png")
## convert to hsv
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)

## mask of green (36,25,25) ~ (86, 255,255)
# mask = cv2.inRange(hsv, (36, 25, 25), (86, 255,255))
mask = cv2.inRange(hsv, (36, 25, 25), (70, 255,255))

## slice the green
imask = mask>0
green = np.zeros_like(img, np.uint8)
green[imask] = img[imask]

green_filter=img[100,100,0]
print(green_filter)


## save 
cv2.imshow('image', green)
cv2.imshow('  ',img)
cv2.waitKey(0)
print(img)
print(green)

image i have: https://ibb.co/vJX1jM4 image i reached currently: https://ibb.co/FnScYHc我拥有的图像: https://ibb.co/vJX1jM4我目前达到的图像: https://ibb.co/FnScYHc

import cv2
import numpy as np
import matplotlib.pyplot as plt
import imageio

## Read
img = cv2.imread("test.png",-1)
## convert to hsv
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)

mask = cv2.inRange(hsv, (36, 25, 25), (70, 255,255))
contours, _ = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE) 
idx = 0 
mask1 = np.zeros_like(img) 

cv2.drawContours(mask1, contours, idx, (255,255,255), -1) 
out = np.zeros_like(img)
out[mask1 == (255,255,255)] = img[mask1 == 255]

fig, axs = plt.subplots(1, 3, figsize=(16, 4))
for ax, image in zip(axs, ['img', 'mask1', 'out']):
    ax.imshow(eval(image))
    ax.set_title(image)
    ax.grid(True)

plt.show()

=> =>

加工

I let you play around with this snippet, especially if you want to remove your green contour.我让你玩弄这个片段,特别是如果你想删除你的绿色轮廓。

If you want to reverse the kept area, you need to replace:如果要反转保留区域,则需要替换:
out[mask1 == (255,255,255)] = img[mask1 == 255]
by:经过:
out[mask1 == (0,0,0)] = img[mask1 == 0]
(see second figure below) (见下图二)

反转结果

I just imported matplotlib for convenience here.为了方便起见,我只是在这里导入了 matplotlib

Sorry for the quick and dirty sudo code here, but this is generally how you would go about it.抱歉,这里的 sudo 代码又快又脏,但这通常是您对它的 go 的方式。

Load in image
Convert to grayscale
Find Contours
Find the index of the contour you want to fill
Use fillConvexPoly to fill the specific contour with the index found above.

Here is some code that can guide you in the right direction.这里有一些代码可以引导您朝着正确的方向前进。

#Read in the image
img = cv2.imread("test.png")

#Convert to grayscale
grayImg = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

#Find contours
contours = cv2.findContours(edged, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)

#Find contour index that you want
#I didn't put code here because I don't know what index would work for your image,
#But you can manually find it by just drawing each contour found one by one until
#You get the contour you want, then use that index to fill it.

#Fill the contour
#NOTE: You will have to adjust this function for your own code
cv2.fillConvexPoly(img, contours[index], lineType=8, shift=0)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM