【问题标题】:how to create an gray-scale image from existing 2D array data in python如何从python中现有的二维数组数据创建灰度图像
【发布时间】:2016-10-20 14:53:26
【问题描述】:

我在 python 中从现有的二维数组创建灰度图像时遇到问题。

假设我有一个二维数组,尺寸为 504x896 的 X 数据类型是 unit8。如何从这个二维数组创建灰度图像? OpenCV 会为其提供功能还是我必须包含其他图像处理库?


我已经尝试过了,但由于某种原因它不起作用。给定一个二维数组 z

dim = z.shape
h =  dim[0]
w = dim[1]

copy_image = np.zeros((h,w,1), np.uint8)
copy_image = z.copy();
cv2.imwrite("cpImage.bmp",copy_image)

【问题讨论】:

标签: python opencv image-processing


【解决方案1】:

这应该可以按预期工作。我使用 Float32 作为两个参数 cv.CvtColor 必须具有相同的深度。

import numpy as np
import cv2
nArray = np.zeros((504, 896), np.float32) #Create the arbitrary input img
h,w = nArray.shape
cArray1 = cv2.CreateMat(h, w, cv2.CV_32FC3)
cArray2 = cv2.fromarray(nArray)
cv2.CvtColor(cArray2, cArray1, cv2.CV_GRAY2BGR)
cv2.imwrite("cpImage.bmp", cArray1)

【讨论】:

  • 这不适用于最新版本的 python。
猜你喜欢
  • 2020-02-21
  • 1970-01-01
  • 2015-04-22
  • 1970-01-01
  • 2012-06-21
  • 2020-03-14
  • 2020-12-24
  • 1970-01-01
  • 2016-09-06
相关资源
最近更新 更多