Qt图片的显示示例

QPixmap OpenCV40_VC14_Qt58::ImageToPixmap(const Mat cvimage)
{
	QPixmap pixmap;
	QImage *qimage = NULL;

	if (1 == cvimage.channels())
	{
		uchar *imgData = (uchar *)cvimage.data;
		qimage = new QImage(imgData, cvimage.cols, cvimage.rows, cvimage.step
			, QImage::Format_Indexed8);
		pixmap = QPixmap::fromImage(*qimage);
	}
	else if (3 == cvimage.channels())
	{
		Mat img(cvimage.cols, cvimage.rows, cvimage.depth());
		cvtColor(cvimage, img, COLOR_BGR2RGB);
		uchar *imgData = (uchar *)img.data;
		qimage = new QImage(imgData, cvimage.cols, cvimage.rows, cvimage.step
			, QImage::Format_RGB888);
		pixmap = QPixmap::fromImage(*qimage);
	}

	return pixmap;
}

void OpenCV40_VC14_Qt58::ShowImage(cv::Mat color_img)
{
	// Judge the if the image is empty.
	if (color_img.empty())
	{
		return;
	}
	QLabel *lbl_show_img = NULL;
	lbl_show_img = ui.label_show;
	lbl_show_img->setPixmap(ImageToPixmap(color_img));
	lbl_show_img->setScaledContents(true);
	lbl_show_img->show();


}

 

你可能感兴趣的:(#工具使用,学习笔记)