Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8814559
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T04:09:26+00:00 2026-06-14T04:09:26+00:00

#include opencv2/core/core.hpp #include opencv2/highgui/highgui.hpp #include opencv2/imgproc/imgproc.hpp #include <QtGui> //make QImage point to the contents

  • 0
#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"    

#include <QtGui>

//make QImage point to the contents of cv::Mat
inline QImage const mat_to_qimage_ref(cv::Mat &mat)
{
  return QImage((unsigned char*)(mat.data), mat.cols, mat.rows, mat.step1(), QImage::Format_RGB32);
}

int main(int argc, char *argv[])
{
  QApplication a(argc, argv);
  QImage img("lena2.jpg");
  cv::Mat mat(img.height(), img.width(), CV_8UC4, img.bits(), img.bytesPerLine());
  QImage img = mat_to_qimage_ref(mat); //#1
  //QImage img2((unsigned char*)(mat.data), mat.cols, mat.rows, mat.step, QImage::Format_RGB32); #2

  QLabel label;
  label.setPixmap(QPixmap::fromImage(img)); //crash at here
  label.show();

  return a.exec();
}

(#2) is ok, but #1 will occur undefined behavior?(my case is crash)

Besides, if you use it as the codes below, It is okay

 cv::Mat img = cv::imread("lena2.jpg");
 QLabel label;
 label.setPixmap(QPixmap::fromImage(mat_to_qimage_ref(img)));
 label.show();

Don’t know what is happening, something related to cycle dependency?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T04:09:28+00:00Added an answer on June 14, 2026 at 4:09 am

    your function should be like this:

        QImage mat_to_qimage_ref(const cv::Mat3b &src) {
            QImage dest(src.cols, src.rows, QImage::Format_ARGB32);
            for (int y = 0; y < src.rows; ++y) {
                    const cv::Vec3b *srcrow = src[y];
                    QRgb *destrow = (QRgb*)dest.scanLine(y);
                    for (int x = 0; x < src.cols; ++x) {
                            destrow[x] = qRgba(srcrow[x][2], srcrow[x][1], srcrow[x][0], 255);
                    }
            }
            return dest;
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this code in OpenCV C++: #include <opencv2/imgproc/imgproc.hpp> #include <opencv2/highgui/highgui.hpp> using namespace cv;
These are includes which I am using #include <opencv2/features2d/features2d.hpp> #include <opencv2/highgui/highgui.hpp> #include <opencv2/core/core.hpp> #include
Here is the gpu surf code: #include <iostream> #include <iomanip> #include opencv2/contrib/contrib.hpp #include opencv2/objdetect/objdetect.hpp
I'm using OpenCV v2.4.1 And this is the code I'm using #include opencv2/opencv.hpp #include
#include <boost/smart_ptr.hpp> class Base { }; class Derived : public Base { public: Derived()
i have a file with only import: #include <iostream> #include <stdio.h> #include cxcore.hpp #include
I am using VS2010 and opencv2.1. Under Win7, the sample programme is below: #include
I'm following a OpenCV book's tutorial and the following code doesn't work: #include <iostream>
#include <iostream> #include <tuple> int main(){ auto bt=std::make_tuple(std::tuple<>(),std::tuple<std::tuple<>>()); //Line 1 auto bt2=std::make_tuple(std::tuple<>(),std::tuple<>()); //Line 2
#include<stdio.h> #include<stdlib.h> char* re() { char *p = hello; return p; } int main()

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.