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 threading and opencv
Forum Updated to NodeBB v4.3 + New Features

Issue with threading and opencv

Scheduled Pinned Locked Moved Solved General and Desktop
41 Posts 3 Posters 9.5k 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.
  • K Offline
    K Offline
    Kira
    wrote on 12 Jun 2019, 04:58 last edited by aha_1980 6 Dec 2019, 13:06
    #1

    Hello All,
    I have written a program in opencv to fetch an image form camera and save in it folder.
    There is another program which takes the program form the same folder and do some computation of the image.
    I combined both the program and put the second logic in thread. Now the problem is what is figured is:
    The main program tries to save the image in the same program and at the same time the thread tries to read different image form the same folder. So sometimes the image is saved but only half of the image is saved and other half contains no data. Also the thread breaks.
    I tried using QMutex but the issue was the thread break if the main thread is already lock.
    The summary of the problem is:
    ->How can i provide synchronization between the thread.
    ->How should i make the other thread to wait until the lock is not released.

    J 1 Reply Last reply 12 Jun 2019, 05:01
    0
    • K Kira
      12 Jun 2019, 04:58

      Hello All,
      I have written a program in opencv to fetch an image form camera and save in it folder.
      There is another program which takes the program form the same folder and do some computation of the image.
      I combined both the program and put the second logic in thread. Now the problem is what is figured is:
      The main program tries to save the image in the same program and at the same time the thread tries to read different image form the same folder. So sometimes the image is saved but only half of the image is saved and other half contains no data. Also the thread breaks.
      I tried using QMutex but the issue was the thread break if the main thread is already lock.
      The summary of the problem is:
      ->How can i provide synchronization between the thread.
      ->How should i make the other thread to wait until the lock is not released.

      J Offline
      J Offline
      J.Hilk
      Moderators
      wrote on 12 Jun 2019, 05:01 last edited by
      #2

      Hi @Kira

      a solution doesn't quit come to my mind, but I have to ask, why do save the image in the HD first ? Seems like an unnecessary step ?


      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.

      K 1 Reply Last reply 12 Jun 2019, 05:07
      2
      • J J.Hilk
        12 Jun 2019, 05:01

        Hi @Kira

        a solution doesn't quit come to my mind, but I have to ask, why do save the image in the HD first ? Seems like an unnecessary step ?

        K Offline
        K Offline
        Kira
        wrote on 12 Jun 2019, 05:07 last edited by
        #3

        @J.Hilk : Yes i agree with you its unnessary to save the image. In future we will store it internally in queue. But this is an interesting use case. I wanted to know the problem exactly so that i can avoid it in near future.

        J 1 Reply Last reply 12 Jun 2019, 05:12
        0
        • K Kira
          12 Jun 2019, 05:07

          @J.Hilk : Yes i agree with you its unnessary to save the image. In future we will store it internally in queue. But this is an interesting use case. I wanted to know the problem exactly so that i can avoid it in near future.

          J Offline
          J Offline
          J.Hilk
          Moderators
          wrote on 12 Jun 2019, 05:12 last edited by J.Hilk 6 Dec 2019, 05:19
          #4

          @Kira how do you save the file, also with Qt ? Than maybe QSaveFile will help

          https://doc.qt.io/qt-5/qsavefile.html#details


          That said, maybe using QFileSystemWatcher to monitor your saved file should also reduce the error frequency ?


          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.

          K 1 Reply Last reply 12 Jun 2019, 05:21
          0
          • J J.Hilk
            12 Jun 2019, 05:12

            @Kira how do you save the file, also with Qt ? Than maybe QSaveFile will help

            https://doc.qt.io/qt-5/qsavefile.html#details


            That said, maybe using QFileSystemWatcher to monitor your saved file should also reduce the error frequency ?

            K Offline
            K Offline
            Kira
            wrote on 12 Jun 2019, 05:21 last edited by
            #5

            @J.Hilk : I am using imwrite an opencv function to save the image.
            Actually the problem arise after i start using thread. So i just want to know how the thread is causing the issue.

            J 1 Reply Last reply 12 Jun 2019, 05:30
            0
            • K Kira
              12 Jun 2019, 05:21

              @J.Hilk : I am using imwrite an opencv function to save the image.
              Actually the problem arise after i start using thread. So i just want to know how the thread is causing the issue.

              J Offline
              J Offline
              jsulm
              Lifetime Qt Champion
              wrote on 12 Jun 2019, 05:30 last edited by
              #6

              @Kira The thread writing the file should emit a signal when the file was written and closed. The thread consuming files should only read files which were already notified through that signal. This way you would avoid reading and writing same file from two different threads at the same time.

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              K 1 Reply Last reply 12 Jun 2019, 07:02
              2
              • J jsulm
                12 Jun 2019, 05:30

                @Kira The thread writing the file should emit a signal when the file was written and closed. The thread consuming files should only read files which were already notified through that signal. This way you would avoid reading and writing same file from two different threads at the same time.

                K Offline
                K Offline
                Kira
                wrote on 12 Jun 2019, 07:02 last edited by
                #7

                @jsulm : Just one question for clarification.
                If i use two different mutex, will the one mutex will inform about other if i tried to lock ?
                Or should i use same mutex where the conflicting situation is expected ?
                Also i was going through opencv i got the following warning which was not seen before:
                Warning:Premature end of JPEG file

                J 1 Reply Last reply 12 Jun 2019, 07:08
                0
                • K Kira
                  12 Jun 2019, 07:02

                  @jsulm : Just one question for clarification.
                  If i use two different mutex, will the one mutex will inform about other if i tried to lock ?
                  Or should i use same mutex where the conflicting situation is expected ?
                  Also i was going through opencv i got the following warning which was not seen before:
                  Warning:Premature end of JPEG file

                  J Offline
                  J Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on 12 Jun 2019, 07:08 last edited by
                  #8

                  @Kira said in Issue with threading and opecv:

                  If i use two different mutex, will the one mutex will inform about other if i tried to lock ?

                  No.
                  But with the solution I suggested there is no need at all for any mutexes. Just use signals/slots as I suggested with queued connection type.

                  "Warning:Premature end of JPEG file" - I guess it's because the file is damaged.

                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                  K 1 Reply Last reply 12 Jun 2019, 07:57
                  2
                  • J jsulm
                    12 Jun 2019, 07:08

                    @Kira said in Issue with threading and opecv:

                    If i use two different mutex, will the one mutex will inform about other if i tried to lock ?

                    No.
                    But with the solution I suggested there is no need at all for any mutexes. Just use signals/slots as I suggested with queued connection type.

                    "Warning:Premature end of JPEG file" - I guess it's because the file is damaged.

                    K Offline
                    K Offline
                    Kira
                    wrote on 12 Jun 2019, 07:57 last edited by
                    #9

                    @jsulm : Thanks for highlighting the possibility of signal/slots.
                    I will have to redesign my approach for implementing the same.
                    Just couple of question:
                    -> If i queue signal and slot will it affect my overall program.
                    ie. Suppose i queue the signal/slots related to this program will it affect the other signal/slots
                    written in my program . For ex: delay in execution of the signal/slots of the main program.
                    -> How to make a mutex wait for the previous lock if we are not sure how much time it is going to take.

                    J 1 Reply Last reply 12 Jun 2019, 08:03
                    0
                    • K Kira
                      12 Jun 2019, 07:57

                      @jsulm : Thanks for highlighting the possibility of signal/slots.
                      I will have to redesign my approach for implementing the same.
                      Just couple of question:
                      -> If i queue signal and slot will it affect my overall program.
                      ie. Suppose i queue the signal/slots related to this program will it affect the other signal/slots
                      written in my program . For ex: delay in execution of the signal/slots of the main program.
                      -> How to make a mutex wait for the previous lock if we are not sure how much time it is going to take.

                      J Offline
                      J Offline
                      jsulm
                      Lifetime Qt Champion
                      wrote on 12 Jun 2019, 08:03 last edited by jsulm 6 Dec 2019, 08:03
                      #10

                      @Kira said in Issue with threading and opecv:

                      Suppose i queue the signal/slots related to this program will it affect the other signal/slots
                      written in my program . For ex: delay in execution of the signal/slots of the main program.

                      Queued signals do not block anything - they are just in a queue. The only delay you will have is the slot when it is actually called - but that you will have anyway as you have to execute the code anyway, right?

                      "How to make a mutex wait for the previous lock if we are not sure how much time it is going to take" - I don't understand this question. A mutex either blocks the thread which wants to lock if the mutex is already locked, or you can continue immediately. So, you already have this "waiting" if the mutex is already locked.

                      https://forum.qt.io/topic/113070/qt-code-of-conduct

                      K 1 Reply Last reply 12 Jun 2019, 09:19
                      3
                      • J jsulm
                        12 Jun 2019, 08:03

                        @Kira said in Issue with threading and opecv:

                        Suppose i queue the signal/slots related to this program will it affect the other signal/slots
                        written in my program . For ex: delay in execution of the signal/slots of the main program.

                        Queued signals do not block anything - they are just in a queue. The only delay you will have is the slot when it is actually called - but that you will have anyway as you have to execute the code anyway, right?

                        "How to make a mutex wait for the previous lock if we are not sure how much time it is going to take" - I don't understand this question. A mutex either blocks the thread which wants to lock if the mutex is already locked, or you can continue immediately. So, you already have this "waiting" if the mutex is already locked.

                        K Offline
                        K Offline
                        Kira
                        wrote on 12 Jun 2019, 09:19 last edited by
                        #11

                        @jsulm
                        Queued signals do not block anything - they are just in a queue. The only delay you will have is the slot when it is actually called - but that you will have anyway as you have to execute the code anyway, right? : Yes

                        Thanks for the help cleared a lot of implementation doubts.
                        Just a small question. If i have a thread which acquires the lock and there is one more thread which requests for the locked resource. How will i tell the other thread wait until and unless the lock in the thread have not been unlocked.
                        We can currently use the wait time but is there any way to tell when create a notification when the lock is released and tell the other thread to wait for that specific time.

                        And one more thing how to highlight specific lines which we are answering. Which you have did for my earlier questions. :)

                        J 1 Reply Last reply 12 Jun 2019, 10:24
                        0
                        • K Kira
                          12 Jun 2019, 09:19

                          @jsulm
                          Queued signals do not block anything - they are just in a queue. The only delay you will have is the slot when it is actually called - but that you will have anyway as you have to execute the code anyway, right? : Yes

                          Thanks for the help cleared a lot of implementation doubts.
                          Just a small question. If i have a thread which acquires the lock and there is one more thread which requests for the locked resource. How will i tell the other thread wait until and unless the lock in the thread have not been unlocked.
                          We can currently use the wait time but is there any way to tell when create a notification when the lock is released and tell the other thread to wait for that specific time.

                          And one more thing how to highlight specific lines which we are answering. Which you have did for my earlier questions. :)

                          J Offline
                          J Offline
                          jsulm
                          Lifetime Qt Champion
                          wrote on 12 Jun 2019, 10:24 last edited by jsulm 6 Dec 2019, 10:24
                          #12

                          @Kira said in Issue with threading and opecv:

                          How will i tell the other thread wait until

                          That's exactly what a mutex is doing: it blocks the thread which tries to lock a mutex which is already locked by other thread, as soon as the other thread releases the mutex the OS will wake up one of the blocked (waiting) threads. So, you do not have to tell the thread anything.

                          https://forum.qt.io/topic/113070/qt-code-of-conduct

                          K 1 Reply Last reply 13 Jun 2019, 06:28
                          2
                          • J jsulm
                            12 Jun 2019, 10:24

                            @Kira said in Issue with threading and opecv:

                            How will i tell the other thread wait until

                            That's exactly what a mutex is doing: it blocks the thread which tries to lock a mutex which is already locked by other thread, as soon as the other thread releases the mutex the OS will wake up one of the blocked (waiting) threads. So, you do not have to tell the thread anything.

                            K Offline
                            K Offline
                            Kira
                            wrote on 13 Jun 2019, 06:28 last edited by
                            #13

                            @jsulm : Hello i have implemented the save image function in a slot as suggested by you.
                            But the problem is that the image is not being saved. I would like to share the given code:

                            signals:
                            void signalSaveImage(cv::Mat,String row,String column);
                            slots:
                            public slots:
                            void saveImage(Mat finalImage,String row, String column);
                            Slot Definition:

                            void MainWindow::saveImage(Mat finalImage, String row, String column)
                            {
                                cout<<"Inside image save function"<<endl;
                                cout<<"Row"<<row<<endl;
                                cout<<"Column"<<column<<endl;
                                String imagePath = "D:/testN/"
                                String imageFormat = ".bmp";
                                try {
                                       cv::imwrite(imagePath+row+"_"+column+imageFormat,finalImage);
                                   }
                                   catch (cv::Exception& ex) {
                                       fprintf(stderr, "Exception in saving image: %s\n", ex.what());
                                   }
                                   fprintf(stdout, "Saved PNG file with alpha data.\n");
                            
                                cout<<"\n Entering inside save mutex"<<endl;
                            
                                cout<<"\n Completed image save function"<<endl;
                            
                            
                            }
                            

                            Signal to slot connection:
                            connect(this,SIGNAL(signalSaveImage(cv::Mat,String,String)),this,SLOT(saveImage(Mat,String,String)),Qt::QueuedConnection);

                            MainWindow.cpp
                            Emitting signal to save image:
                            emit signalSaveImage(image,to_string(row),to_string(column));

                            But my problem is that my images are not being saved.

                            J 1 Reply Last reply 13 Jun 2019, 06:34
                            0
                            • K Kira
                              13 Jun 2019, 06:28

                              @jsulm : Hello i have implemented the save image function in a slot as suggested by you.
                              But the problem is that the image is not being saved. I would like to share the given code:

                              signals:
                              void signalSaveImage(cv::Mat,String row,String column);
                              slots:
                              public slots:
                              void saveImage(Mat finalImage,String row, String column);
                              Slot Definition:

                              void MainWindow::saveImage(Mat finalImage, String row, String column)
                              {
                                  cout<<"Inside image save function"<<endl;
                                  cout<<"Row"<<row<<endl;
                                  cout<<"Column"<<column<<endl;
                                  String imagePath = "D:/testN/"
                                  String imageFormat = ".bmp";
                                  try {
                                         cv::imwrite(imagePath+row+"_"+column+imageFormat,finalImage);
                                     }
                                     catch (cv::Exception& ex) {
                                         fprintf(stderr, "Exception in saving image: %s\n", ex.what());
                                     }
                                     fprintf(stdout, "Saved PNG file with alpha data.\n");
                              
                                  cout<<"\n Entering inside save mutex"<<endl;
                              
                                  cout<<"\n Completed image save function"<<endl;
                              
                              
                              }
                              

                              Signal to slot connection:
                              connect(this,SIGNAL(signalSaveImage(cv::Mat,String,String)),this,SLOT(saveImage(Mat,String,String)),Qt::QueuedConnection);

                              MainWindow.cpp
                              Emitting signal to save image:
                              emit signalSaveImage(image,to_string(row),to_string(column));

                              But my problem is that my images are not being saved.

                              J Offline
                              J Offline
                              J.Hilk
                              Moderators
                              wrote on 13 Jun 2019, 06:34 last edited by
                              #14

                              @Kira said in Issue with threading and opencv:

                              QueuedConnection

                              forces a copy of the arguments, The MetaSystem doesn't know the type cv::Mat -> can't be transmitted via QueuedConnection.

                              Check your console output you should have warning of unknown metatyps (declare and/or register)


                              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.

                              K 1 Reply Last reply 13 Jun 2019, 07:03
                              2
                              • J J.Hilk
                                13 Jun 2019, 06:34

                                @Kira said in Issue with threading and opencv:

                                QueuedConnection

                                forces a copy of the arguments, The MetaSystem doesn't know the type cv::Mat -> can't be transmitted via QueuedConnection.

                                Check your console output you should have warning of unknown metatyps (declare and/or register)

                                K Offline
                                K Offline
                                Kira
                                wrote on 13 Jun 2019, 07:03 last edited by
                                #15

                                @J.Hilk : I have registered it.
                                #include "mainwindow.h"
                                #include <QApplication>

                                int main(int argc, char *argv[])
                                {
                                qRegisterMetaType< cv::Mat >("cv::Mat");
                                QApplication a(argc, argv);
                                MainWindow w;
                                w.show();

                                return a.exec();
                                

                                }

                                J 1 Reply Last reply 13 Jun 2019, 07:08
                                0
                                • K Kira
                                  13 Jun 2019, 07:03

                                  @J.Hilk : I have registered it.
                                  #include "mainwindow.h"
                                  #include <QApplication>

                                  int main(int argc, char *argv[])
                                  {
                                  qRegisterMetaType< cv::Mat >("cv::Mat");
                                  QApplication a(argc, argv);
                                  MainWindow w;
                                  w.show();

                                  return a.exec();
                                  

                                  }

                                  J Offline
                                  J Offline
                                  J.Hilk
                                  Moderators
                                  wrote on 13 Jun 2019, 07:08 last edited by
                                  #16

                                  @Kira

                                  the data goes most likely out of scope, as the copy constructor of cv::Mat does actually no coping

                                  0_1560409629873_2c331f8b-46e4-4408-8567-abe5ac4169e3-image.png

                                  I had the same issues in my time with opencv.

                                  ended up converting it to a Image before transferring it from the opencv thread to the normal one.


                                  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.

                                  K 1 Reply Last reply 13 Jun 2019, 08:01
                                  3
                                  • J J.Hilk
                                    13 Jun 2019, 07:08

                                    @Kira

                                    the data goes most likely out of scope, as the copy constructor of cv::Mat does actually no coping

                                    0_1560409629873_2c331f8b-46e4-4408-8567-abe5ac4169e3-image.png

                                    I had the same issues in my time with opencv.

                                    ended up converting it to a Image before transferring it from the opencv thread to the normal one.

                                    K Offline
                                    K Offline
                                    Kira
                                    wrote on 13 Jun 2019, 08:01 last edited by
                                    #17

                                    @J.Hilk : Just for clarification should i replace this part with cv::clone
                                    void saveImage(Mat finalImage,String row, String column);

                                    1 Reply Last reply
                                    0
                                    • K Offline
                                      K Offline
                                      Kira
                                      wrote on 13 Jun 2019, 09:50 last edited by
                                      #18

                                      @J-Hilk : will try and also i tried to run the program in debug mode and getting the following error.
                                      0_1560418863054_65dbf469-08ad-429c-bed7-3a5997fe0246-image.png

                                      Debugger redirects to following line of file of ostream:
                                      // MANIPULATORS
                                      template<class _Elem,
                                      class _Traits> inline
                                      basic_ostream<_Elem, _Traits>&
                                      __CLRCALL_OR_CDECL endl(basic_ostream<_Elem, _Traits>& _Ostr)
                                      { // insert newline and flush stream
                                      _Ostr.put(_Ostr.widen('\n'));
                                      _Ostr.flush();
                                      return (_Ostr);
                                      }

                                      0_1560419407453_ee0e3e1d-16a4-4a24-8b13-2cf3c1db7c66-image.png

                                      And the program quits.
                                      Can u please explain what may be the relevant reason for error

                                      J 1 Reply Last reply 13 Jun 2019, 10:54
                                      0
                                      • K Kira
                                        13 Jun 2019, 09:50

                                        @J-Hilk : will try and also i tried to run the program in debug mode and getting the following error.
                                        0_1560418863054_65dbf469-08ad-429c-bed7-3a5997fe0246-image.png

                                        Debugger redirects to following line of file of ostream:
                                        // MANIPULATORS
                                        template<class _Elem,
                                        class _Traits> inline
                                        basic_ostream<_Elem, _Traits>&
                                        __CLRCALL_OR_CDECL endl(basic_ostream<_Elem, _Traits>& _Ostr)
                                        { // insert newline and flush stream
                                        _Ostr.put(_Ostr.widen('\n'));
                                        _Ostr.flush();
                                        return (_Ostr);
                                        }

                                        0_1560419407453_ee0e3e1d-16a4-4a24-8b13-2cf3c1db7c66-image.png

                                        And the program quits.
                                        Can u please explain what may be the relevant reason for error

                                        J Offline
                                        J Offline
                                        jsulm
                                        Lifetime Qt Champion
                                        wrote on 13 Jun 2019, 10:54 last edited by
                                        #19

                                        @Kira Please post the stack trace after crash

                                        https://forum.qt.io/topic/113070/qt-code-of-conduct

                                        K 1 Reply Last reply 13 Jun 2019, 12:02
                                        0
                                        • J jsulm
                                          13 Jun 2019, 10:54

                                          @Kira Please post the stack trace after crash

                                          K Offline
                                          K Offline
                                          Kira
                                          wrote on 13 Jun 2019, 12:02 last edited by
                                          #20

                                          @jsulm : I thought this is the stack trace after crash

                                          J 1 Reply Last reply 13 Jun 2019, 12:03
                                          0

                                          1/41

                                          12 Jun 2019, 04:58

                                          • Login

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