图像处理之图像拼接

    图像拼接是图像处理中最为常见的一个功能,在不考虑旋转的情况下,也就是需要求取拼接亮就行了,然而当光源是点光源时,往往成像都是高斯性质,

这样会导致中间亮,两边暗或者四周暗,在这种情况下拼接,图像会存在很明显的拼接痕迹。

    因此,本文针对上述拼接痕迹,设计了一种线性平滑的方法。

上代码:

 1 CString leftimagepath;
 2                 CString rightimagepath;
 3                 leftimagepath.Format("0-%d-%d.jpg",i,j);
 4                 leftimagepath=pathIn+leftimagepath;
 5                 rightimagepath.Format("0-%d-%d.jpg",i,j+1);
 6                 rightimagepath=pathIn+rightimagepath;
 7                 IplImage* leftimg=cvLoadImage(leftimagepath,-1);
 8                 IplImage* rightimg=cvLoadImage(rightimagepath,-1);
 9                 sourewidth=rightimg->width;
10                 soureheight=rightimg->height;
11                 IplImage* rightresult=cvCreateImage(cvSize(sourewidth*2,soureheight),8,rightimg->nChannels);
12                 cvZero(rightresult);
13                 cvSetImageROI(rightresult,cvRect(sourewidth-xsum,0,sourewidth,soureheight));
14                 cvCopy(rightimg,rightresult);
15                 cvResetImageROI(rightresult);
16                 IplImage* result=cvCreateImage(cvSize(sourewidth*2,soureheight),8,rightimg->nChannels);
17                 cv::Mat imleft(leftimg,0); //左边的图像
18                 cv::Mat imright(rightresult,0); //右边的图像,但是右边的有效区域开始于(左边的图像大小减去重合量)
19                 cv::Mat dst(result,0); //结果图像
20                 int channle = imright.channels();
21                 for(int jx = 0;jx < imright.rows;jx++)
22                 {   
23                     // 行定位
24                     uchar *data =dst.ptr<uchar>(jx);                // 最终要得到的拼接图
25                     uchar *data2 = imleft.ptr<uchar>(jx);        // 左图
26                     uchar *datar = imright.ptr<uchar>(jx);        // 单应变换后(右图)
27                     for (int iy=0,q =0;iy < imright.cols;iy++)
28                     {
29 
30                         if (iy<imleft.cols && datar[iy*channle+0]==0 && datar[iy*channle+1]==0 && datar[iy*channle+2]==0)
31                         {    //只在左图部分
32                             data[iy*channle+0] = data2[iy*channle+0];
33                             data[iy*channle+1] = data2[iy*channle+1];
34                             data[iy*channle+2] = data2[iy*channle+2];
35                             q++;                                                 // 进入右图的标记
36                         }
37                         else if(iy<imleft.cols )
38                         {    //在左图也在右图部分
39                             float overlapwidth = float(imleft.cols - q);         // 重叠区域的宽度
40                             float overlapratio = float(iy-q);                     // 当前位置的重叠比例
41                             float thr = overlapratio/overlapwidth;                 // 阈值
42                             data[iy*channle+0] = (1-thr) * data2[iy*channle+0] + thr * datar[iy*channle+0];
43                             data[iy*channle+1] = (1-thr) * data2[iy*channle+1] + thr * datar[iy*channle+1];
44                             data[iy*channle+2] = (1-thr) * data2[iy*channle+2] + thr * datar[iy*channle+2];
45                         }
46                         else if(iy>=imleft.cols)
47                         {    //只在右图部分
48                             data[iy*channle+0] = datar[iy*channle+0];
49                             data[iy*channle+1] = datar[iy*channle+1];
50                             data[iy*channle+2] = datar[iy*channle+2];
51                         }
52                     }//iy
53                  }//jx
54                     //保存图像
55                     //保存当前拼接后的
56                     cvSetImageROI(result,cvRect(0,0,rightimg->width-xsum,rightimg->height));
57                     CString saveleftimagepath;
58                     saveleftimagepath.Format("0-%d-%d.jpg",i,j);
59                     saveleftimagepath=pathIn+saveleftimagepath;
60                     cvSaveImage(saveleftimagepath,result);
61                     cvResetImageROI(result);
62                     //覆盖原始图像
63                     CString saverigimagepath;
64                     saverigimagepath.Format("0-%d-%d.jpg",i,j+1);
65                     saverigimagepath=pathIn+saverigimagepath;
66                     cvSetImageROI(result,cvRect(rightimg->width-xsum,0,rightimg->width,rightimg->height));
67                     cvSaveImage(saverigimagepath,result);
68                     cvResetImageROI(result);
69                     /*cvNamedWindow("rs",0);
70                     cvShowImage("rs",result);
71                     cvWaitKey(0);*/

没有采用上述拼接结果:

 采用上述过渡算法的结果:

原文地址:https://www.cnblogs.com/love6tao/p/5974185.html