Linux下Opencv+Qt4

原创 2007年09月16日 20:30:00
Qt开发的程序一般都要借助qmake生成makefile文件。因此为了加入opencv库就要修改.pro文件,下面是Linux下该文件的配置。(增加的部分)

INCLUDEPATH += . /usr/local/include/opencv
LIBS 
+= /usr/local/lib/libcv.so /
   
    /usr/local/lib/libcvaux.so /
   
    /usr/local/lib/libcxcore.so /
   
    /usr/local/lib/libhighgui.so /
   
    /usr/local/lib/libml.so

Opencv中通过摄像头捕捉到的每帧图像的数据结构是IplImage类型的,要把它显示到Qt窗口中就需要把它转化为QImage类型的图像。
#include <QVector>
#include 
<cstring>

QImage MyThread::IplImageToQImage(
const IplImage * iplImage,double mini, double maxi)
{
    uchar 
*qImageBuffer = NULL;

        
int width = iplImage->width;

/* Note here that OpenCV image is stored so that each lined is
32-bits aligned thus
* explaining the necessity to "skip" the few last bytes of each
line of OpenCV image buffer.
*/

        
int widthStep = iplImage->widthStep;
        
int height = iplImage->height;

        
switch (iplImage->depth)
        
{
            
case IPL_DEPTH_8U:
            
if(iplImage->nChannels == 1)
            
{
            
/* OpenCV image is stored with one byte grey pixel. We convert it
            to an 8 bit depth QImage.
            
*/

   
                qImageBuffer 
= (uchar *) malloc(width*height*sizeof(uchar));
                uchar 
*QImagePtr = qImageBuffer;
                
const uchar *iplImagePtr = (const uchar *) iplImage->imageData;
   
                
for(int y = 0; y < height; y++)
                
{
                    
// Copy line by line
                        memcpy(QImagePtr, iplImagePtr, width);
                        QImagePtr 
+= width;
                        iplImagePtr 
+= widthStep;
                }

   
            }

            
else if(iplImage->nChannels == 3)
            
{
                    
/* OpenCV image is stored with 3 byte color pixels (3 channels).
                    We convert it to a 32 bit depth QImage.
                    
*/

                    qImageBuffer 
= (uchar *) malloc(width*height*4*sizeof(uchar));
                    uchar 
*QImagePtr = qImageBuffer;
                    
const uchar *iplImagePtr = (const uchar *) iplImage->imageData;
                    
for(int y = 0; y < height; y++)
                    
{
                        
for (int x = 0; x < width; x++)
                        
{
                                
// We cannot help but copy manually.
                                QImagePtr[0= iplImagePtr[0];
                                QImagePtr[
1= iplImagePtr[1];
                                QImagePtr[
2= iplImagePtr[2];
                                QImagePtr[
3= 0;
       
                                QImagePtr 
+= 4;
                                iplImagePtr 
+= 3;
                        }

                        iplImagePtr 
+= widthStep-3*width;
                    }
    
   
            }

            
else
            
{
                    qDebug(
"IplImageToQImage: image format is not supported : depth=8U and %d channels ", iplImage->nChannels);
            }

            
break;
            
case IPL_DEPTH_16U:
            
if(iplImage->nChannels == 1)
            
{
            
/* OpenCV image is stored with 2 bytes grey pixel. We convert it
            to an 8 bit depth QImage.
            
*/

                    qImageBuffer 
= (uchar *) malloc(width*height*sizeof(uchar));
                    uchar 
*QImagePtr = qImageBuffer;
                    
//const uint16_t *iplImagePtr = (const uint16_t *);
            const unsigned int *iplImagePtr = (const unsigned int *)iplImage->imageData;
                    
for (int y = 0; y < height; y++)
                    
{
                        
for (int x = 0; x < width; x++)
                        
{
                        
// We take only the highest part of the 16 bit value. It is
                        
//similar to dividing by 256.
                            *QImagePtr++ = ((*iplImagePtr++>> 8);
                        }

                        iplImagePtr 
+= widthStep/sizeof(unsigned int)-width;
                    }

            }

            
else
            
{
                    qDebug(
"IplImageToQImage: image format is not supported : depth=16U and %d channels ", iplImage->nChannels);
   
            }

            
break;
            
case IPL_DEPTH_32F:
             
if(iplImage->nChannels == 1)
             
{
            
/* OpenCV image is stored with float (4 bytes) grey pixel. We
            convert it to an 8 bit depth QImage.
            
*/

                     qImageBuffer 
= (uchar *) malloc(width*height*sizeof(uchar));
                     uchar 
*QImagePtr = qImageBuffer;
                     
const float *iplImagePtr = (const float *) iplImage->imageData;
                     
for(int y = 0; y < height; y++)
                     
{
                         
for(int x = 0; x < width; x++)
                         
{
                                 uchar p;
                                 
float pf = 255 * ((*iplImagePtr++- mini) / (maxi - mini);
                                 
if(pf < 0) p = 0;
                                 
else if(pf > 255) p = 255;
                                 
else p = (uchar) pf;
   
                                 
*QImagePtr++ = p;
                          }

                         iplImagePtr 
+= widthStep/sizeof(float)-width;
                     }

             }

             
else
             
{
                     qDebug(
"IplImageToQImage: image format is not supported : depth=32F and %d channels ", iplImage->nChannels);
             }
    
               
break;
               
case IPL_DEPTH_64F:
             
if(iplImage->nChannels == 1)
             
{
                
/* OpenCV image is stored with double (8 bytes) grey pixel. We
                convert it to an 8 bit depth QImage.
                
*/

                    qImageBuffer 
= (uchar *) malloc(width*height*sizeof(uchar));
                    uchar 
*QImagePtr = qImageBuffer;
                    
const double *iplImagePtr = (const double *) iplImage->imageData;
                    
for(int y = 0; y < height; y++)
                    
{
                        
for(int x = 0; x < width; x++)
                        
{
                                uchar p;
                                
double pf = 255 * ((*iplImagePtr++- mini) / (maxi - mini);
   
                                
if(pf < 0) p = 0;
                                
else if(pf > 255) p = 255;
                                
else p = (uchar) pf;
   
                                
*QImagePtr++ = p;
                        }

                        iplImagePtr 
+= widthStep/sizeof(double)-width;
                    }

            }

            
else
            
{
                    qDebug(
"IplImageToQImage: image format is not supported : depth=64F and %d channels ", iplImage->nChannels);
            }

            
break;
            
default:
                qDebug(
"IplImageToQImage: image format is not supported : depth=%d and %d channels ", iplImage->depth, iplImage->nChannels);
        }


        QImage qImage;
        QVector
<QRgb> vcolorTable;
        
if(iplImage->nChannels == 1)
        
{
            
// We should check who is going to destroy this allocation.
            QRgb *colorTable = new QRgb[256];
            
for(int i = 0; i < 256; i++)
            
{
                   colorTable[i] 
= qRgb(i, i, i);
                   vcolorTable[i] 
= colorTable[i];
            }

            qImage 
= QImage(qImageBuffer, width, height, QImage::Format_Indexed8).copy();
            qImage.setColorTable(vcolorTable);
        }

        
else
        
{
            qImage 
= QImage(qImageBuffer, width, height, QImage::Format_RGB32).copy();
        }

    free(qImageBuffer);
   
return qImage;
}

于是你可以下面的代码(部分)测试一下:摄像头获取每一帧IplImage*类型的图像,转化为QImage类型的图像,用update()发出一个paintEvent(QPaintEvent*)事件,如此不断更新图像。(IplImageToQImage中的mini和maxi默认初始化为0)

void ImageViewer::paintEvent(QPaintEvent *){
    QPainter painter(
this);
    painter.drawImage(QPoint(
0,0), *image);
}


bool ImageViewer::ShowImage(){
    IplImage 
*pImage = NULL;
    CvCapture 
*pCapture = NULL;
    
if((pCapture = cvCaptureFromCAM(-1)) == NULL){
        cout 
<< "Open camera failed! ";
        
return false;
    }

    
while((pImage = cvQueryFrame(pCapture)) != NULL){
        image 
= IplImageToQImage(pImage);
        update();
    }

    cvReleaseImage(
&pImage);
    cvReleaseCapture(
&pCapture);
    
return true;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

linux下的QT4闹钟界面!

  • 2010-05-12 13:25
  • 16KB
  • 下载

Qt4在linux下的安装

1、下载SDK ftp://ftp.informatik.hu-berlin.de/pub/Mirrors/ftp.troll.no/QT/qtsdk/qt-sdk-linux-x86-openso...
  • xsc_c
  • xsc_c
  • 2013-06-20 22:07
  • 2033

linux下QT4的使用

最近碰到需要使用qt做界面开发的项目。记得上次使用QT都是两年前的事了,那时候QT3,当时用起来还是觉得很舒服的。但现在把QT4装上我才知道,原来QT都发生了很大的变化了。uic,qmake的使用都变...

win7+qt4+qtcreator+OpenCV 2.4.10的开发环境搭建

本文转自http://blog.csdn.net/qiurisuixiang/article/details/8665278/,在他的基础上,建议读者先看我的前一篇博客《QtCreator2.8.0+...

第一篇,关于Qt4+opencv实现打开摄像头和拍照功能(整理更正版)

本人第一次写技术类的帖子,有不足之处敬请批评指正。          刚学Qt4还不到4天,以前用得比较多的是matlab,但是要做图像处理方面的研 究,因此需要用opencv,觉得Qt 确实很不...

asmlib-opencv-qt4

  • 2015-04-15 15:50
  • 1.01MB
  • 下载

OpenCV学习之路之一开发环境搭建(W7+Qt4+opencv2+mingw)

OpenCV学习之路之开发环境搭建(w7+ Qt+opencv2+mingw) 因为项目需要,这段时间开始学习opencv了,不得不说道路还是挺漫长的,之前对这个图形库有所了解,不过都是概念型的,...

Linux环境下移植qt4及qwt从零开始

Linux环境下移植qt4及qwt从零开始 Zhuxiaoyongsdu@163.com   这两天终于在linux下搞定了qt的移植,以及qwt控件的安装。现在把整个过程整理出来。实际上,这篇...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)