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

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/mengxiang2425/article/details/82560270
#include <highgui.h>
#include <cv.h>
#include <cxcore.h>
#include <iostream>

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:"<<t*1000/getTickFrequency()<<endl;
    disp.convertTo(img, CV_8U, 255/(numberOfDisparities*16.));

    cvNamedWindow("disparity", CV_WINDOW_AUTOSIZE);
    imshow("disparity", img);

    Mat img_pseudocolor(img.rows, img.cols, CV_8UC3);//构造RGB图像,参数CV_8UC3教程文档里面有讲解

     int tmp=0;
     for (int y=0;y<img.rows;y++)//转为伪彩色图像的具体算法
     {
            for (int x=0;x<img.cols;x++)
            {
                   tmp = img.at<unsigned char>(y,x);
                   img_pseudocolor.at<Vec3b>(y,x)[0] = abs(255-tmp); //blue
                   img_pseudocolor.at<Vec3b>(y,x)[1] = abs(127-tmp); //green
                    img_pseudocolor.at<Vec3b>(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<Vec3b>(y,x)[0]
     #define IMG_G(img,y,x) img.at<Vec3b>(y,x)[1]
     #define IMG_R(img,y,x) img.at<Vec3b>(y,x)[2]
     uchar tmp2=0;
     for (int y=0;y<img.rows;y++)//转为彩虹图的具体算法,主要思路是把灰度图对应的0~255的数值分别转换成彩虹色:红、橙、黄、绿、青、蓝。
      {
             for (int x=0;x<img.cols;x++)
             {
                    tmp2 = img.at<uchar>(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" );
}

展开阅读全文

没有更多推荐了,返回首页