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. QT QImage Read From Error

QT QImage Read From Error

Scheduled Pinned Locked Moved Unsolved General and Desktop
qimageqthreadsignals & slotsqfileinfo
23 Posts 5 Posters 3.4k Views
  • 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.
  • M Offline
    M Offline
    mvsri
    wrote on 9 Sept 2020, 07:41 last edited by
    #1

    Hey there,
    I am developing a GUI, where i read a Qimage from a stored location and display it.
    I have used QThread for image grabbing and used signal and slot mechanism to display image in mainwindow.
    The code is working as expected but the problem is when the frequency of image grabbing increase, image does not show fully it has some blank portion covered.
    i'm attaching the image below
    ![alt text](![image url](error_img.png image url))
    as you can see bottom part of the image is completely blank.
    Just wanted to know what could be the reason for it and how to approach a better solution.

            bool check = doesFilexExist(image_path);
            if(check){
                bool result = image.load(image_path);
                QThread::msleep(20);
                if(!image.isNull()){
                    qDebug() << "==> Timestamp for image grabbing: " << QTime::currentTime();
    
                    emit sendImage(image);
                    QFile::remove(image_path);           
                    QThread::msleep(50);
                }
    

    I have attached code snippet here.

    J 1 Reply Last reply 9 Sept 2020, 07:55
    0
    • M mvsri
      9 Sept 2020, 07:41

      Hey there,
      I am developing a GUI, where i read a Qimage from a stored location and display it.
      I have used QThread for image grabbing and used signal and slot mechanism to display image in mainwindow.
      The code is working as expected but the problem is when the frequency of image grabbing increase, image does not show fully it has some blank portion covered.
      i'm attaching the image below
      ![alt text](![image url](error_img.png image url))
      as you can see bottom part of the image is completely blank.
      Just wanted to know what could be the reason for it and how to approach a better solution.

              bool check = doesFilexExist(image_path);
              if(check){
                  bool result = image.load(image_path);
                  QThread::msleep(20);
                  if(!image.isNull()){
                      qDebug() << "==> Timestamp for image grabbing: " << QTime::currentTime();
      
                      emit sendImage(image);
                      QFile::remove(image_path);           
                      QThread::msleep(50);
                  }
      

      I have attached code snippet here.

      J Online
      J Online
      JonB
      wrote on 9 Sept 2020, 07:55 last edited by
      #2

      @mvsri
      What is the scope of your image variable? Does it remain in scope throughout the time the main UI thread accesses it? Does this code get re-entered and re-load the same image variable while it is still being accessed in the main UI thread?

      M 1 Reply Last reply 9 Sept 2020, 08:31
      0
      • J JonB
        9 Sept 2020, 07:55

        @mvsri
        What is the scope of your image variable? Does it remain in scope throughout the time the main UI thread accesses it? Does this code get re-entered and re-load the same image variable while it is still being accessed in the main UI thread?

        M Offline
        M Offline
        mvsri
        wrote on 9 Sept 2020, 08:31 last edited by
        #3

        @JonB
        it get re-entered and re-load the same image variable.

        J 1 Reply Last reply 9 Sept 2020, 08:37
        0
        • C Online
          C Online
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on 9 Sept 2020, 08:32 last edited by
          #4

          Why do you need a thread for this at all?
          Simply create a new QImage object instead using a local / member var would be a first step.

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          1 Reply Last reply
          1
          • M mvsri
            9 Sept 2020, 08:31

            @JonB
            it get re-entered and re-load the same image variable.

            J Online
            J Online
            JonB
            wrote on 9 Sept 2020, 08:37 last edited by
            #5

            @mvsri said in QT QImage Read From Error:

            @JonB
            it get re-entered and re-load the same image variable.

            Then I'm thinking that is not a good idea?

            Start by looking at @Christian-Ehrlicher's advice.

            M 1 Reply Last reply 9 Sept 2020, 08:44
            0
            • J JonB
              9 Sept 2020, 08:37

              @mvsri said in QT QImage Read From Error:

              @JonB
              it get re-entered and re-load the same image variable.

              Then I'm thinking that is not a good idea?

              Start by looking at @Christian-Ehrlicher's advice.

              M Offline
              M Offline
              mvsri
              wrote on 9 Sept 2020, 08:44 last edited by
              #6

              @JonB @Christian-Ehrlicher reason for using thread was the entire code snippet will be in a while loop, continuously grabbing image. and main GUI has other functions on it.
              and also i have tried without using qthread but there was no real time image capturing happening there (i also used QTimer).

              J 1 Reply Last reply 9 Sept 2020, 08:52
              0
              • M mvsri
                9 Sept 2020, 08:44

                @JonB @Christian-Ehrlicher reason for using thread was the entire code snippet will be in a while loop, continuously grabbing image. and main GUI has other functions on it.
                and also i have tried without using qthread but there was no real time image capturing happening there (i also used QTimer).

                J Online
                J Online
                JonB
                wrote on 9 Sept 2020, 08:52 last edited by
                #7

                @mvsri
                Just for now: instead of a single image declaration somewhere, in your code try:

                QImage *image = new QImage;
                bool result = image->load(image_path);
                ...
                emit sendImage(*image);
                // or, with corresponding change to signal/slot parameter
                emit sendImage(image);
                

                I know this will leak the image! Assuming it's not too big, and you don't do it too often, so that memory is not an issue, does that make a difference to the "black" you see?

                C M 2 Replies Last reply 9 Sept 2020, 09:03
                0
                • J JonB
                  9 Sept 2020, 08:52

                  @mvsri
                  Just for now: instead of a single image declaration somewhere, in your code try:

                  QImage *image = new QImage;
                  bool result = image->load(image_path);
                  ...
                  emit sendImage(*image);
                  // or, with corresponding change to signal/slot parameter
                  emit sendImage(image);
                  

                  I know this will leak the image! Assuming it's not too big, and you don't do it too often, so that memory is not an issue, does that make a difference to the "black" you see?

                  C Online
                  C Online
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on 9 Sept 2020, 09:03 last edited by
                  #8

                  @JonB No need to create it with new, a local instance on the stack will work fine

                  Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                  Visit the Qt Academy at https://academy.qt.io/catalog

                  J 1 Reply Last reply 9 Sept 2020, 09:14
                  0
                  • C Christian Ehrlicher
                    9 Sept 2020, 09:03

                    @JonB No need to create it with new, a local instance on the stack will work fine

                    J Online
                    J Online
                    JonB
                    wrote on 9 Sept 2020, 09:14 last edited by
                    #9

                    @Christian-Ehrlicher
                    I don't see that as safe? If the stack variable in the thread goes out of scope while the signal has been emit()ed passing the image for the main UI thread to use, won't that release the image while it is still needed by the main thread?

                    1 Reply Last reply
                    0
                    • J JonB
                      9 Sept 2020, 08:52

                      @mvsri
                      Just for now: instead of a single image declaration somewhere, in your code try:

                      QImage *image = new QImage;
                      bool result = image->load(image_path);
                      ...
                      emit sendImage(*image);
                      // or, with corresponding change to signal/slot parameter
                      emit sendImage(image);
                      

                      I know this will leak the image! Assuming it's not too big, and you don't do it too often, so that memory is not an issue, does that make a difference to the "black" you see?

                      M Offline
                      M Offline
                      mvsri
                      wrote on 9 Sept 2020, 09:14 last edited by
                      #10

                      @JonB @Christian-Ehrlicher
                      i tried both methods using new and local instance.
                      still it doesn't make any difference.
                      will it be advisable to use QMutex for this?

                      1 Reply Last reply
                      0
                      • C Online
                        C Online
                        Christian Ehrlicher
                        Lifetime Qt Champion
                        wrote on 9 Sept 2020, 09:18 last edited by
                        #11

                        No need for a mutex, please post code with stack variable.

                        will be in a while loop, continuously grabbing image. and main GUI has other functions on it.

                        So you say the image reading take too long?

                        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                        Visit the Qt Academy at https://academy.qt.io/catalog

                        M 1 Reply Last reply 9 Sept 2020, 09:35
                        0
                        • C Christian Ehrlicher
                          9 Sept 2020, 09:18

                          No need for a mutex, please post code with stack variable.

                          will be in a while loop, continuously grabbing image. and main GUI has other functions on it.

                          So you say the image reading take too long?

                          M Offline
                          M Offline
                          mvsri
                          wrote on 9 Sept 2020, 09:35 last edited by
                          #12

                          @Christian-Ehrlicher
                          This is the code

                          while(true)
                             {
                                 bool check = doesFilexExist(image_path);
                                 if(check){
                                     QImage image;
                                     bool result = image.load(image_path);
                                     QThread::msleep(20);
                                     if(!image.isNull()){
                                         qDebug() << "==> Timestamp for image grabbing: " << QTime::currentTime();
                          
                                         emit sendImage(image);
                                         QFile::remove(image_path);
                                         QThread::msleep(50);
                                     }
                                 }
                                 else {
                                     // Do nothing
                                 }
                             }
                          

                          @Christian-Ehrlicher said in QT QImage Read From Error:

                          So you say the image reading take too long?

                          I'm not so sure about that.

                          The logic is very simple :
                          1. Check if image exists in folder.
                          2.if exists, load image
                          3.emit the image
                          4. delete image from the folder

                          Note: 3rd party camera stores a single image in that folder

                          C J.HilkJ 2 Replies Last reply 9 Sept 2020, 09:36
                          0
                          • M mvsri
                            9 Sept 2020, 09:35

                            @Christian-Ehrlicher
                            This is the code

                            while(true)
                               {
                                   bool check = doesFilexExist(image_path);
                                   if(check){
                                       QImage image;
                                       bool result = image.load(image_path);
                                       QThread::msleep(20);
                                       if(!image.isNull()){
                                           qDebug() << "==> Timestamp for image grabbing: " << QTime::currentTime();
                            
                                           emit sendImage(image);
                                           QFile::remove(image_path);
                                           QThread::msleep(50);
                                       }
                                   }
                                   else {
                                       // Do nothing
                                   }
                               }
                            

                            @Christian-Ehrlicher said in QT QImage Read From Error:

                            So you say the image reading take too long?

                            I'm not so sure about that.

                            The logic is very simple :
                            1. Check if image exists in folder.
                            2.if exists, load image
                            3.emit the image
                            4. delete image from the folder

                            Note: 3rd party camera stores a single image in that folder

                            C Online
                            C Online
                            Christian Ehrlicher
                            Lifetime Qt Champion
                            wrote on 9 Sept 2020, 09:36 last edited by
                            #13

                            @mvsri So are you sure the camera does not override the image while you try to load it? Please try without the camera with a static image.

                            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                            Visit the Qt Academy at https://academy.qt.io/catalog

                            M 1 Reply Last reply 9 Sept 2020, 09:42
                            0
                            • M mvsri
                              9 Sept 2020, 09:35

                              @Christian-Ehrlicher
                              This is the code

                              while(true)
                                 {
                                     bool check = doesFilexExist(image_path);
                                     if(check){
                                         QImage image;
                                         bool result = image.load(image_path);
                                         QThread::msleep(20);
                                         if(!image.isNull()){
                                             qDebug() << "==> Timestamp for image grabbing: " << QTime::currentTime();
                              
                                             emit sendImage(image);
                                             QFile::remove(image_path);
                                             QThread::msleep(50);
                                         }
                                     }
                                     else {
                                         // Do nothing
                                     }
                                 }
                              

                              @Christian-Ehrlicher said in QT QImage Read From Error:

                              So you say the image reading take too long?

                              I'm not so sure about that.

                              The logic is very simple :
                              1. Check if image exists in folder.
                              2.if exists, load image
                              3.emit the image
                              4. delete image from the folder

                              Note: 3rd party camera stores a single image in that folder

                              J.HilkJ Offline
                              J.HilkJ Offline
                              J.Hilk
                              Moderators
                              wrote on 9 Sept 2020, 09:38 last edited by J.Hilk 9 Sept 2020, 09:41
                              #14

                              @mvsri Have you thought about using QFileSystemWatcher to listen to changes in the folder,?

                              No need to reinvent the wheel, you can simply react to the directoryChanged signal


                              If @Christian-Ehrlicher is right, and the camera overwrites the image, than the fileChanged signal will be much handier to use.


                              Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                              Q: What's that?
                              A: It's blue light.
                              Q: What does it do?
                              A: It turns blue.

                              M 1 Reply Last reply 9 Sept 2020, 09:43
                              0
                              • C Christian Ehrlicher
                                9 Sept 2020, 09:36

                                @mvsri So are you sure the camera does not override the image while you try to load it? Please try without the camera with a static image.

                                M Offline
                                M Offline
                                mvsri
                                wrote on 9 Sept 2020, 09:42 last edited by
                                #15

                                @Christian-Ehrlicher
                                I think the camera does override the image when i try to load it because when i tested it with static image the code worked perfectly

                                C 1 Reply Last reply 9 Sept 2020, 09:44
                                0
                                • J.HilkJ J.Hilk
                                  9 Sept 2020, 09:38

                                  @mvsri Have you thought about using QFileSystemWatcher to listen to changes in the folder,?

                                  No need to reinvent the wheel, you can simply react to the directoryChanged signal


                                  If @Christian-Ehrlicher is right, and the camera overwrites the image, than the fileChanged signal will be much handier to use.

                                  M Offline
                                  M Offline
                                  mvsri
                                  wrote on 9 Sept 2020, 09:43 last edited by
                                  #16

                                  @J-Hilk said in QT QImage Read From Error:

                                  @mvsri Have you thought about using QFileSystemWatcher to listen to changes in the folder,?

                                  I haven't thought about that will try to use it now.

                                  1 Reply Last reply
                                  0
                                  • M mvsri
                                    9 Sept 2020, 09:42

                                    @Christian-Ehrlicher
                                    I think the camera does override the image when i try to load it because when i tested it with static image the code worked perfectly

                                    C Online
                                    C Online
                                    Christian Ehrlicher
                                    Lifetime Qt Champion
                                    wrote on 9 Sept 2020, 09:44 last edited by
                                    #17

                                    @mvsri said in QT QImage Read From Error:

                                    I think the camera does override the image when i try to load it because when i tested it with static image the code worked perfectly

                                    So you've your explanation of the issue - Qt can't do anything against this.

                                    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                                    Visit the Qt Academy at https://academy.qt.io/catalog

                                    M 1 Reply Last reply 9 Sept 2020, 09:47
                                    0
                                    • C Christian Ehrlicher
                                      9 Sept 2020, 09:44

                                      @mvsri said in QT QImage Read From Error:

                                      I think the camera does override the image when i try to load it because when i tested it with static image the code worked perfectly

                                      So you've your explanation of the issue - Qt can't do anything against this.

                                      M Offline
                                      M Offline
                                      mvsri
                                      wrote on 9 Sept 2020, 09:47 last edited by
                                      #18

                                      @Christian-Ehrlicher
                                      yeah i got that, but the thing is if you see the code,
                                      after reading the image i'm deleting the image from the folder, so the chances for image override is very less

                                      C J 2 Replies Last reply 9 Sept 2020, 09:53
                                      0
                                      • M mvsri
                                        9 Sept 2020, 09:47

                                        @Christian-Ehrlicher
                                        yeah i got that, but the thing is if you see the code,
                                        after reading the image i'm deleting the image from the folder, so the chances for image override is very less

                                        C Online
                                        C Online
                                        Christian Ehrlicher
                                        Lifetime Qt Champion
                                        wrote on 9 Sept 2020, 09:53 last edited by
                                        #19

                                        @mvsri Maybe try to rename it before you try to read it.

                                        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                                        Visit the Qt Academy at https://academy.qt.io/catalog

                                        1 Reply Last reply
                                        0
                                        • B Offline
                                          B Offline
                                          Bonnie
                                          wrote on 9 Sept 2020, 10:01 last edited by
                                          #20

                                          Maybe try to find out if that 3rd party camera has other API than save the image to the disk.

                                          1 Reply Last reply
                                          0

                                          1/23

                                          9 Sept 2020, 07:41

                                          • Login

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