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. Issue with storing Mat objects in QList
Qt 6.11 is out! See what's new in the release blog

Issue with storing Mat objects in QList

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 3 Posters 2.1k Views 2 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.
  • KiraK Offline
    KiraK Offline
    Kira
    wrote on last edited by VRonin
    #1

    Hello All,
    I have created QList of Mat (Memory Allocator for Image in Opencv) object and i am storing the camera frame in it.
    My problem is that after storing the camera frames when i am trying to save the content of list.
    All the images are replaced by the last image.
    Please find the code below for more clarification:

    Code:

      QList<Mat> images;  //Declaration of list of mat object
      VideoCapture camera(0);
    
    
    //Appending the image to list
    for(int m = 0; m < 12; m++){
            
            // Move the controller to the desired location
             camera >> controllerFrame;   // Capture the frame
             images.append(controllerFrame); // Appending the image frames
             qDebug()<<"Size of the image List:"<<images.size();
    
    		 //Saving the current frame which have been captured
             String saveImage;
             saveImage = "C:\\Users\\demo\\ImageAppendedToList"+to_string(m)+".jpeg";
             qDebug()<<saveImage.c_str();
             imwrite(saveImage.c_str(), controllerFrame);
    		 
    		 }
    // Display the image frames capture in the list
    for (int xy = 0 ; xy<images.size(); xy++) {
              String saveImageList;
              saveImageList="C:\\Users\\demo\\ImagesFrom the list"+to_string(xy)+".jpeg";
              qDebug()<<saveImageList.c_str();
              imwrite(saveImageList.c_str(), (images.at(xy)));
    
              }
    

    When i am trying to display the image stored in the list. I always get the last frame.
    Could not identify the probable reason of the issue.

    1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by
      #2

      replace images.append(controllerFrame); with images.append(controllerFrame.clone());

      The reason is that the copy constructor of cv::Mat does not perform any copy at all, all the images in the list point to the same data that you overwrite each time

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      KiraK 1 Reply Last reply
      3
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi,

        You are appending always the same object to the list so you likely have 12 copies of it that share their underlying data.

        As an experiment, move the instantiation of controllerFrame in your for loop.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        KiraK 1 Reply Last reply
        3
        • VRoninV VRonin

          replace images.append(controllerFrame); with images.append(controllerFrame.clone());

          The reason is that the copy constructor of cv::Mat does not perform any copy at all, all the images in the list point to the same data that you overwrite each time

          KiraK Offline
          KiraK Offline
          Kira
          wrote on last edited by
          #4

          @VRonin : Thanks for the help it works. You always highlight this thing :)

          1 Reply Last reply
          0
          • SGaistS SGaist

            Hi,

            You are appending always the same object to the list so you likely have 12 copies of it that share their underlying data.

            As an experiment, move the instantiation of controllerFrame in your for loop.

            KiraK Offline
            KiraK Offline
            Kira
            wrote on last edited by
            #5

            @SGaist : Nope i am taking snaps of different location which i have mentioned in the comment.

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              I wasn't referring to what you were taking pictures of but the objects that contain it. Basically the same thing that @VRonin described.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              KiraK 1 Reply Last reply
              1
              • SGaistS SGaist

                I wasn't referring to what you were taking pictures of but the objects that contain it. Basically the same thing that @VRonin described.

                KiraK Offline
                KiraK Offline
                Kira
                wrote on last edited by
                #7

                @SGaist : Ohh k. Thanks for your help. :)

                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