xhenryx14
xhenryx14

Reputation: 754

Why OpenCV shows an empty window instead of an image?

I'm using Python and openCV in linux. The program should read the camera and be able to change trackbars of min and max h,s and v color ranges to segment the frame in real-time. After the segmentation it should display that image in another window. My problem is that the segmentation is displayed as an empty window.

here is part of my code:

while(True):

    if(not pause):
        ret, current_img = video.read()
        new_range(0)
        cv2.imshow("Video", current_img)

    key = cv2.waitKey(33)
    if key==ord(' '):
        pause = not pause
    elif key == 27:
        break

that is the loop for the video and the function that segments and then displays the image is:

def new_range(x):
if(current_img != 0):

    h_min = cv2.getTrackbarPosition("Bars", "min_hue")
    h_max = cv2.getTrackbarPosition("Bars", "max_hue")

    s_min = cv2.getTrackbarPosition("Bars", "min_saturation")
    s_max = cv2.getTrackbarPosition("Bars", "max_saturartion")

    v_min = cv2.getTrackbarPosition("Bars", "min_saturation")
    v_max = cv2.getTrackbarPosition("Bars", "max_saturartion")

    min_hsv = np.array([h_min, s_min, v_min])
    max_hsv = np.array([h_max, s_max, v_max])
    hsv = cv2.cvtColor(current_img, cv2.COLOR_BGR2HSV)
    bw = cv2.inRange(current_img, min_hsv, max_hsv)
    cv2.imshow("Segmentation", bw)
    cv2.waitKey(0)

help? Thanks!

Upvotes: 0

Views: 2057

Answers (2)

xhenryx14
xhenryx14

Reputation: 754

I fixed it! it was something very simple current_img wasn't global so I wrote global current_img at the beginning of the function new_range(x). Thanks!

Upvotes: 0

The Nomadic Coder
The Nomadic Coder

Reputation: 650

Convert the image back to BGR format

bgr = cv2.cvtColor(hsv, cv2.COLOR_HSV2BGR)

Upvotes: 1

Related Questions