图像处理之图像拼接四

1 亮度校正的代码

实际上就是把图像im1 和图像im2 的重合部分转换到hsv颜色空间中,计算两个重合部分亮度和的比值,用这个比值去校正图像im2

void colorjust(cv::Mat &im1,cv::Mat &im2, int stitchlength)
{
    int soureheight=im1.rows;
    int sourewidth=im1.cols;
    int xsum=stitchlength;
    /////////////////////////////////////////
    int channle = im1.channels();
    float v2=0;
    for (int jx=0;jx<soureheight;jx++)
    {
        uchar *data =im2.ptr<uchar>(jx);
        for (int iy=0;iy<xsum;iy++)
        {
            float I2=0.59*data[iy*channle+0]+0.11*data[iy*channle+1]+0.3*data[iy*channle+2];
            v2=v2+I2;
        }
    }

    float v1=0;
    for (int jx=0;jx<soureheight;jx++)
    {
        uchar *data =im1.ptr<uchar>(jx);
        for (int iy=sourewidth-xsum;iy<sourewidth;iy++)
        {
            float I1=0.59*data[iy*channle+0]+0.11*data[iy*channle+1]+0.3*data[iy*channle+2];
            v1=v1+I1;
        }
    }
    float k=v1/v2;
    for (int jx=0;jx<soureheight;jx++)
    {
        uchar *data =im2.ptr<uchar>(jx);
        for (int iy=0;iy<sourewidth;iy++)
        {
            data[iy*channle+0]=k*data[iy*channle+0];
            data[iy*channle+1]=k*data[iy*channle+1];
            data[iy*channle+2]=k*data[iy*channle+2];
        }
    }
}

2  弄出背景,转到lab空间,分离处L,根据这个L建立一个模板,求解L到L模板的映射。来一个图像,将这个图像分离处L,利用映射去校正,然后回到RGB空间中

void newbackground(IplImage *image1, cv::Mat& listtable)
{

    //读取背景图像,分离出l分量放入testimg
    IplImage *testimg=cvCreateImage(cvGetSize(image1),8,1);
    IplImage *newtestimg=cvCreateImage(cvGetSize(image1),8,1);
    cvZero(testimg);
    cvZero(newtestimg);
    cv::Mat ming1(image1,0); 
    cv::cvtColor(ming1, ming1, CV_BGR2Lab);
    uchar *data1;
    int channle = ming1.channels();
    for(int x=0;x<image1->height;x++)
    {
        data1 =ming1.ptr<uchar>(x);
        for(int y=0;y<image1->width;y++)
        {
            BYTE vb=ming1.at<cv::Vec3b>(x,y)[0];
            BYTE vg=ming1.at<cv::Vec3b>(x,y)[1];
            BYTE vr=ming1.at<cv::Vec3b>(x,y)[2];
            float vm11=vb;
            cvSet2D(testimg,x,y,cvScalar(vm11));

        }
    }
    //背景建模
    int time=4;
    cv::Mat mingt(testimg,0); 
    cv::Mat mingnt(newtestimg,0); 
    uchar *datan=mingt.ptr<uchar>(testimg->height/2);
    int centervalue=datan[testimg->width/2];
    for (int i=1;i<=time;i++)
    {
        if (i==1)
        {
            for(int x=0;x<testimg->height;x++)
            {
                uchar *datant=mingnt.ptr<uchar>(x);
                for(int y=0;y<testimg->width;y++)
                {
                    datant[y]=centervalue-3;
                }
            }
        }else if (i==2)
        {
            for(int x=testimg->height/4;x<testimg->height*0.75;x++)
            {
                uchar *datant=mingnt.ptr<uchar>(x);
                for(int y=testimg->width/4;y<testimg->width*0.75;y++)
                {
                    datant[y]=centervalue-2;
                }
            }
        }else if (i==3)
        {
            for(int x=3*testimg->height/8;x<5*testimg->height/8;x++)
            {
                uchar *datant=mingnt.ptr<uchar>(x);
                for(int y=3*testimg->width/8;y<5*testimg->width/8;y++)
                {
                    datant[y]=centervalue-1;
                }
            }
        }else if (i==4)
        {
            for(int x=7*testimg->height/16;x<9*testimg->height/16;x++)
            {
                uchar *datant=mingnt.ptr<uchar>(x);
                for(int y=7*testimg->width/16;y<9*testimg->width/16;y++)
                {
                    datant[y]=centervalue;
                }
            }
        }
    }
    //计算list 
    for(int x=0;x<testimg->height;x++)
    {
        uchar *datat=mingt.ptr<uchar>(x);
        uchar *datant=mingnt.ptr<uchar>(x);
        for(int y=0;y<testimg->width;y++)
        {
            if (datat[y]!=0)
            {
                float value=(float)datant[y]/(float)datat[y];
                listtable.ptr<float>(x)[y]=value;
            }

        }
    }
    //释放
    cvReleaseImage(&testimg);
    cvReleaseImage(&newtestimg);

}

 

posted on 2016-11-22 17:36  mitutao  阅读(559)  评论(0编辑  收藏  举报

导航