如何使用Opencv在Python 2.7中删除边框组件 [英] How to remove border components in Python 2.7 using Opencv

查看:157
本文介绍了如何使用Opencv在Python 2.7中删除边框组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要删除那些触及图像边框的组件.

I want to remove the components which are touching the border of the image.

我正在使用OpenCV 2.4.10和Python 2.7.

I'm using OpenCV 2.4.10 and Python 2.7.

我已经完成了HSV转换和图像的THRESHOLD_BINARY,接下来我要删除触摸图像边缘的组件(对象).

I have done HSV conversion and THRESHOLD_BINARY of the image, next I want to remove the components (objects) which are touching to border of the image.

在Matlab中对此进行了解释- http://blogs.mathworks.com/steve/2007/09/04/clearing-border-components/

It was explained in Matlab here - http://blogs.mathworks.com/steve/2007/09/04/clearing-border-components/

但是我想使用OpenCV在Python中完成.

but I want to do in Python using OpenCV.

请向我解释代码.

推荐答案

openCV中没有直接方法可以做到这一点.您可以使用方法 floodFill 编写函数,然后循环将边界像素作为种子点.

There is no direct method in openCV to do that. You can write a function using the method floodFill and loop over for border pixels as seed points.

floodFill(dstImg,seed,Scalar (0));

其中:

dstImg :输出已删除边框.

种子:[(x,y)点]所有边界坐标

seed : [(x,y) points] All the border co-ordinates

标量(0):如果找到朝向种子点的连接区域,则要填充的颜色.因此,根据您的情况将(0)填充为黑色.

Scalar(0) : The color to be filled if a connected region towards a seed point is found. Hence (0) as your case is to fill it as black.

示例:

int totalRows = srcImg.rows;
int totalCols = srcImg.cols;
int strt = 0, flg = 0;
int iRows = 0, jCols = 0;
while (iRows < srcImg.rows)
{
    if (flg ==1)
        totalRows = -1;
    Point seed(strt,iRows);     
    iRows++;
    floodFill(dstImg,seed,Scalar (0));
    if (iRows == totalRows)
    {
        flg++;
        iRows = 0;
        strt = totalCols - 1;
    }
}       

类似地,对列进行修改. 希望对您有帮助.

Similarly do modify it for columns. Hope It helps.

这篇关于如何使用Opencv在Python 2.7中删除边框组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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