视差图转换伪伪彩色图和彩虹图

#include 
#include 
#include 
#include 

using namespace std;
using namespace cv;

int main(int argc, char** argv[])
{
     cvNamedWindow("left", CV_WINDOW_AUTOSIZE);
     cvNamedWindow("right", CV_WINDOW_AUTOSIZE);

     CvCapture* capture1;
     CvCapture* capture2;
         if (argc==1)
         {
             capture1 = cvCreateCameraCapture( 1 );
             capture2 = cvCreateCameraCapture( 0 );
         }

     IplImage* img1;
     IplImage* img2;

     while(1)
     {
         img1 = cvQueryFrame( capture1 );
         img2 = cvQueryFrame( capture2 );
         if( !(img1&&img2) )
              break;
          cvShowImage( "left", img1 );
          cvShowImage( "right", img2 );

    cv::StereoSGBM sgbm;
    int SADWindowSize = 5;
    sgbm.preFilterCap = 63;
    sgbm.SADWindowSize = SADWindowSize > 0 ? SADWindowSize : 3;

    int cn = img1->nChannels;
    int numberOfDisparities=128;

    sgbm.P1 = 8*cn*sgbm.SADWindowSize*sgbm.SADWindowSize;
    sgbm.P2 = 32*cn*sgbm.SADWindowSize*sgbm.SADWindowSize;
    sgbm.minDisparity = 0;
    sgbm.numberOfDisparities = numberOfDisparities;
    sgbm.uniquenessRatio = 10;
    sgbm.speckleWindowSize = 100;
    sgbm.speckleRange = 10;
    sgbm.disp12MaxDiff = 1;

    Mat disp, img;
    int64 t = getTickCount();

    sgbm((Mat)img1, (Mat)img2, disp);
    t = getTickCount() - t;
    cout<<"Time elapsed:"<(y,x);
                   img_pseudocolor.at(y,x)[0] = abs(255-tmp); //blue
                   img_pseudocolor.at(y,x)[1] = abs(127-tmp); //green
                    img_pseudocolor.at(y,x)[2] = abs( 0-tmp); //red
            }
     }
     namedWindow("img_pseudocolor");
     imshow("img_pseudocolor", img_pseudocolor);

     Mat img_color(img.rows, img.cols, CV_8UC3);//构造RGB图像
     #define IMG_B(img,y,x) img.at(y,x)[0]
     #define IMG_G(img,y,x) img.at(y,x)[1]
     #define IMG_R(img,y,x) img.at(y,x)[2]
     uchar tmp2=0;
     for (int y=0;y(y,x);
                    if (tmp2 <= 51)
                    {
                           IMG_B(img_color,y,x) = 255;
                           IMG_G(img_color,y,x) = tmp2*5;
                           IMG_R(img_color,y,x) = 0;
                    }
                    else if (tmp2 <= 102)
                    {
                           tmp2-=51;
                           IMG_B(img_color,y,x) = 255-tmp2*5;
                           IMG_G(img_color,y,x) = 255;
                           IMG_R(img_color,y,x) = 0;
                     }
                     else if (tmp2 <= 153)
                     {
                            tmp2-=102;
                            IMG_B(img_color,y,x) = 0;
                            IMG_G(img_color,y,x) = 255;
                            IMG_R(img_color,y,x) = tmp2*5;
                     }
                    else if (tmp2 <= 204)
                     {
                             tmp2-=153;
                             IMG_B(img_color,y,x) = 0;
                             IMG_G(img_color,y,x) = 255-uchar(128.0*tmp2/51.0+0.5);
                             IMG_R(img_color,y,x) = 255;
                     }
                     else
                      {
                             tmp2-=204;
                             IMG_B(img_color,y,x) = 0;
                             IMG_G(img_color,y,x) = 127-uchar(127.0*tmp2/51.0+0.5);
                             IMG_R(img_color,y,x) = 255;
                       }
                }
          }
     namedWindow("img_ rainbowcolor");
     imshow("img_ rainbowcolor", img_color);

    waitKey(30);
    }

    cvReleaseCapture( &capture1 );
    cvReleaseCapture( &capture2 );
    cvDestroyWindow( "left" );
    cvDestroyWindow( "right" );
    cvDestroyWindow( "disparity" );
}

视差图转换伪伪彩色图和彩虹图_第1张图片

你可能感兴趣的:(opencv)