0
votes

cv2.imshow() does not work properly when the image is small.

import numpy as np
import cv2
im = np.ones((5, 10, 3)) * 255
im = im.astype(np.uint8)
im[:, 5:, :] = np.zeros((5, 5, 3)).astype(np.uint8)
cv2.imshow('1', im)
cv2.imshow('2', cv2.resize(im, dsize=(20, 20), interpolation=cv2.INTER_NEAREST))

touch_img = np.zeros((5, 256, 3))
for i in range(25):
    touch_img[:, i*10 + 6:(i+1)*10 + 6, :] = im
touch_img[:, :6, :] = im[:, 4:, :]

cv2.imshow('3', touch_img)
cv2.imshow('4', cv2.resize(touch_img, dsize=(512, 10), interpolation=cv2.INTER_NEAREST))

im = np.ones((1, 2, 3)) * 255
im = im.astype(np.uint8)
im[:, 1:, :] = np.zeros((1, 1, 3)).astype(np.uint8)
cv2.imshow('5', im)
cv2.waitKey(0)

These code will create 5 windows to show the different images. However, I found the following problems of these images' show results.

  1. window 1 does not show properly;

  2. window 2 shows properly;

  3. window 3 does not show properly, because the rightest of image has some unexpected white region.

  4. window 4 works properly. problem-img

1

1 Answers

0
votes

If the problem is coming from the image being too small, add the following line before your cv2.imshow('window_name', image) lines:

cv2.namedWindow('window_name', cv2.WINDOW_NORMAL)

Then you can resize it to whatever you want. Reference: https://docs.opencv.org/2.4/modules/highgui/doc/user_interface.html?highlight=namedwindow