I'm working on a homework for my Digital Image Processing class, and I'm using OpenCV with QT Framework.
I've created a class ImageDisplay, which is as sub class of the QWidget class.
I've using OpenCV to manipulate a grayscale image, and then creating a QImage object from the Mat object. After that I use the QWidget::drawImage() to draw the image. But sometimes it shows a distorted, angled image.
I was experimenting and discovered that it has something to do with the image dimensions.
For instance, this image with 320x391 pixels is rendered normally
But if change the dimension to 321x391 (using Gimp), it shows up like this:
This is the code for the paintEvent method:
void ImageDisplay::paintEvent(QPaintEvent *){
QPainter painter(this);
Mat tmp;
/* The mat in the next line is a Mat object that contains the image data */
cvtColor(mat, tmp, CV_GRAY2BGR);
QImage image(tmp.data, tmp.cols, tmp.rows, QImage::Format_RGB888);
painter.drawImage(rect(), image);
}
Does anyone has a clue what is the problem and how to fix it?
Thanks in advance!
I think you may need to specify the step of the image (number of bytes per line) when creating QImage in the fourth parameter:
QImage image((const uchar*) tmp.data, tmp.cols, tmp.rows, tmp.step, QImage::Format_RGB888);
Related
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.
I am programming in Qt environment and I have a Mat image with size 2592x2048 and I want to resize it to the size of a "label" that I have. But when I want to show the image, I have to multiply the width by 3, so the image is shown in its correct size. Is there any explanation for that?
This is my code:
//Here I get image from the a buffer and save it into a Mat image.
//img_width is 2592 and img_height is 2048
Mat image = Mat(cv::Size(img_width, img_height), CV_8UC3, (uchar*)img, Mat::AUTO_STEP);
Mat cimg;
double r; int n_width, n_height;
//Get the width of label (lbl) into which I want to show the image
n_width = ui->lbl->width();
r = (double)(n_width)/img_width;
n_height = r*(img_height);
cv::resize(image, cimg, Size(n_width*3, n_height), INTER_AREA);
Thanks.
The resize function works well, because if you save the resized image as a file is displayed correctly. Since you want to display it on QLabel, I assume you have to transform your image to QImage first and then to QPixmap. I believe the problem lies either in the step or the image format.
If we ensure the image data passed in
Mat image = Mat(cv::Size(img_width, img_height), CV_8UC3, (uchar*)img, Mat::AUTO_STEP);
are indeed an RGB image, then below code should work:
ui->lbl->setPixmap(QPixmap::fromImage(QImage(cimg.data, cimg.cols, cimg.rows, *cimg.step.p, QImage::Format_RGB888 )));
Finally, instead of using OpenCV, you could construct a QImage object using the constructor
QImage((uchar*)img, img_width, img_height, QImage::Format_RGB888)
and then use the scaledToWidth method to do the resize. (beware thought that this method returns the scaled image, and does not performs the resize operation to the image per se)
I'm new to OpenCV and I've done a small POC for reading an image from some URL.
I'm reading the image from an URL using video capture. The code is as follows:
VideoCapture vc;
vc.open("http://files.kurento.org/img/mario-wings.png");
if(vc.isOpened() && vc.grab())
{
cv::Mat logo;
vc.retrieve(logo);
cv::namedWindow("t");
imwrite( "mario-wings-opened.png", logo);
cv::imshow("t", logo);
cv::waitKey(0);
vc.release();
}
This image is not opened correctly, possibly due to alpha channel.
What is the way to preserve alpha channel and get the image correctly?
Any help is appreciated.
-Thanks
Expected output
Actual output
if you are only loading an image, I recommend you to use imread instead, also, you will need to specified the second parameter of imread to load the alpha channel too, that is CV_LOAD_IMAGE_UNCHANGED or cv::IMREAD_UNCHANGED, depending on the version (in the worst case a -1 also works).
As far as I know, the VideoCaptureclass do not load images/video with a 4th channel. Since you are using a web url, loading the image won't work with imread, but you may use any method to download the data (curl for example) and then use imdecode with the data buffer to get the cv::Mat. OpenCV is a library for image processing, not for downloading images.
If you wanna draw it over another image you can do that:
/**
* #brief Draws a transparent image over a frame Mat.
*
* #param frame the frame where the transparent image will be drawn
* #param transp the Mat image with transparency, read from a PNG image, with the IMREAD_UNCHANGED flag
* #param xPos x position of the frame image where the image will start.
* #param yPos y position of the frame image where the image will start.
*/
void drawTransparency(Mat frame, Mat transp, int xPos, int yPos) {
Mat mask;
vector<Mat> layers;
split(transp, layers); // seperate channels
Mat rgb[3] = { layers[0],layers[1],layers[2] };
mask = layers[3]; // png's alpha channel used as mask
merge(rgb, 3, transp); // put together the RGB channels, now transp insn't transparent
transp.copyTo(frame.rowRange(yPos, yPos + transp.rows).colRange(xPos, xPos + transp.cols), mask);
}
I want to display 3 channel cv::Mat on Qt interface and usually I use this expressions:
e
QImage qImage = QImage( (uchar*)cvImage.data, cvImage.cols, cvImage.rows, cvImage.cols*3, QImage::Format_RGB888 );
QPixmap pixmap = QPixmap::fromImage(qImage);
myLabel.setPixmap(pixmap);
But conversion to QPixmap is slow enough. Do you know guys how to avoid the conversion? May be the function setImage can help, but I don't know how to use it...
What I want to do:
Convert a ROI of the Nao Robot camera image on the OpenCV::Mat format. Later I will make use of this OpenCV::Mat
The situation:
Nao SDK provide the image in a format called ALImage. It is possible to convert the ALImage to OpenCV::Mat format, but I do not need all the image, only a small ROI. Despite ALImage provides its own ROI, the methods to use it are not really helpful:
int getNumOfROIs () const
const ROI* getROI (int index) const
void addROI (const ROI &rect)
void cleanROIs ()
void setEnableROIs (bool enable)
bool isROIEnabled () const
The question:
How can I use these ROIs?
Assuming you already have the coordinates of your ROI, you can crop your cv::Mat like this:
// your source image
cv::Mat image(imagesource);
// a rectangle with your ROI coordinates
cv::Rect myROI(10, 10, 100, 100);
// a "reference" to the image data within the rectangle
cv::Mat croppedImage = image(myROI);
Note that this does not copy the image data. Both, image and croppedImage share the same underlying raw data (a detailed example can be found in the opencv documentation). When you're done with the big source image, you can do image.release() and croppedImage = croppedImage.clone(); to deallocate all unnecessary data (outside of your ROI).
EDIT:
I didn't work with AL::ALImage::ROI yet, but the definition in alimage/alimage.h looks familiar to cv::Rect. So you can probably do the following:
// let's pretend you already got your image ...
AL::ALImage yourImage;
// ... and a ROI ...
AL::ALImage::ROI yourROI;
// ... as well as a Mat header with the dimensions and type of yourImage
cv::Mat header;
// then you can convert the ROI, ...
cv::Rect cvROI(yourROI.x, yourROI.y, yourROI.w, yourROI.h);
// ... wrap ALImage to a Mat (which should not copy anything) ...
header.data = yourImage.getData();
// ... and then proceed with the steps mentioned above to crop your Mat
cv::Mat cropped = header(cvROI);
header.release();
cropped = cropped.clone();
// ...
// your image processing using cropped
// ...
I hope this helps.