在利用Qt进行OpenCV开发时,经常会利用QLabel进行图像的显示,这时需要将Mat型图像转为QImage型图像
static QImage Mat2QImage(cv::Mat& image)
{
QImage img;
if (image.channels()==3) {
cvtColor(image, image, CV_BGR2RGB);
img = QImage((const unsigned char *)(image.data), image.cols, image.rows,
image.cols*image.channels(), QImage::Format_RGB888);
} else if (image.channels()==1) {
img = QImage((const unsigned char *)(image.data), image.cols, image.rows,
image.cols*image.channels(), QImage::Format_ARGB32);
} else {
img = QImage((const unsigned char *)(image.data), image.cols, image.rows,
image.cols*image.channels(), QImage::Format_RGB888);
}
return img;
}