F
F
Fedor2014-11-21 17:29:04
Qt
Fedor, 2014-11-21 17:29:04

If the aspect ratio is different from 4:3, the image is distorted when converted from IplImage to QImage. How to fix the code?

Hello everybody. I am developing an image processing program for studying in C ++, OpenCV and QT. As a rule, an aspect ratio of 4 to 3 is used. The program works fine. As soon as we load an image different from this aspect ratio, the resulting image moves.
Normal images:
Original:
12675dc23fc24f5282bcd7aaddc66ec2.jpg
Program output: c4840536e3a44cd4a0733291c46cfce5.png
.
Accordingly, when the aspect ratio is different:
6da41dd6c068439fab5b677efeaa231a.jpg
the following is obtained:
33de5f1594ab4b4f9d4c40aa3abf91f3.png.
All transformations happen in OpenCV.
The final picture is displayed on the form and saved from QImage, and now, when we pass the picture to IplImage to the function to get the QImage, we have distortion.
IplImage2QImage function code, in which distortion occurs when the aspect ratio of the image is incorrect.

QImage MainWindow::IplImage2QImage(const IplImage *iplImage)
{
    //http://josedolz.jimdo.com/computer-vision-tutorials/iplimage-to-qimage/
       int height = image_original->height(); //iplImage->height;
       int width = image_original->width();//iplImage->width;
       const uchar *qImageBuffer =(const uchar*)iplImage->imageData;
       QImage img(qImageBuffer, width, height, QImage::Format_RGB888);

       return img.rgbSwapped();
}

Actually, the question is: how to fix this code so that it works correctly for other aspect ratios of the image?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
DancingOnWater, 2014-11-21
@DancingOnWater

I have a suspicion that the problem is not here, but in how you display this image, or you have an image served with an alpha channel, and you report that it is not

A
Alexey Obukhov, 2015-08-04
@Obukhoff

My suspicion is that opencv stores a string of pixels aligned to some buffer size.
When converting, the tail of a line of pixels climbs onto the next line, and it turns out that each subsequent line leaves by a pixel / another.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question