Ask Your Question
1

How to delete those border component

asked 2017-09-07 11:13:07 -0600

yode gravatar image

updated 2017-09-07 13:13:58 -0600

If you are in Matlab,there is a convenient function imclearborder. But I don't very fimilar the OpenCV. So I writer such code

#include<opencv.hpp>
using namespace cv;

int main() {
    Mat srcImg = imread("border.jpg",IMREAD_GRAYSCALE);
    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(srcImg, seed, Scalar(0));
        if (iRows == totalRows)
        {
            flg++;
            iRows = 0;
            strt = totalCols - 1;
        }
    }
    imshow("src", srcImg);
    waitKey();
    return 0;
}

But I will get a poor result like

This is my border.jpg

This is my expected result

  1. What error in my code?
  2. Is there any better method to do this?
edit retag flag offensive close merge delete

1 answer

Sort by » oldest newest most voted
2

answered 2017-09-07 12:16:13 -0600

updated 2017-09-07 12:42:02 -0600

try this, it has a simpler way to do what your code does. but there is still a dot on result image when using floodFill() method.

image description

so you should use findContours() to get rid of it.

image description

#include<opencv2/core.hpp>
#include<opencv2/imgproc.hpp>
#include<opencv2/highgui.hpp>

using namespace cv;
using namespace std;

int main() {
    Mat srcImg = imread("test.png", IMREAD_GRAYSCALE);
    Mat dstImg = srcImg > 127;

    rectangle(dstImg, Rect(0, 0, dstImg.cols, dstImg.rows), Scalar(255));
    floodFill(dstImg, Point(0, 0), Scalar(0));

    imshow("srcImg", srcImg);
    imshow("dstImg", dstImg);

    srcImg = srcImg > 127;
    vector<vector<Point> > contours;
    findContours(srcImg, contours, RETR_LIST, CHAIN_APPROX_SIMPLE);

    for (size_t i = 0; i < contours.size(); i++)
    {
        Rect bounding_rect = boundingRect(contours[i]);
        Rect test_rect = bounding_rect & Rect(1, 1, dstImg.cols - 2, dstImg.rows - 2);
        if (bounding_rect != test_rect)
        {
            drawContours(srcImg, contours, (int)i, Scalar(0),-1);
        }
    }
    imshow("Result using findContours", srcImg);
    waitKey();
    return 0;
}
edit flag offensive delete link more

Comments

Thanks very very much again. BTW,could you tell me what meaning of your srcImg > 127

yode gravatar imageyode ( 2017-09-07 12:41:57 -0600 )edit

you can find the answer in documentation

Comparison: A cmpop B, A cmpop alpha, alpha cmpop A, where cmpop is one of >, >=, ==, !=, <=, <. The result of comparison is an 8-bit single channel mask whose elements are set to 255 (if the particular element or pair of elements satisfy the condition) or 0.

sturkmen gravatar imagesturkmen ( 2017-09-07 12:55:00 -0600 )edit

Thanks your link. I know that now.

yode gravatar imageyode ( 2017-09-07 12:56:37 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2017-09-07 11:13:07 -0600

Seen: 3,012 times

Last updated: Sep 07 '17