之前还做过GIGE相机的raw格式转换为OpenCV格式,一开始觉得很不好下手,但其实图像都会遵循标准的,,不要慌,仔细看文档,看清楚是什么格式后就能发现转换的办法
代码如下
- void MainWindow::on_pushButton_clicked()
- {
- Mat image;
- image = imread("test.bmp", CV_LOAD_IMAGE_COLOR); // Read the file
- if(! image.data ) // Check for invalid input
- {
- cout << "Could not open or find the image" << std::endl ;
- }
- Mat rgb;
- cvtColor(image, rgb, CV_BGR2RGB);
- QImage img = QImage((const unsigned char*)(rgb.data), rgb.cols, rgb.rows, QImage::Format_RGB888);
- }
补充,如果图像在opencv层面已经是二值化图像了,那么QImage那一行相应要改写方能正常显示
- QImage img = QImage((const unsigned char*)(rgb.data), rgb.cols, rgb.rows, QImage::Format_Indexed8);
- QVector<QRgb> colorTable;
- for(int k=0;k<256;++k)
- {
- colorTable.push_back( qRgb(k,k,k) );
- }
- img.setColorTable(colorTable);