1

I have multiple threads (one for each camera) that needs to imshow images. However, I run into an error with the second NamedWindow command. I read some of the previous Q/A and realize that highgui and multithreading don't go too well together, however, in this case I need to run it from a thread. I'm ok with displaying one of the image streams (and then press a button to get to the next one).

I'm not too inclined to concatenate images together and pass it to the main thread (where it seems most people suggest to keep high gui calls) as I don't want to use any more resources than I have to.

Moreover, imshow in a separate thread works fine for me (for a single camera). I tried running separate programs in different shells (not threads but processes this time) and it seemed to work - is there a way to create a separate process in OpenCV for displaying image streams simultaneously? Is it possible to show multiple image streams in different threads?

nathancy
  • 42,661
  • 14
  • 115
  • 137
Bill Quesy
  • 83
  • 1
  • 7
  • I have never managed to run `imshow()` in multiple threads - despite much trying. I have generally had to resort to using a single thread for all `imshow()s` and `waitKey()s`. It's easy enough to put image processing in other threads, just not display it seems. If anyone has an example that works, please ping me too! – Mark Setchell Apr 17 '19 at 21:16

1 Answers1

0

One way to display multiple image streams simultaneously is to create separate objects with each object having its own thread polling images from a different camera. You can then stitch these images into a single panel to display all frames. A single imshow panel can be used to display these frames after grabbing each thread's camera frame. A simple panel with 4 camera frames:

4 panel camera frame

Panel code:

import cv2
import numpy as np

image = cv2.imread('placeholder5.PNG')

# Note all frames must be of the same size
image = cv2.resize(image, (0, 0), None, .4, .4)

grey = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
grey_3_channel = cv2.cvtColor(grey, cv2.COLOR_GRAY2BGR)

numpy_horizontal_row1 = np.hstack((image, grey_3_channel))
numpy_horizontal_row2 = np.hstack((grey_3_channel, image))

combined_images = np.concatenate((numpy_horizontal_row1, numpy_horizontal_row2), axis=0)

cv2.imshow('Image panel', combined_images)
cv2.waitKey()

For instance, I have a single imshow panel to display 4 different cameras with each camera having its own thread to poll frames.

Camera example with 4 frames

nathancy
  • 42,661
  • 14
  • 115
  • 137
  • i will be testing this out as soon as I can, thanks! – Bill Quesy Mar 15 '19 at 14:44
  • Glad to help. Don't forget to accept if it works for you – nathancy Mar 15 '19 at 20:45
  • Still waiting for the cameras to come in :) – Bill Quesy Mar 18 '19 at 21:34
  • @BillQuesy this is a rather strange statement, given that you chose to *open* a question in the first place – desertnaut Apr 17 '19 at 19:21
  • Yeah I know, I had the cameras initially but then they were taken to a site to test (and kept there since they're still testing them). So far, I haven't seen anything to indicate that this solution works. The problem (I'm speculating here) seems to be with the X11 backend. Threading for imshow on X11 seems to have a lot of issues (the same ones I'm having...). – Bill Quesy Apr 17 '19 at 20:15
  • Moreover, I did explicitly ask for a solution that didn't stitch the images together (which is the only reason I'm a bit hesitant to mark this solution as solved) @desertnaut – Bill Quesy Apr 17 '19 at 20:18