zoukankan      html  css  js  c++  java
  • opencv2.2 和 2.3 在MFC中显示图像

    int showImage(Mat *workImg, CDC *pDC,int flag)
    {
     //======建立位图信息=========== 
     int width, height, depth,channel;
     width = workImg->cols;;
     height = workImg->rows;
     depth = workImg->depth();
     channel = workImg->channels();

     int bits,colors,i;
     bits = (8<<(depth/2)) * channel;
     if (bits>8) colors=0;
     else colors=1<<bits;
     if (bits == 24)
     {
      bits = 32;
     }

     //位图的头
     BITMAPINFOHEADER BIH={40, 0, 0, 1, 8, BI_RGB , 0, 0, 0, 0, 0};
     //BIH.biSize = 40;
     BIH.biWidth = width;
     BIH.biHeight = height;
     //BIH.biPlanes = 1; 
     BIH.biBitCount = bits;//表示颜色用到的位数
     //BIH.biCompression = BI_RGB;
     //BIH.biSizeImage=0;//图像数据站的字节数,Specifies the size, in bytes, of the image. This may be set to zero for BI_RGB bitmaps.
     //BIH.biXPelsPerMeter = 0;
     //BIH.biYPelsPerMeter = 0;
     //BIH.biClrUsed = 0;
     //BIH.biClrImportant = 0;

     //  建立位图信息
     LPBITMAPINFO lpBmi;
     lpBmi=(LPBITMAPINFO) malloc(40+4*colors);
     memcpy(lpBmi,&BIH,40);                  //  复制位图信息头

     if (bits==8) {                          //  256 色位图
      if (flag==1) {                      //  设置灰阶调色板
       for (i=0;i<256;i++) {
        VgaColorTab[i].rgbRed=VgaColorTab[i].rgbGreen=
         VgaColorTab[i].rgbBlue=(BYTE) i;
       }
       memcpy(lpBmi->bmiColors,VgaColorTab,1024);
      }
      else if (flag==2) {                 //  设置默认调色板
       memcpy(lpBmi->bmiColors,VgaDefPal,1024);
      }
      else if (flag==3) {                 //  设置自定义调色板
       memcpy(lpBmi->bmiColors,VgaColorTab,1024);
      }
     }


     //======颠倒数据
     //======Mat 中从上往下存,而bitmap中从下往上存。  都是从左往右,并且bitmap每一个点多占一个保留字节,默认255
     
     unsigned char *m_pDibBits;//存储图像中的数据,从下向上,从左向右
     //x行 * Y列
     int x,y; 
     unsigned char * bmpdata;
     unsigned char * imgData = workImg->data;
     if (bits == 8)
     {
      m_pDibBits = new unsigned char[width * height];
      //把imgData中的第一行复制到  m_pDibBits 的最后一行,依次颠倒
      for (x=0;  x<height;  x++ )
      {
       bmpdata = m_pDibBits + (height-1-x)*width;
       memcpy(bmpdata,imgData,width);
       imgData = imgData + width;
      }
     }
     else if (bits == 32)
     {
      m_pDibBits = new unsigned char[ width * height*4 ];
      //把imgData中的第一行复制到  m_pDibBits 的最后一行,依次颠倒
      for (x=0;  x<height;  x++ )
      {
       bmpdata = m_pDibBits + (height-1-x)*width*4;
       for (y = 0; y<width; y++)
       {
        memcpy(bmpdata,imgData,3);
        bmpdata[3] = 255;
        bmpdata = bmpdata+4;
        imgData = imgData+3;
       }    
      }
     }
     
     //======显示图像
     SetStretchBltMode(pDC->m_hDC,HALFTONE);//防止自适应窗口图像显示失真
     StretchDIBits(pDC->m_hDC,0, 0, width, height, 0, 0,
      width, height, m_pDibBits, lpBmi, BI_RGB, SRCCOPY);

     delete []m_pDibBits;

     //TRACE("显示之后大小:%d",*img);
     /*namedWindow("ss",WINDOW_AUTOSIZE);
     imshow("ss",*img);*/
     return TRUE;

     
    }

    只需要在onDraw()函数中调用上述函数即可

    例如:

    Mat img;

    img = imread("lena.jpg");

    if( !img.data)

    {

      return;

    }

    showImage(&img, pDC);

  • 相关阅读:
    git安装和使用
    GitHub入门
    jmeter入门
    this关键字
    ES6函数
    代码雨
    this指向练习题
    a标签阻止默认跳转行为事件
    模板引擎的应用
    面向对象
  • 原文地址:https://www.cnblogs.com/liyanwei/p/2133658.html
Copyright © 2011-2022 走看看