【问题标题】:How to convert (1024,1024) shape image to (1024,1024,3) in OpenCV如何在 OpenCV 中将 (1024,1024) 形状图像转换为 (1024,1024,3)
【发布时间】:2019-08-31 05:20:48
【问题描述】:

我有一个形状为 (1024,1024) 的单通道图像。

我想用形状 (1024,1024,3) 的彩色图像覆盖它 dst = cv2.addWeighted(im, 1, newimg, 1, 0)

首先,我想知道,如何将 newimg 转换为 1024x1024x3...因为im 的形状为 1024x1024x3

【问题讨论】:

  • 这是我通过谷歌搜索找到的一种方法:quora.com/…。这是另一个使用 numpy,stackoverflow.com/questions/39463019/…
  • 这是我的评论,但没有链接。但也请参阅下面的我的回答。 --- 你有没有考虑过在这个论坛上提问之前只做一个谷歌搜索?这通常会为大多数问题找到许多解决方案,例如,以前在这个论坛和其他地方已经提出过这类问题。

标签: python opencv image-processing


【解决方案1】:

这似乎是使用 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

how to copy numpy array value into higher dimensions

【讨论】:

  • 工作。您还可以回答如何叠加两个 cv2 图像吗?我试过dst = cv2.addWeighted(im, 1, newimage, 1, 0)
  • 但收到此错误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'
  • 谷歌搜索再次给出了许多使用 addWeight 的示例。请参阅 docs.opencv.org/3.4/d5/dc4/tutorial_adding_images.htmlpyimagesearch.com/2016/03/07/transparent-overlays-with-opencvstackoverflow.com/questions/39250226/…。确保两个图像具有相同的大小(宽度和高度以及通道)和类型。消息似乎是在说您的两个图像具有不同的类型。
  • 类型呢?一个浮点数和另一个 8 位?您输入的图像是什么图像格式?你是怎么读进去的?请出示您的代码。这对这个论坛很重要。请参阅帮助部分,了解如何提出好问题以及什么是好问题。链接在 ?窗口右上角的按钮。
  • newimage = np.dstack((grey, grey, grey)) 大约快 8 倍,但如果 OP 已经安装了 OpenCV,那么 newimage = cv2.cvtColor(grey,cv2.COLOR_GRAY2BGR) 大约快 30 倍 - 因为 SIMD 优化。
【解决方案2】:
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)

这会将图像转换为 3 通道。

【讨论】:

    猜你喜欢
    • 2020-05-01
    • 2020-01-07
    • 2017-02-18
    • 1970-01-01
    • 2020-04-11
    • 2014-10-18
    • 2017-02-10
    • 1970-01-01
    • 2011-10-09
    相关资源
    最近更新 更多