在QLabel中显示OpenCV的IplImage格式的图片

首先要将IplImage格式的图片转化为QImage格式,下边这个程序可以将一个Iplmage格式的图片转化为QImage RGB32格式

void  Ipl2QImageRGB32(IplImage *iplImage, QImage *qImage){
	 unsigned char* ptrQImage=qImage->bits();
	 switch(iplImage->depth){
		   case IPL_DEPTH_8U:
			    if(iplImage->nChannels==1){
					for(int row=0;row<iplImage->height;row++){
						unsigned char* ptr=(unsigned char*)(iplImage->imageData+row*iplImage->widthStep);
						for(int col=0;col<iplImage->width;col++){
						    *(ptrQImage)=*(ptr+col);
                                                    *(ptrQImage+1)=*(ptr+col);
						    *(ptrQImage+2)=*(ptr+col);
						    *(ptrQImage+3)=0;
						    ptrQImage+=4;
						}
					}
			    }
				else if(iplImage->nChannels==3){
				    for(int row=0;row<iplImage->height;row++){
						unsigned char* ptr=(unsigned char*)(iplImage->imageData+row*iplImage->widthStep);
						for(int col=0;col<iplImage->width;col++){
						    *(ptrQImage)=*(ptr+col*3);
                                                    *(ptrQImage+1)=*(ptr+col*3+1);
						    *(ptrQImage+2)=*(ptr+col*3+2);
						    *(ptrQImage+3)=0;
						    ptrQImage+=4;
						}
					}
				}
			    break;
		   case IPL_DEPTH_32F:
			    if(iplImage->nChannels==1){
					for(int row=0;row<iplImage->height;row++){
						float* ptr=(float*)(iplImage->imageData+row*iplImage->widthStep);
						for(int col=0;col<iplImage->width;col++){
						    *(ptrQImage)=(unsigned char)(*(ptr+col)*255.0);
                                                    *(ptrQImage+1)=(unsigned char)(*(ptr+col)*255.0);
						    *(ptrQImage+2)=(unsigned char)(*(ptr+col)*255.0);
						    *(ptrQImage+3)=0;
						    ptrQImage+=4;
						}
					}
			    }
				else if(iplImage->nChannels==3){
				    for(int row=0;row<iplImage->height;row++){
						float* ptr=(float*)(iplImage->imageData+row*iplImage->widthStep);
						for(int col=0;col<iplImage->width;col++){
						    *(ptrQImage)=(unsigned char)(*(ptr+col*3)*255.0);
                                                    *(ptrQImage+1)=(unsigned char)(*(ptr+col*3+1)*255.0);
						    *(ptrQImage+2)=(unsigned char)(*(ptr+col*3+2)*255.0);
						    *(ptrQImage+3)=0;
						    ptrQImage+=4;
						}
					}
				}
			    break;
		   case IPL_DEPTH_64F:
			    if(iplImage->nChannels==1){
					for(int row=0;row<iplImage->height;row++){
						double* ptr=(double*)(iplImage->imageData+row*iplImage->widthStep);
						for(int col=0;col<iplImage->width;col++){
						    *(ptrQImage)=(unsigned char)(*(ptr+col)*255.0);
                                                    *(ptrQImage+1)=(unsigned char)(*(ptr+col)*255.0);
						    *(ptrQImage+2)=(unsigned char)(*(ptr+col)*255.0);
						    *(ptrQImage+3)=0;
						    ptrQImage+=4;
						}
					}
			    }
				else if(iplImage->nChannels==3){
				    for(int row=0;row<iplImage->height;row++){
						double* ptr=(double*)(iplImage->imageData+row*iplImage->widthStep);
						for(int col=0;col<iplImage->width;col++){
						    *(ptrQImage)=(unsigned char)(*(ptr+col*3)*255.0);
                                                    *(ptrQImage+1)=(unsigned char)(*(ptr+col*3+1)*255.0);
						    *(ptrQImage+2)=(unsigned char)(*(ptr+col*3+2)*255.0);
						    *(ptrQImage+3)=0;
						    ptrQImage+=4;
						}
					}
				}
			    break;
		   default:
			    printf("The type of the IplImage should be IPL_DEPTH_8U,IPL_DEPTH_32F or IPL_DEPTH_64F");
	 }

之后可以用QLabel的setPixmap()和QPixmap的fromImage()将IplImage格式的图片显示在QLabel中

你可能感兴趣的:(float)