Using Mat and Mat_<double>



  • Hi guys,
    I need to convert a Mat to QImage for show a video in a label.
    I'm using this code but my image is Mat and the code is Mat_double. I'm trying to do this:
    @
    Mat img;
    Mat2QImage(img);

    QImage Mat2QImage(const cv::Mat_<double> &src)
    {
    double scale = 255.0;
    QImage dest(src.cols, src.rows, QImage::Format_ARGB32);
    for (int y = 0; y < src.rows; ++y) {
    const double *srcrow = src[y];
    QRgb destrow = (QRgb)dest.scanLine(y);
    for (int x = 0; x < src.cols; ++x) {
    unsigned int color = srcrow[x] * scale;
    destrow[x] = qRgba(color, color, color, 255);
    }
    }
    return dest;
    }
    @

    What can I do? Could I pass mat to mat_<double>?

    [code wrappings added, koahnig]


  • Moderators

    Please read the forum help for "code wrappings":http://qt-project.org/wiki/ForumHelp#e3f82045ad0f480d3fb9e0ac2d58fb01 this makes your post readable.



  • This is an OpenCV/basic C++ question, nothing to do with Qt.

    The Mat you declare at line 1 is empty. It has no elements but is capable of holding all sorts of types of data. The function expects a Mat instance containing doubles. Unless you do something between line 1 and 2 to load the Mat with some data of a certain type it will still be empty when you call the function.



  • I put a data in img but I didn't post it here. When I pass a Mat to a Mat_<double> it doesn't work. What can I do?



  • "It doesn't work" is hardly a description of the problem. At least post the compiler, linker, or runtime error or a small, self-contained program than demonstrates the problem. We can guess all day.

    bq. I put a data in img but I didn’t post it here.

    What you put in the Mat object is actually important. It would need to be filled as type CV_64F (or CV_32F) to make sense as your argument. imread() does not generally produce that type of Mat content.

    Edit: This code:
    @
    #include <QApplication>
    #include <QImage>
    #include <QDebug>
    #include <opencv/cv.h>

    QImage Mat2QImage(const cv::Mat_<double> &src)
    {
    double scale = 255.0;
    QImage dest(src.cols, src.rows, QImage::Format_ARGB32);
    for (int y = 0; y < src.rows; ++y) {
    const double *srcrow = src[y];
    QRgb destrow = (QRgb)dest.scanLine(y);
    for (int x = 0; x < src.cols; ++x) {
    unsigned int color = srcrow[x] * scale;
    destrow[x] = qRgba(color, color, color, 255);
    }
    }
    return dest;
    }

    int main(int argc, char **argv)
    {
    QApplication app(argc, argv);
    cv::Mat m = cv::Mat(256, 256, CV_64F, 127.0);
    // cv::Mat m = cv::Mat( 256, 256, cv::DataType<double>::type, 127.0 ); // or this alternate formulation
    QImage img = Mat2QImage(m);
    img.save("test.png");
    return 0;
    }
    @
    Produces a half-grey square as you would expect.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.