Ask Your Question
0

How to remove the edge but keep the content

asked 2017-09-12 07:50:03 -0600

yode gravatar image

updated 2018-01-11 15:41:33 -0600

Suppose I have such image

I hope to delete that bold edge.Then I use this technique based on floodFill:

#include "opencv.hpp"
#include <iostream>

using namespace cv;
using namespace std;

int main()
{
    Mat mark = imread("image.jpg", IMREAD_GRAYSCALE);
    rectangle(mark, Rect(0, 0, mark.cols, mark.rows), Scalar(0));
    Mat rect = mark.clone();
    Mat tempmark = rect.clone();
    floodFill(mark, Point(0, 0), Scalar(255), 0, 10, 20, 8);
    return 0;
}

But I have to say it is not a good solution for a grayscale image.Because it will affect the contenct.As you see

This is before inpaint

This is after inpaint

Not only the edge is disappear,but the content is disappear..Is a better solution can do this?

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

answered 2017-09-12 16:59:52 -0600

i tried to use black squares indicating interested area. the code below is just a small prototype, maybe you will try to improve it. i get the following image but the code will need tunning to be more general.

image description

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

using namespace cv;
using namespace std;


int main(int argc, const char** argv)
{
    Mat img = imread(argv[1]);
    if (img.empty())
    {
        return 1;
    }

    Mat gray, thresh;
    cvtColor(img, gray, COLOR_BGR2GRAY);
    threshold(gray, thresh, 0, 255, THRESH_BINARY_INV + THRESH_OTSU);
    erode(thresh, thresh, Mat(),Point(-1,-1), 4);
    dilate(thresh, thresh, Mat(), Point(-1, -1), 4);

    vector<Point> pts;
    vector<vector<Point> > contours;

    findContours(thresh, contours, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE);

    for (size_t i = 0; i< contours.size(); i++)
    {
        Rect _boundingRect = boundingRect(contours[i]);
        if ((_boundingRect.width < _boundingRect.height * 3) & (_boundingRect.height < _boundingRect.width * 3))
        {
            pts.push_back((_boundingRect.tl()+ _boundingRect.br()) /2 );
        }
    }

    vector<Point> hull;
    convexHull(pts, hull, false);
    Rect r = boundingRect(hull);
    imwrite("result.png", img(r));
    imshow("result", img(r));
    waitKey(0);

}
edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2017-09-12 07:50:03 -0600

Seen: 734 times

Last updated: Sep 12 '17