0

Assuming that i need to show an openCV image in Qt creator.

My image declared with cv::Mat type, but openCV has no member that allows to put an image into Qt application GUI.

Furthermore, i want my code to be able to use this syntax: "image.displayinGUI()" (where image is cv::Mat type). I know how to display image in Qt but I just don't know the right design pattern to deploy inside my program reasonably ( i think the problem is inheritance in C++ , but can't solve it. Remind, i'm not ask any exactly problem , such as Qt or openCV. I wonder how to add new method to old object without edit old code. You can give your suggest with Qt , opencv expample , or any thing in C++ )!

So, what should i do?

(I'm sorry because i'm c++ newbie and my english is not good)

I wrote this scenario but it didn't work:

class usergui : public cv::Mat {
public:
    displayinGUI( QLabel* label ) {
        cv::Mat newimg = this->clone();
        cv::cvtColor( newimg , newimg , CV_BGR2RGB );
        QImage image = QImage((const unsigned char*)newimg.data , newimg.cols , newimg.rows , QImage::Format_RGB888);
        label->setPixmap(QPixmap::fromImage(image));
        label->resize(label->pixmap()->size());
    }
}



// in main program:
/* ... */
cv::Mat myimage;
myimage.imread("lenna.png");
myimage.displayinGUI( myQtlabel ); // this cause an error: "cv::Mat has no member named "displayinGUI()"
/* ...*/
Qu Ang
  • 27
  • 4
  • possible duplicate of [how to convert an opencv cv::Mat to qimage](http://stackoverflow.com/questions/5026965/how-to-convert-an-opencv-cvmat-to-qimage) – kiranpradeep Apr 23 '15 at 10:28
  • this is just an example. In addition, i think use classes is very convenient, particularly if i want reuse my code! – Qu Ang Apr 23 '15 at 10:32
  • "add new method to old object without edit old code" ?? this is essentially what inheritance is for, what is your **exact** problem? – m.s. Apr 23 '15 at 11:53
  • you can see my code. it cause "cv::Mat has no member named displayinGUI(). I wonder what is exact syntax to do it. ( i'm newbie ) – Qu Ang Apr 23 '15 at 12:09

1 Answers1

0

EDIT: Sorry, have not seen the second part of your code example.

It is easy, create usergui instance not cv::Mat:

// in main program:
/* ... */
usergui myimage; //<<<<change here
myimage.imread("lenna.png");
myimage.displayinGUI( myQtlabel ); // should work
/* ...*/

OLD Answer:

Why should a gui class derive from cv::Mat? Why should a class holding image data have dependency to GUI?

You should split this in 3 tasks:

  1. Class which holds cv::Mat image (Non gui)
  2. Class or function which converts (Utility, non gui) cv::Mat in QImage
  3. Class that displays something (GUI)

So you'd show a Mat in QLabel by:

QImage matToImg( cv::Mat  ); //TODO implement

cv::Mat mat = imread("lena.png");
QLabel *label = new QLabel;
label->setPixmap( QPixmap::fromImage( matToImg( mat ) ) );

I'm using following code for Mat to QImage conversion (Without any warranty):

QImage IplImage2QImage(IplImage *iplImg)
{
    int h = iplImg->height;
    int w = iplImg->width;
    int channels = iplImg->nChannels;
    QImage qimg(w, h, QImage::Format_ARGB32);
    char *data = iplImg->imageData;

    for (int y = 0; y < h; y++, data += iplImg->widthStep)
    {
    for (int x = 0; x < w; x++)
    {
    char r, g, b, a = 0;
    if (channels == 1)
    {
    r = data[x * channels];
    g = data[x * channels];
    b = data[x * channels];
    }
    else if (channels == 3 || channels == 4)
    {
    r = data[x * channels + 2];
    g = data[x * channels + 1];
    b = data[x * channels];
    }

    if (channels == 4)
    {
    a = data[x * channels + 3];
    qimg.setPixel(x, y, qRgba(r, g, b, a));
    }
    else
    {
    qimg.setPixel(x, y, qRgb(r, g, b));
    }
    }
    }
    return qimg;
}

QImage toQImage( const cv::Mat &src )
{ 
    return IplImage2QImage( new IplImage(src) );
}
Valentin H
  • 7,240
  • 12
  • 61
  • 111
  • please do **not** recommend anything containing IplImages. – berak Apr 23 '15 at 11:01
  • Thanks. But sorry, i can't use the syntax "image.displayinGUI()" with your code. I'm not ask any exactly problem , such as Qt or openCV. I wonder how to add new method to old object without edit old code. – Qu Ang Apr 23 '15 at 11:20
  • @berak After struggling lot converting Mat to QImage I came to this solution from old days. If you'd propose working code converting cv::Mat to QImage, I'd very appreciate it. – Valentin H Apr 23 '15 at 14:49
  • @QuAng: Sorry, have not read completely. Please check the update. – Valentin H Apr 23 '15 at 14:54