I'm having a one Channel image, and want to display it in a QImage
IplImage *img=cvCreateImage(cvSize(640, 480), IPL_DEPTH_8U, 1);
Knowing that When converting from an IplImage* to QImage, I did what is follow:
uchar* img_d=(uchar*) img->imageData;
QImage img_direction((uchar*)img_d, img->width, img->height, QImage::Format_Mono);
I'm not pretty sure about the Mono format that I have set, even the displayed QImage would be scrambled!
What would be the suitable QImage format for the case of a B&W image?
Qt's Mono format is 1-bit per pixel.
The only 8-bit format is QImage::Format_Indexed8, so you'll need to create a color table with the 256 grays and pass it to QImage::setColorTable.
The table could be filled like this:
QVector<QRgb> colorTable;
for(int i=0; i<256; ++i)
colorTable << qRgb(i,i,i);
Related
I am trying to load an image that is given to me as a BITMAPINFO* and a uchar array.
The documentation states that it is a standard Microsoft device independent bitmap (DIB) with 8-bit pixels and a 256-entry color table.
I am curently able to open this image through:
BITMAPINFO* bmih = givenBITMAPINFO;
uchar* data = givenData;
QImage img = QImage(data, bmih->biWidth, bmih->biHeight, QImage::Format_Grayscale8);
But I have two problems with that:
the image is in QImage::Format_Grayscale8 when the documentation states an 8-bit pixels and a 256-entry color table;
the image is upside down and mirrored. This come from the way the bitmap data is stored in Win32.
Anyone knows how I can load properly this image?
By casting the provided header to a BITMAPINFO instead of a BITMAPINFOHEADER I have access to the color table and then apply a trasformation to get a streight image:
BITMAPINFO* bmi = givenHeader;
uchar* data = givenData;
QImage img = QImage(data, bmi->bmiHeader.biWidth, bmi->bmiHeader.biHeight, QImage::Format_Indexed8);
img.setColorCount(256);
for (int i=0; i<256; ++i){
RGBQUAD* rgbBmi = bmi->bmiColors;
img.setColor(i, qRgb(rgbBmi[i].rgbRed, rgbBmi[i].rgbGreen, rgbBmi[i].rgbBlue))
}
img = img.mirrored(false, true);
like the title says I am trying to convert a cv::mat to a QImage. What I am doing is using the equalizeHist() function on the mat and then converting it to a QImage to display in widget window in Qt. I know the mat works and loads the image correctly because the equalized image will show in the new window with imshow(), however when converting this mat to a QImage, I can not get it to display in the window. I believe the problem is with the conversion from the mat to QImage but cant find the issue. Below is a part of my code snippet.
Mat image2= imread(directoryImage1.toStdString(),0);
//cv::cvtColor(image2,image2,COLOR_BGR2GRAY);
Mat histEquImg;
equalizeHist(image2,histEquImg);
imshow("Histogram Equalized Image 2", histEquImg);
//QImage img=QImage((uchar*) histEquImg.data, histEquImg.cols, histEquImg.rows, histEquImg.step, QImage::Format_ARGB32);
imageObject= new QImage((uchar*) histEquImg.data, histEquImg.cols, histEquImg.rows, histEquImg.step, QImage::Format_RGB888);
image = QPixmap::fromImage(*imageObject);
scene=new QGraphicsScene(this); //create a frame for image 2
scene->addPixmap(image); //put image 1 inside of the frame
ui->graphicsView_4->setScene(scene); //put the frame, which contains image 3, to the GUI
ui->graphicsView_4->fitInView(scene->sceneRect(),Qt::KeepAspectRatio); //keep the dimension ratio of image 3
No errors occur and the program doesnt crash.
Thanks in advance.
Your problem is the conversion of the QImage to cv::Mat, when using the flag 0 in cv::imread implies the reading is grayscale, and you are using the conversion with the format QImage::Format_RGB888. I use the following function to make the conversion of cv::Mat to QImage:
static QImage MatToQImage(const cv::Mat& mat)
{
// 8-bits unsigned, NO. OF CHANNELS=1
if(mat.type()==CV_8UC1)
{
// Set the color table (used to translate colour indexes to qRgb values)
QVector<QRgb> colorTable;
for (int i=0; i<256; i++)
colorTable.push_back(qRgb(i,i,i));
// Copy input Mat
const uchar *qImageBuffer = (const uchar*)mat.data;
// Create QImage with same dimensions as input Mat
QImage img(qImageBuffer, mat.cols, mat.rows, mat.step, QImage::Format_Indexed8);
img.setColorTable(colorTable);
return img;
}
// 8-bits unsigned, NO. OF CHANNELS=3
if(mat.type()==CV_8UC3)
{
// Copy input Mat
const uchar *qImageBuffer = (const uchar*)mat.data;
// Create QImage with same dimensions as input Mat
QImage img(qImageBuffer, mat.cols, mat.rows, mat.step, QImage::Format_RGB888);
return img.rgbSwapped();
}
return QImage();
}
After that I see that you have misconceptions of how QGraphicsView and QGraphicsScene work when commenting: put the frame, which contains image 3, to the GUI, with ui->graphicsView_4->setScene(scene); you are not setting a frame but a scene, and the scene should only be set once and preferably in the constructor.
// constructor
scene = new QGraphicsScene(this);
ui->graphicsView->setScene(scene);
So when you want to load the image just use the scene:
cv::Mat image= cv::imread(filename.toStdString(), CV_LOAD_IMAGE_GRAYSCALE);
cv::Mat histEquImg;
equalizeHist(image, histEquImg);
QImage qimage = MatToQImage(histEquImg);
QPixmap pixmap = QPixmap::fromImage(qimage);
scene->addPixmap(pixmap);
ui->graphicsView->fitInView(scene->sceneRect(), Qt::KeepAspectRatio);
The complete example can be found in the following link.
With Qt, I am trying to convert a Format_Indexed8 image to Format_RGB30 by using a custom conversion rule defined by a color table. I thought this would be simple, since QImage::convertToFormat can take a color table as an argument, but I can't make it work.
Here is my code:
QImage image = QImage(data, width, height, QImage::Format_Indexed8);
QVector<QRgb> colorTable(256);
for (int i = 0; i < 255; i++)
colorTable[i] = qRgb(255 - i, i, i);
image = image.convertToFormat(QImage::Format_RGB30, colorTable);
This code just gives me an image that is in RGB format, but that looks identical to the eye to the grayscale image.
I think the color table argument in QImage::convertToFormat is required to convert from RGB to indexed, while you're converting the other way around.
I would try setting the color table directly in the indexed file (source), using QImage::setColorTable, then call convertToFormat passing the format argument only:
QImage image = QImage(data, width, height, QImage::Format_Indexed8);
image.setColorTable(colorTable);
image = image.convertToFormat(QImage::Format_RGB30);
This is not necessary since Qt 5.5 (released in July of 2015). You can now use QImage::Format_Grayscale8. Your code would be simply:
QImage image{data, width, height, QImage::Format_Grayscale8};
I have a 32-bit integer array containing pixel values of a 3450x3450 image I want to create a Mat image with. Tried the following:
int *image_array;
image_array = (int *)malloc( 3450*3450*sizeof(int) );
memset( (char *)image_array, 0, sizeof(int)*3450*3450 );
image_array[0] = intensity_of_first_pixel;
...
image_array[11902499] = intensity_of_last_pixel;
Mat M(3450, 3450, CV_32FC1, image_array);
and upon displaying the image I get a black screen. I should also note the array contains a 16-bit grayscale image.
I guess you should try to convert the input image, which I assume is in RGB[A] format using:
cv::Mat m(3450, 3450, CV_8UC1, image_array) // For GRAY image
cv::Mat m(3450, 3450, CV_8UC3, image_array) // For RGB image
cv::Mat m(3450, 3450, CV_8UC4, image_array) // For RGBA image
Hi
I have read the openCV reference from this site and using the following code:
VideoCapture mCap;
Mat mcolImage, mbwImage;
mCap >> mcolImage; // New frames from the camera
cvtColor( mcolImage, mcolImage, CV_BGR2RGB);
cvtColor( mcolImage, mbwImage, CV_RGB2GRAY);
QImage colImagetmp( (uchar*)mcolImage.data, mcolImage.cols, mcolImage.rows, mcolImage.step,
QImage::Format_RGB888 ); //Colour
QImage bwImagetmp ( (uchar*)mbwImage.data, mbwImage.cols, mbwImage.rows, mbwImage.step,
QImage::Format_Indexed8); //Greyscale. I hope
ui.bwDisplay->setPixmap(QPixmap::fromImage(bwImagetmp));
ui.colDisplay->setPixmap( QPixmap::fromImage(colImagetmp ));
I'm trying to convert one of the output into greyscale. Unfortunately they're still both in colour and I can't see that I've missed a step somewhere.
Thanks for the help.
You need to explicitly set a gray color table for bwImagetmp:
QVector<QRgb> colorTable;
for (int i = 0; i < 256; i++) colorTable.push_back(qRgb(i, i, i));
bwImagetmp.setColorTable(colorTable);