+ Reply to Thread
Results 1 to 3 of 3

Thread: OpenCV , Mat image from QByteArray

  1. #1
    Join Date
    Nov 2014
    Location
    Rome
    Posts
    3
    Qt products
    Qt4 Qt5
    Platforms
    Windows Android

    Question OpenCV , Mat image from QByteArray

    Hi all ,

    I'm trying to show an image with opencv function cv::imshow("windowsName",cv::Mat) starting from QByteArray. The problem is that the image appears duplicated:

    Like this:
    DuplicatedMat.jpg

    Here the code:

    Qt Code:
    1. void Locus::getImages(QFile* file, Mat& matImg)
    2. {
    3. QByteArray imagesData;
    4.  
    5. if(file->exists())
    6. {
    7. //get byteArray from raw data file
    8. file->open(QFile::ReadOnly);
    9. imagesData = file->readAll();
    10. file->close();
    11.  
    12. //Create qimage
    13. QImage img((const uchar*)imagesData.data(),WIDTH,HEIGHT,QImage::Format_RGB16);
    14.  
    15. //create mat image from qimage
    16. matImg = this->qImage2Mat(img);
    17.  
    18. imshow("window",matImg);
    19. waitKey();
    20. }
    21. }
    22.  
    23. Mat Locus::qImage2Mat(const QImage& src)
    24. {
    25.  
    26. Mat tmp(src.height(),src.width(), CV_8UC3,(uchar*)src.bits(),src.bytesPerLine());
    27. Mat res;
    28. cvtColor(tmp, res,CV_RGB2GRAY);
    29. return res;
    30. }
    To copy to clipboard, switch view to plain text mode 

    Any help?
    ps: i think the problem is the image format, but i've tried many ways help me xD.

  2. #2
    Join Date
    Nov 2014
    Location
    Rome
    Posts
    3
    Qt products
    Qt4 Qt5
    Platforms
    Windows Android

    Default Re: OpenCV , Mat image from QByteArray

    The problem was the format, it's not CV_8UC3 but CV_16UC1(16 bit - 1 channel ).
    This is just my case ,for this specific problem , but when you work on image processing you must keep in your mind that the format convertions are very important.

  3. #3
    Join Date
    Nov 2007
    Location
    Italy
    Posts
    615
    Thanks
    22
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: OpenCV , Mat image from QByteArray

    You can create a Mat image directly from QByteArray without the need of creating a QImage

    Mat img = Mat(WIDTH,HEIGHT, CV_8UC1, imagesData.data());

    Regards
    Franco Amato

+ Reply to Thread

Similar Threads

  1. efficient way to display opencv image into Qt
    By gerardpuducul in forum Qt Programming
    Replies: 5
    Last Post: 25th January 2016, 08:59
  2. Image processing with using QByteArray
    By loading... in forum Qt Programming
    Replies: 1
    Last Post: 19th January 2015, 09:52
  3. Replies: 3
    Last Post: 4th January 2013, 06:40
  4. OpenCV Image to QImage
    By kaszewczyk in forum Newbie
    Replies: 3
    Last Post: 11th December 2012, 21:19
  5. Error OpenCV capture image
    By Mien89 in forum Qt Programming
    Replies: 3
    Last Post: 18th October 2012, 22:21

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts