Image edge smoothing with opencv

后端 未结 2 1334
清歌不尽
清歌不尽 2020-12-01 01:47

I am trying to smooth output image edges using opencv framework, I am trying following steps. Steps took from here https://stackoverflow.com/a/17175381/790842



        
相关标签:
2条回答
  • 2020-12-01 02:29

    I have followed the following steps to smooth the edges of the Foreground I got from GrabCut.

    1. Create a binary image from the mask I got from GrabCut.
    2. Find the contour of the binary image.
    3. Create an Edge Mask by drawing the contour points. It gives the boundary edges of the Foreground image I got from GrabCut.
    4. Then follow the steps define in https://stackoverflow.com/a/17175381/790842
    0 讨论(0)
  • 2020-12-01 02:30

    Do you want to get something like this?

    enter image description here

    If yes, then here is the code:

    #include <iostream>
    #include <vector>
    #include <string>
    #include <fstream>
    #include <opencv2/opencv.hpp>
    
    using namespace cv;
    using namespace std;
    
    int main(int argc, char **argv)
    {
        cv::namedWindow("result");
        Mat img=imread("TestImg.png");
        Mat whole_image=imread("D:\\ImagesForTest\\lena.jpg");
        whole_image.convertTo(whole_image,CV_32FC3,1.0/255.0);
        cv::resize(whole_image,whole_image,img.size());
        img.convertTo(img,CV_32FC3,1.0/255.0);
    
        Mat bg=Mat(img.size(),CV_32FC3);
        bg=Scalar(1.0,1.0,1.0);
    
        // Prepare mask
        Mat mask;
        Mat img_gray;
        cv::cvtColor(img,img_gray,cv::COLOR_BGR2GRAY);
        img_gray.convertTo(mask,CV_32FC1);
        threshold(1.0-mask,mask,0.9,1.0,cv::THRESH_BINARY_INV);
    
        cv::GaussianBlur(mask,mask,Size(21,21),11.0);
        imshow("result",mask);
        cv::waitKey(0);
    
    
            // Reget the image fragment with smoothed mask
        Mat res;
    
        vector<Mat> ch_img(3);
        vector<Mat> ch_bg(3);
        cv::split(whole_image,ch_img);
        cv::split(bg,ch_bg);
        ch_img[0]=ch_img[0].mul(mask)+ch_bg[0].mul(1.0-mask);
        ch_img[1]=ch_img[1].mul(mask)+ch_bg[1].mul(1.0-mask);
        ch_img[2]=ch_img[2].mul(mask)+ch_bg[2].mul(1.0-mask);
        cv::merge(ch_img,res);
        cv::merge(ch_bg,bg);
    
        imshow("result",res);
        cv::waitKey(0);
        cv::destroyAllWindows();
    }
    

    And I think this link will be interestiong for you too: Poisson Blending

    0 讨论(0)
提交回复
热议问题