cv2.floodfill如何工作? [英] How does cv2.floodfill work?

查看:234
本文介绍了cv2.floodfill如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是一个示例代码,显示了cv2.floodfill函数的用法

here is a example code showing usage of cv2.floodfill function

import cv2
import numpy as np
import os

def imshow(img):
    cv2.imshow('img', img)
    cv2.waitKey(0)
    cv2.destroyAllWindows()


img = cv2.imread('test4.png')

gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

thresh = cv2.adaptiveThreshold(gray,255,cv2.ADAPTIVE_THRESH_MEAN_C,cv2.THRESH_BINARY_INV,3,1)

_, contours, hierarchy = cv2.findContours(thresh,cv2.RETR_CCOMP,cv2.CHAIN_APPROX_NONE)

mask = np.zeros(img.shape[:-1],np.uint8)

cv2.drawContours(mask,contours,-1,(255,255,255),-1)

height, width = img.shape[:-1]

mask1 = np.zeros((height+2, width+2), np.uint8)     # line 26
cv2.floodFill(mask,mask1,(0,0),255)     # line 27
mask_inv=cv2.bitwise_not(mask)

imshow(mask_inv)

我正在我的一个项目中使用此功能,但我不了解代码的mask1部分(第26行和第27行) 即

i am using this function in one of me projects but i am not understanding the mask1 part of the code (line 26 and line 27 ) that is,

对于给定高度为"h"和宽度为"w"的图像,为什么要创建形状为h + 2,w + 2的mask1(第26行)

why do we create the mask1 with shape h+2, w+2 for a given image with height-'h' and width-'w'?(line 26)

为什么我们必须将此mask1传递给cv2.floodfill函数?(第27行)

why do we have to pass this mask1 to the cv2.floodfill function?(line 27)

这是示例代码的输入和输出.

here is the input and output of the example code.

输入图像

输出图像

请帮助

推荐答案

当前实现的简短答案是不需要使用该参数.您可以在通话中简单地将mask1替换为None,因为您没有使用它.在此处 此处,以查看不带mask参数的floodFill()的示例.

The short answer for your current implementation is that you don't need to use that parameter. You can simply replace mask1 with None in the call because you're not using it. Check out my answers here, here, and here to see examples of floodFill() without the mask parameter.

mask参数只是在应用了 floodFill()的情况下填写,或者在您只想创建遮罩而不修改图像的情况下.您可以在此处的一个简单项目中看到一个示例范围.在这种情况下,我模仿的是Adobe Photoshop魔术棒选择工具,因此我只需要蒙版,而又不想修改图像.

The mask parameter simply fills in where floodFill() has been applied, or in case you just want to create a mask and not modify your image. You can see an example on one of my simple projects here using the mask parameter. In this case I was mimicking the Adobe Photoshop Magic Wand selection tool, and so I only needed the mask and didn't want to modify the image.

floodFill()的作用是,如果相邻像素在某个像素的阈值差之内,则将像素连接到其相邻像素.四向连通性检查上下左右以及左右两侧的邻居.八向连通性还检查对角像素.这意味着在边框像素处,您需要一堆if语句以不检查边框外的像素,或者,您可以简单地在图像的两边各填充一个像素,这样就不需要特殊情况,两者的代码读取效果更好,速度也更快.

What floodFill() does is connects a pixel to it's neighbors if the neighbors are within some threshold difference of the pixel. Four-way connectivity checks the neighbors above and below, and to the left and right. Eight-way connectivity checks the diagonal pixels in addition. This means that at border pixels, you either need a bunch of if statements to not check the pixels outside the border, OR, you can simply pad the image with one pixel on each side so that you don't need special cases, which both reads better in code and is faster.

通常,这只能在函数内部完成,而不会暴露给用户.但是,如果需要,floodFill()可以在图像上多次调用,因此每次调用它时创建一个新的填充Mat都会使函数变慢.因此,我相信,填充是传递给用户的,因此,如果他们多次调用该函数,则填充的蒙版只会创建一次.

Normally, this would just be done inside the function and not exposed to the user. However, floodFill() is designed to be called multiple times on an image if need-be, and so creating a new padded Mat each time it's called would make the function slow. So instead, I believe, the padding is passed on to the user so that if they call the function multiple times, the padded mask is created only once.

这篇关于cv2.floodfill如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