QImage与IplImage之间的转换

网上很多的QImage与IplImage之间的转换代码,不知道别人能否顺利的用过,但是我是没顺利的使用过。主要问题是图像数据排列问题,最重要的是关注图像数据每行占多少个字节,因为涉及到对齐问题。两种不同的数据结构中都有相关的成员变量来表示,这个必须得统一,否则会发生错位或者segment fault的错误。

嵌入式Linux 中并没有完全实现openCV类库,所以不能正常使用像cvLoadImage这类函数。我的大体思路是使用QT来读入,显示,存储图像,用openCV里的函数来处理识别图像。

使用openCV中绝大部分图像处理函数的输入都是IplImage类型的灰度图像,其中涉及最重要的代码就是QImage与IplImage之间的转换,在借鉴其他网友的代码基础上做了适合自己应用的修改,代码如下:

 

 1 IplImage *Lplimage2QImage::QImageToIplImageRGB(const QImage * qImage)
 2 {
 3     int width = qImage->width();
 4     int height = qImage->height();
 5     CvSize Size;
 6     Size.height = height;
 7     Size.width = width;
 8     IplImage *IplImageBuffer = cvCreateImage(Size, IPL_DEPTH_8U, 3);
 9     for (int y = 0; y < height; ++y)
10     {
11         for (int x = 0; x < width; ++x)
12         {
13             QRgb rgb = qImage->pixel(x, y);
14             cvSet2D(IplImageBuffer, y, x, CV_RGB(qRed(rgb), qGreen(rgb), qBlue(rgb)));
15         }
16     }
17     return IplImageBuffer;
18 }
19 
20 
21 IplImage *Lplimage2QImage::QImageToIplImageGray(const QImage * qImage)
22 {
23     int width = qImage->width();
24     int height = qImage->height();
25     int widthStep = 0;
26 
27     CvSize Size;
28     Size.height = height;
29     Size.width = width;
30 
31     IplImage *charIplImageBuffer = cvCreateImage(Size, IPL_DEPTH_8U, 1);
32     widthStep = charIplImageBuffer->widthStep;
33     char *charTemp = (char *) charIplImageBuffer->imageData;
34 
35     for (int y = 0; y < height; ++y)
36     {
37         for (int x = 0; x < width; ++x)
38         {
39             int index = y * widthStep + x;
40             charTemp[index] = (char) qGray(qImage->pixel(x, y));
41         }
42     }
43 
44     return charIplImageBuffer;
45 }
46 
47 
48 QImage *Lplimage2QImage::IplImageToQImageGray(const IplImage * iplImage)
49 {
50     int width = iplImage->width;
51     int widthStep = iplImage->widthStep;
52     int height = iplImage->height;
53 
54     QImage *qImage = new QImage((uchar*)iplImage->imageData,width,height, \
55                                         widthStep,QImage::Format_Indexed8);
56     QVector<QRgb> grayColourTable;
57     for(int i = 0; i < 256; i++)
58        grayColourTable.append(qRgb(i, i, i));
59     qImage->setColorTable(grayColourTable);
60     return qImage;
61 }
62 
63 QImage *Lplimage2QImage::IplImageToQImageRGB(const IplImage * iplImage)
64 {
65     int width = iplImage->width;
66     int widthStep = iplImage->widthStep;
67     int height = iplImage->height;
68 
69     uchar *qImageBuffer = (uchar *) malloc(width*height*4*sizeof(uchar));
70     uchar *QImagePtr = qImageBuffer;
71     const uchar *iplImagePtr = (const uchar *) iplImage->imageData;
72     for (int y = 0; y < height; y++)
73     {
74         for (int x = 0; x < width; x++)
75         {
76             QImagePtr[0] = iplImagePtr[0];
77             QImagePtr[1] = iplImagePtr[1];
78             QImagePtr[2] = iplImagePtr[2];
79             QImagePtr[3] = 0;
80 
81             QImagePtr += 4;
82             iplImagePtr += 3;
83         }
84     iplImagePtr += widthStep-3*width;
85     }
86     QImage *qImage = new QImage(qImageBuffer, width, height, QImage::Format_RGB32);
87     return qImage;
88 
89 }
View Code

 

 

posted on 2012-04-20 16:23  数学之道  阅读(455)  评论(0编辑  收藏  举报

导航