Skip to content
Advertisement

Drawing a simple image, displaying it, and closing it

I am trying to do some simple drawings. I wanted to use opencv (cv2) because on a second project I have to display a small animation (rectangle, size depending on a variable; updated every X seconds). However, I do not have experience with image processing libraries and opencv.

I am running into a lot of problems, one of which is that I do not know how to display/close images. The image I am creating is a simple fixation cross, black; on a light gray background:

import numpy as np
import cv2

screen_width = 1024
screen_height = 768
img = np.zeros((screen_height, screen_width, 3), np.uint8) # Black image
img = img + 210 # light gray

screen_center = (screen_width//2, screen_height//2)
rect_width = int(0.2*screen_width)
rect_height = int(0.02*screen_height)

xP1 = screen_center[0] - rect_width//2
yP1 = screen_center[1] + rect_height//2
xP2 = screen_center[0] + rect_width//2
yP2 = screen_center[1] - rect_height//2
cv2.rectangle(img, (xP1, yP1), (xP2, yP2), (0, 0, 0), -1)

xP1 = screen_center[0] - rect_height//2
yP1 = screen_center[1] + rect_width//2
xP2 = screen_center[0] + rect_height//2
yP2 = screen_center[1] - rect_width//2
cv2.rectangle(img, (xP1, yP1), (xP2, yP2), (0, 0, 0), -1)

N.B: If there is a better way to create it, I am also interested :)

My goal is for this first project to do have the following code structure:

img = load_saved_img() # The created fixation cross
display_image()

add_text_to_image('texte to add')
# do stuff
# for several minutes
while something:
    do_this()

remove_text_from_image() # Alternatively, go back to the initial image/change the image
# do stuff
# for several minutes
while something:
    do_this()

close_image()

I know I can add text with cv2.putText() and that I can this way create a second image with the text. What I do not know is how can I manage the displaying of the different images; especially in a light-weight fashion while “doing stuff” on the background. Most people seems to use cv2.waitKey() which is not suited since I do not want to have any user input and since it seems to be something similar to a time.sleep() during which the program is basically paused.

Any tips welcome, even on other libraries and implementation :)

Advertisement

Answer

As proposed by @Miki, the combination of .imshow() and .waitKey(1) is working.

cv2.imshow(window, img)
cv2.waitKey(1)

However, those can not be used with time.sleep() to pause the program. Sometimes, the display will not be updated. For instance, on a 3 second countdown:

import time
import cv2

window = 'Name of the window'

def countdown(window, images):
    """
    images = [image3, image2, image1]
    """
    for img in images:
        cv2.imshow(window, img)
        cv2.waitKey(1)
        time.sleep(1)

Sometimes one of the displays will be skipped. Instead, changing the parameter of cv2.waitKey() to 1000 (timer needed) and removing the use of the time module works best, if no keyboard input is expected during this time.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement