0

I have a single channel image with shape (1024,1024).

I want to overlay it with a color image of shape (1024,1024,3) using dst = cv2.addWeighted(im, 1, newimg, 1, 0)

So First, I want to know, how can I convert newimg to 1024x1024x3...Because im is of shape 1024x1024x3

fmw42
  • 46,825
  • 10
  • 62
  • 80
John Bott
  • 375
  • 3
  • 13
  • Here is one approach I found by Googling: https://www.quora.com/How-do-I-convert-a-grayscale-image-to-an-RGB-image-in-Python. Here is another using numpy, https://stackoverflow.com/questions/39463019/how-to-copy-numpy-array-value-into-higher-dimensions – fmw42 Aug 31 '19 at 05:33
  • Here is my comment again, but without the link. But see my answer below also. --- Have you considered just doing a Google search before asking on this forum? That often will find numerous solutions to most problems, for example, that type of questions has been asked before on this forum and elsewhere. – fmw42 Aug 31 '19 at 05:43

2 Answers2

3

This seems to be one simple solution using numpy.

Create a 3-channel black image

Put your same grayscale image into each channel


newimage = np.zeros((grayimage.shape[0],grayimage.shape[1],3))
newimage[:,:,0] = grayimage
newimage[:,:,1] = grayimage
newimage[:,:,2] = grayimage

See how to copy numpy array value into higher dimensions

fmw42
  • 46,825
  • 10
  • 62
  • 80
  • Worked. Can you also answer how to overlay two cv2 images? I tried `dst = cv2.addWeighted(im, 1, newimage, 1, 0)` – John Bott Aug 31 '19 at 05:46
  • but got this error `error: OpenCV(4.1.0) C:\projects\opencv-python\opencv\modules\core\src\arithm.cpp:687: error: (-5:Bad argument) Whn the input arrays in add/subtract/multiply/divide functions have different types, the output array type must be explicitly specified in function 'cv::arithm_op'` – John Bott Aug 31 '19 at 05:47
  • Googling again gives numerous examples using addWeight. See https://docs.opencv.org/3.4/d5/dc4/tutorial_adding_images.html and https://www.pyimagesearch.com/2016/03/07/transparent-overlays-with-opencv/ and https://stackoverflow.com/questions/39250226/overlay-two-images-without-losing-the-intensity-of-the-color-using-opencv-and-py. Be sure both images have the same size (width and height and channels) and type. It looks like the message is saying that your two images have different types. – fmw42 Aug 31 '19 at 05:50
  • What about types? Is one float and the other 8-bit? What image formats are you input images? How have you read them in? Please show your code. That is important for this forum. See the help section on how to ask a good question and what are good questions. The link is under the ? button at the top right corner of the window. – fmw42 Aug 31 '19 at 05:53
  • 1
    `newimage = np.dstack((grey, grey, grey))` is about 8x faster, but if OP already has **OpenCV** installed then `newimage = cv2.cvtColor(grey,cv2.COLOR_GRAY2BGR)` is around 30x faster - because of SIMD optimisation. – Mark Setchell Aug 31 '19 at 09:53
  • Many ways to "skin the cat"! Thanks for pointing others out along with speed comparisons, Mark. – fmw42 Aug 31 '19 at 18:13
  • If your single channel grayscale image is `image` with shape `(1024,1024)`. I believe another approach is `result = cv2.merge([image, image, image])`. This should result in shape `(1024,1024,3)` – nathancy Sep 03 '19 at 21:13
0
mask = cv2.inRange(frame, lower_blue, upper_blue)
print(mask.shape)
mask2 = cv2.cvtColor(mask, cv2.COLOR_GRAY2BGR)
print(mask2.shape)

Output:
(480, 640)
(480, 640, 3)

This converts image into 3 channel.

Ruli
  • 2,592
  • 12
  • 30
  • 40
sourab maity
  • 1,025
  • 2
  • 8
  • 16