How to show inRange in 3 dimensional array?

Question:

So I wanted to make a side by side video comparison. On the left side will be my original video and the right side will be the mask. I create a large blank image with np.zeros to store this two images. My problem is the mask image is in 2 dimensional array, and the original is in 3 dimensional. My code look like this:

    _, videoCam= vid.read()
    f_height,f_width,_=videoCam.shape
    hsv = cv.cvtColor(videoCam, cv.COLOR_BGR2HSV)

    ## mask of green (36,0,0) ~ (70, 255,255)
    mask1 = cv.inRange(hsv, (36, 0, 0), (70, 255,255))
    cv.bitwise_not(mask1)

    blank_image = np.zeros((f_height,f_width*2,3), np.uint8)

    blank_image[:,0:f_width] = hsv 
    blank_image[:,f_width:f_width*2] = mask1 #ValueError: could not broadcast input array from shape (480,640) into shape (480,640,3)
    
    cv.imshow('',blank_image)

I think i should convert the 2 dimensional mask to 3. But how to do this? what to fill in the empty dimension?

Any help or suggestions would be greatly appreciated

Asked By: Flitzcore

||

Answers:

You can do:

blank_image[:,f_width:f_width*2] = mask1[:,:,None]

or

blank_image[:,f_width:f_width*2] = mask1[:,:,np.newaxis]

This will results in mask1 values to be copied over 3 dimensions when being assigned to blank_image (meaning a RGB image with equal R, G and B at each pixel)

Answered By: PlainRavioli
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.