A-nak Wannapaschaiyong
A-nak Wannapaschaiyong

Reputation: 308

imshow seems to show the wrong image (not duplicate)

I am trying to show image using cv2.imshow(), but batch_data (original image) got altered after I concatenate it with data (set of all image). I am note sure how original image is altered.

data = np.array([]).reshape([0, IMG_WIDTH , IMG_HEIGHT ,IMG_DEPTH ])
label = np.array([])
batch_label = np.array([255]).reshape(1) #label number represent background
x = True
for (n,address) in enumerate(address_list):
    print("Reading all images with background from ", address)
    batch_data = cv2.imread(image_dir + address)
    dim = (IMG_WIDTH ,IMG_HEIGHT)
    if batch_data is not None:
        batch_data = cv2.resize(batch_data,dim, interpolation = cv2.INTER_NEAREST)
    else:
        print("batch_data is not read.")

    batch_data = np.expand_dims(batch_data, axis= 0)
    data = np.concatenate((data,batch_data))
    label = np.concatenate((label,batch_label))
    while x:
        print("batch_data.shape",batch_data.shape)
        print("data.shape", data.shape)
        print((np.squeeze(batch_data, axis=0) == data[n,...]).all()) # return true
        cv2.imshow('image', np.squeeze(batch_data, axis= 0)) # show original image
        cv2.imshow('image2', data[n,...]) #show original image but color is alter to white and red
        cv2.waitKey(0)
        cv2.destroyAllWindows()
        x = False

I think cv2.imshow('image2', data[n,...]) show the original image because I tried using transpose to swap axis=1 to axis=2, and the red spot is moved accordingly. I may be wrong.

Can anyone spot the mistake? I feel like it is going to be a very stupid mistake, but I just could not find it.

Upvotes: 0

Views: 259

Answers (1)

Berriel
Berriel

Reputation: 13651

I think this is a data type problem.

Try to change data from float64 to uint8:

data = np.array([], dtype=np.uint8).reshape([0, IMG_WIDTH , IMG_HEIGHT ,IMG_DEPTH])

The white and red are signs that indicate saturation. float64 range is expected to be [0, 1], while uint8 is expected to be [0, 255]. You can find more about this problem here.

Upvotes: 1

Related Questions