Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Using Mat and Mat_<double>

Using Mat and Mat_<double>

Scheduled Pinned Locked Moved General and Desktop
5 Posts 3 Posters 3.8k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    jekasores
    wrote on last edited by
    #1

    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) {

    * scale;

    = qRgba(color, color, color, 255);

    }
    }
    return dest;
    }
    @

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

    [code wrappings added, koahnig]

    1 Reply Last reply
    0
    • K Offline
      K Offline
      koahnig
      wrote on last edited by
      #2

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

      Vote the answer(s) that helped you to solve your issue(s)

      1 Reply Last reply
      0
      • C Offline
        C Offline
        ChrisW67
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        • J Offline
          J Offline
          jekasores
          wrote on last edited by
          #4

          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?

          1 Reply Last reply
          0
          • C Offline
            C Offline
            ChrisW67
            wrote on last edited by
            #5

            "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) {

            * scale;

            = 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.

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved