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

本文介绍了如何将OpenCV的IplImage图像转换为QImage RGB32格式,以便在QLabel中进行显示。内容涵盖了一个转换程序的详细步骤。

首先要将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中

### 使用OpenCV在Qt的QLabel控件中显示图像 为了实现这一功能,可以采用如下方法: 将OpenCV读取到的`Mat`对象转换成PyQt可识别的形式——即先将其颜色空间由BGR转为RGB(因为OpenCV默认的颜色顺序是BGR),然后再创建一个`QImage`实例来承载这个图像数据。最后利用`QPixmap`从该`QImage`生成,并设置给指定的`QLabel`。 具体操作过程如下所示[^2]: ```python def pushbutton_function(self): Img = cv2.imread('JP1.JPG') image_height, image_width, image_depth = Img.shape # 转换色彩空间 BGR -> RGB QIm = cv2.cvtColor(Img, cv2.COLOR_BGR2RGB) # 创建 QImage 对象 q_image = QImage( QIm.data, image_width, image_height, image_width * image_depth, QImage.Format_RGB888 ) # 设置 QLabel 的 pixmap 属性以展示图片 self.label.setPixmap(QPixmap.fromImage(q_image)) ``` 对于灰度图的情况,则需额外处理一步即将其转化为RGB模式以便于正常渲染[^3]: ```python rgb_img = cv2.cvtColor(img, cv2.COLOR_GRAY2RGB) q_img = QImage( rgb_img, rgb_img.shape[1], rgb_img.shape[0], rgb_img.shape[1]*3, QImage.Format_RGB888 ) pix = QPixmap(q_img).scaled(ui.label_vision.width(), ui.label_vision.height()) ui.label_vision.setPixmap(pix) ``` 值得注意的是,在某些情况下可能会遇到图像显示异常的问题,比如图像被拉伸或压缩变形等。这通常是因为没有正确调整`QImage`构造参数中的字节宽度所致。确保传递给`QImage`构造器的最后一项参数能够准确反映每一行像素所占用的实际内存大小是非常重要的[^1]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值