opencv

来源:互联网 发布:java lambda filter 编辑:程序博客网 时间:2024/05/22 06:33
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"


#include <iostream>
#include <fstream>
#include <string>

using namespace cv;
using namespace std;

#define POINTNUM 12
#define WAITTIME 2000
static void help()
{
    cout << "This program is used to replace the source image teeth area with the destination image\n"
            "Call:\n"
            "./dsd_sow datafile sourceimage destimage\n"
            "Using OpenCV version %s\n" << CV_VERSION << "\n" << endl;
}



static void getBinMask( const Mat& comMask, Mat& binMask )
{
    if( comMask.empty() || comMask.type()!=CV_8UC1 )
        CV_Error( CV_StsBadArg, "comMask is empty or has incorrect type (not CV_8UC1)" );
    if( binMask.empty() || binMask.rows!=comMask.rows || binMask.cols!=comMask.cols )
        binMask.create( comMask.size(), CV_8UC1 );
    binMask = comMask & 1;
}
static void matchAndFill(Mat& srcMat, Mat& dstMat, Mat& templ)
{
    int cols = srcMat.cols - templ.cols + 1;
    int rows = srcMat.rows - templ.rows + 1;
    Mat result;
    result.create(cols, rows, CV_32FC1 );
    matchTemplate( srcMat, templ, result, 0);
    normalize( result, result, 0, 1, NORM_MINMAX, -1, Mat() );
    double minVal; double maxVal; Point minLoc; Point maxLoc;
    minMaxLoc( result, &minVal, &maxVal, &minLoc, &maxLoc, Mat() );
    



    int duration = dstMat.rows / 11;
    int durationArr[12];
    double rateArr[11]={0,0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1};
    for ( int index = 0; index < 11; index ++)
    {
        durationArr[index] = minLoc.y + index * duration;
    }
    durationArr[11] = minLoc.y + dstMat.rows;

    int nr= srcMat.rows;
    int nc= srcMat.cols * srcMat.channels();  
    for (int j=0; j<nr; j++) {  
        if ( j>= minLoc.y && j< (minLoc.y + templ.rows))
        {
            double alpha;
            for ( int index = 0; index < 11; index ++)
            {
                if( durationArr[index]<= j && j< durationArr[index+1])
                {
                    alpha = rateArr[index];
                    break;
                }
            }
            uchar* data= srcMat.ptr<uchar>(j);
            uchar* data1 = dstMat.ptr<uchar>(j-minLoc.y);
            for (int i=0; i<nc; i++) {  
                if ( i >= (minLoc.x*srcMat.channels()) && i< ((minLoc.x*srcMat.channels()) + templ.cols * srcMat.channels()) )
                    if ( data1[i-(minLoc.x*srcMat.channels())] != 0 )
                    {
                        char tmpval = data1[i-(minLoc.x*srcMat.channels())]*(1-alpha)+data[i]*alpha;
                        data[i]= tmpval;
                        
                    }
            }                    
        }
    }


}

static void doMask(Mat& srcMat, Mat& dstMat)
{

    int nr= srcMat.rows;
    int nc= srcMat.cols * srcMat.channels();
    for (int j=0; j<nr; j++) {
        uchar* data= srcMat.ptr<uchar>(j);
        uchar* data1 = dstMat.ptr<uchar>(j);
        for (int i=0; i<nc; i++) {
            if( data1[i] != 0 )
            {
                data[i]= data1[i];
            }
        }
    }
                




}


static void doBlur(Mat& srcMat, Mat& dstMat)
{
/*
    int nr= srcMat.rows;
        int nc= srcMat.cols * srcMat.channels();
        for (int j=0; j<nr; j++) {
                uchar* data= srcMat.ptr<uchar>(j);
                for (int i=0; i<nc; i++) {
                        if( data[i] < 2 )
                        {
                                data[i]= 0;
                        }
                }
        }
*/

    Mat tmp,mask;
    cvtColor(srcMat,tmp, CV_BGR2GRAY);
        int nr= tmp.rows;
        int nc= tmp.cols * tmp.channels();
        for (int j=0; j<nr; j++) {
                uchar* data= tmp.ptr<uchar>(j);
                for (int i=0; i<nc; i++) {
                        if( data[i] < 2 )
                        {
                                data[i]= 0;
                        }
                }
        }

    getBinMask( tmp, mask);
    srcMat.copyTo(dstMat,mask);

    
    



}




