I am trying to stack two images together, so i can show both in a single window. First image is the original, 3-channel image, second one is a gray version. I did the color conversion with cv2.cvtColor, transformed back to 3-channel with numpy.dstack, and when i try cv2.imshow, it gives me a "core dumped" error. 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 Ubuntu 64bit 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()
On the other hand, using
gray = cv2.cvtColor(gray,cv2.COLOR_GRAY2BGR)
in line 7 works without complaints, so i will stick to this for now. This means that there is a difference between the cv2.cvtColor result and numpy.dstack result for turning a 1-channel image to 3-channel with equal values.
As suggested in the comments, try using cv2.merge
since apparently it's strided differently from np.dstack
:
gray = cv2.merge([gray]*3)
See @fraxel's link for more info
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With