Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

How to delete those border component

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),dstImg;
    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;
        }
    }
    return 0;
}

But I don't know why my code cannot be compile. This is my test image

This is my expected result

  1. What error in my code?
  2. And is there any better method can do this?

How to delete those border component

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),dstImg;
    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;
        }
    }
    return 0;
}

But I don't know why my code cannot be compile. This is my test image

This is my expected result

  1. What error in my code?
  2. And is Is there any better method can do this?

How to delete those border component

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),dstImg;
    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;
        }
    }
    return 0;
}

But I don't know why my code cannot be compile. This is my test image

This is my expected result

  1. What error in my code?
  2. Is there any better method can to do this?

How to delete those border component

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),dstImg;
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(dstImg, floodFill(srcImg, seed, Scalar(0));
        if (iRows == totalRows)
        {
            flg++;
            iRows = 0;
            strt = totalCols - 1;
        }
    }
    imshow("src", srcImg);
    waitKey();
    return 0;
}

But I don't know why my code cannot be compile. will get a poor result like

This is my test image

This is my expected result

  1. What error in my code?
  2. Is there any better method to do this?

How to delete those border component

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 test imageborder.jpg

This is my expected result

  1. What error in my code?
  2. Is there any better method to do this?

How to delete those border component

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?