Want to save image using OpenCV but Matplotlib insists I save a figure instead

166 views Asked by At

OpenCV 4.7, Matplotlib 3.7.1, Spinnaker-Python 3.0.0.118, Python 3.10, Win 10 x64

I'm acquiring images from a FLIR thermal camera via their Spinnaker API.

I am displaying the images using OpenCV & concurrently displaying a histogram of the images using Matplotlib. The relevant code snippet, (that sits in a function which also contains camera set up & error handling)

cam.BeginAcquisition()
print('Acquiring images...')
print('Press enter to close the program..')

fig = plt.figure(1)
fig.canvas.mpl_connect('close_event', handle_close)
acq_count = 0 # number of saved images
        
# Retrieve and display images
while(continue_recording):
    try:
                
        image_result = cam.GetNextImage(1000)

        #  Ensure image completion
        if image_result.IsIncomplete():
            print('Image incomplete with image status %d ...' % image_result.GetImageStatus())

        else:                    

            # Getting the image data as a numpy array
            image_data = image_result.GetNDArray()
                      
            image_data = cv2.rotate(image_data, cv2.ROTATE_90_COUNTERCLOCKWISE)
            image_data = cv2.flip(image_data, 1)
            image_copy = image_data
                
            count, bins = np.histogram(image_data.flatten())
                        
            cv2.imshow("FLIR", image_data)
                    
            plt.stairs(count, bins)
            plt.pause(0.001)
            plt.clf()
            
            # if 's' pressed then save image data           
            if cv2.waitKey(100) & 0xFF == ord('s'):
                        
                f_name = "test_" + str(acq_count) + '.png'
                cv2.imwrite(f_name, image_data)
                acq_count += 1
                        
            # If user presses enter, close the program
            if keyboard.is_pressed('ENTER'):
                        
                print('Program is closing...')
                cv2.destroyAllWindows()
                plt.close('all')             
                input('Done! Press Enter to exit...')
                continue_recording=False                        

            image_result.Release() # clears camera buffer

        except PySpin.SpinnakerException as ex:
            print('Error: %s' % ex)
            return False

cam.EndAcquisition()

When s is pressed though a blocking popup appears asking me where I want to save figure 1 and then proceeds to save the histogram but not the image!

EDIT :: I should also mention that it all works fine as long as there is no Matplotlib figure.

1

There are 1 answers

0
DrBwts On

As per the discussion in the commets to the OP.

As OpenCV & Matplotlib both have a window open when the keyboard is scanned and an s is pressed Matplotlib's save callback automatically kicks in.

To disable this,

plt.rcParams['keymap.save'].remove('s')

I also got rid of the OpenCV keyboard manager & just used the keyboard libraries keyboard manager.