Cv2.imshow and numpy.dstack core dumped

I am trying to put two images together, so I can show both in one window. The first image is an original three-channel image, the second is a gray version. I did cv2.cvtColor color conversion, converted back to 3-channel with numpy.dstack , and when I try to execute cv2.imshow it gives me an error with an explicit reset . Am I missing something? I need both images to have the same number of channels to stack them with numpy.hstack. This happens on a 64-bit Ubuntu machine.

import cv2 import numpy as np img = cv2.imread("/home/bernie/Dropbox/Python/Opencv/lena512.jpg") gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) gray = np.dstack((gray,gray,gray)) #res = np.hstack((img,gray)) print gray.dtype print gray.shape cv2.imshow('gray',gray) #cv2.imshow('res',res) cv2.waitKey() 

Adding

On the other hand, using

 gray = cv2.cvtColor(gray,cv2.COLOR_GRAY2BGR) 

line 7 works without complaint, so I will stick to this for now. This means that there is a difference between the result of cv2.cvtColor and the result of numpy.dstack to convert a 1-channel image to a 3-channel image with equal values.

+4
source share
1 answer

As pointed out in the comments, try using cv2.merge , since it seems to be different from np.dstack :

 gray = cv2.merge([gray]*3) 

See <fraxel for more information.

+1
source

All Articles