int main( int argc, char** argv )
{
    if(argc != 4)
    {
        help();
        return 1;
    }

    const char* dataFile = argv[1];
    const char* srcImgName = argv[2];
    const char* dstImgName = argv[3];
    
    
    Mat bgdModel, fgdModel;

    Mat mask;


    ifstream fin(dataFile);  
    string  s;  
    int arr[POINTNUM * 2];
    int arrCount = 0;
    while ( fin >> s )
    {
        cout <<  "Read from file: "  << s << endl;  
        if ( arrCount < (POINTNUM * 2) )
        {
            sscanf(s.c_str(),"%d",&(arr[arrCount]));
            arrCount++;
        }
    }

    for( int i=0; i < (POINTNUM * 2); i++)
    {
        cout << arr[i]<< endl;
    }


    namedWindow("display", WINDOW_AUTOSIZE);
    

    // read src image from file
    Mat srcImg = imread(srcImgName);
    if(srcImg.empty())
    {
        fprintf(stderr, "Can not load image %s\n", srcImgName);
        return -1;
    }
    
        imshow("display", srcImg);
    waitKey(WAITTIME);

    Mat dstImg = imread(dstImgName);
    if(dstImg.empty())
    {
        fprintf(stderr, "Can not load image %s\n", dstImgName);
        return -1;
    }
    imshow("display", dstImg);
    waitKey(WAITTIME);


    Point2f point1(arr[1],arr[0]),point2(arr[3],arr[2]);
    Rect rect(point1,point2);
    Point point3(arr[5],arr[4]),point4(arr[7],arr[6]),point5(arr[9],arr[8]),point6(arr[11],arr[10]),point7(arr[13],arr[12]),point8(arr[15],arr[14]);
    Point point9(arr[17],arr[16]),point10(arr[19],arr[18]);
    Point point11(arr[21],arr[20]),point12(arr[23],arr[22]);
    int srcWidth = arr[19] - arr[17];
    cout << srcWidth << endl;
    int dstWidth = arr[23] - arr[21];
    cout << dstWidth << endl;
    double rate = (double)srcWidth / (double)dstWidth;
    cout << rate << endl;






    Mat srcImg1;
    srcImg.copyTo(srcImg1);
    line(srcImg1,point9, point10, Scalar(255,0,0),5);
        imshow("display", srcImg1);
        waitKey(WAITTIME);





    Mat dstImg1;
    dstImg.copyTo(dstImg1);
    line(dstImg1, point11, point12, Scalar(255,0,0),5);
    imshow("display",dstImg1);
    waitKey(WAITTIME);








    // resize dst image for use
        Size size;
        size.width = dstImg.cols * rate;
        size.height = dstImg.rows * rate;
        Mat resizedstImg(size, CV_8UC3, Scalar(255,255,255));
        resize(dstImg, resizedstImg,resizedstImg.size(), 0, 0,  INTER_AREA);
        imshow("display", resizedstImg);
    waitKey(WAITTIME);
    







    //rectangle(srcImg, rect, Scalar(255,0,0));
    Mat cannySrcImg;
    srcImg.copyTo(cannySrcImg);
    line(cannySrcImg, point3, point4, Scalar(255,255,255),5);
    line(cannySrcImg, point4, point5, Scalar(255,255,255),5);
    line(cannySrcImg, point5, point6, Scalar(255,255,255),5);
    line(cannySrcImg, point6, point7, Scalar(255,255,255),5);
    line(cannySrcImg, point7, point8, Scalar(255,255,255),5);
    line(cannySrcImg, point8, point3, Scalar(255,255,255),5);
    imshow("display",cannySrcImg);
    waitKey(WAITTIME);







    
    Mat cannyImg;
    vector<vector<Point> > contours;  
    vector<Vec4i> hierarchy;
    Canny(cannySrcImg, cannyImg, 300, 900, 3);
    imshow("display",cannyImg);
    waitKey(WAITTIME);











    findContours(cannyImg, contours, hierarchy, CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE, Point(0, 0));
    double minarea = 1000000;
    int maxAreaIdx = 0;


    for (int i = 0; i<contours.size(); i++)
    {
        double tmparea = fabs(contourArea(contours[i]));
        if (tmparea< minarea)
        {
            contours.erase(contours.begin() + i);
            continue;


        }
    }
    Mat dst(cannyImg.size(), CV_8UC3, Scalar(255,255,255));
    for (int i = 0; i< contours.size(); i++)
    {
        Scalar color(0,0,0);
        drawContours(dst, contours, i, color, CV_FILLED, 8, hierarchy, 0, Point());
    }
    imshow("display",dst);
        waitKey(WAITTIME);









    Mat cannyMask, cannyBinMask;
    cvtColor(dst,cannyMask, CV_BGR2GRAY);
    imshow("display",cannyMask);
        waitKey(WAITTIME);






    
    getBinMask( cannyMask, cannyBinMask);
    imshow("display",cannyBinMask);
        waitKey(WAITTIME);






    srcImg.copyTo(dst,cannyBinMask);
    imshow("display",dst);
        waitKey(WAITTIME);






    
    int dst_cols = resizedstImg.cols;
    int dst_rows = resizedstImg.rows;
    int wideth = resizedstImg.cols - 10;
    int highth = resizedstImg.rows - 10;
    Rect dstRect(10,10,wideth,highth);
    Mat dstBinMask;
    Mat dstRes(resizedstImg.size(), CV_8UC3, Scalar(0,0,0));
    //grabCut( resizedstImg,mask, dstRect, bgdModel, fgdModel, 1, GC_INIT_WITH_RECT );
    //getBinMask( mask, dstBinMask);
    //resizedstImg.copyTo(dstRes, dstBinMask);


    //Mat blurDstImg(resizedstImg.size(), CV_8UC3, Scalar(0,0,0));
    //doBlur(resizedstImg,blurDstImg);
    matchAndFill(srcImg,resizedstImg,resizedstImg);    




    doMask(srcImg,dst);











    //grabCut( srcImg, mask, rect, bgdModel, fgdModel, 5, GC_INIT_WITH_RECT );

    //Mat res;
    //Mat binMask;
    
    //getBinMask( mask, binMask );
        //srcImg.copyTo( res, binMask );

    imshow("display",srcImg);
    waitKey();
    return 0;

}

0 0
原创粉丝点击