将Numpy数组转换为OpenCV数组。

时间:2023-01-06 01:41:30

I'm trying to convert a 2D Numpy array, representing a black-and-white image, into a 3-channel OpenCV array (i.e. an RGB image).

共识共识共识共识共识共识共识共识共识共识共识共识共识共识共识共识共识共识共识共识共识共识共识共识共识共识共识共识共识共识共识共识共识共识共识共识共识共识共识共识共识共识共识共识共识共识共识共识共识共识共识共识共识共识共识共识共识共识共识共识共识共识共识共识共识共识共识共识共识共识共识共识共识共识共识共识

Based on code samples and the docs I'm attempting to do this via Python like:

基于代码示例和文档,我试图通过Python来实现这一点:

import numpy as np, cv
vis = np.zeros((384, 836), np.uint32)
h,w = vis.shape
vis2 = cv.CreateMat(h, w, cv.CV_32FC3)
cv.CvtColor(vis, vis2, cv.CV_GRAY2BGR)

However, the call to CvtColor() is throwing the following cpp-level Exception:

但是,对CvtColor()的调用将抛出以下cpplevel异常:

OpenCV Error: Image step is wrong () in cvSetData, file /build/buildd/opencv-2.1.0/src/cxcore/cxarray.cpp, line 902
terminate called after throwing an instance of 'cv::Exception'
  what():  /build/buildd/opencv-2.1.0/src/cxcore/cxarray.cpp:902: error: (-13)  in function cvSetData

Aborted

What am I doing wrong?

我做错了什么?

2 个解决方案

#1


25  

Your code can be fixed as follows:

你的代码可以固定如下:

import numpy as np, cv
vis = np.zeros((384, 836), np.float32)
h,w = vis.shape
vis2 = cv.CreateMat(h, w, cv.CV_32FC3)
vis0 = cv.fromarray(vis)
cv.CvtColor(vis0, vis2, cv.CV_GRAY2BGR)

Short explanation:

简短说明:

  1. np.uint32 data type is not supported by OpenCV (it supports uint8, int8, uint16, int16, int32, float32, float64)
  2. np。uint32数据类型不支持OpenCV(它支持uint8、int8、uint16、int16、int32、float32、float64)
  3. cv.CvtColor can't handle numpy arrays so both arguments has to be converted to OpenCV type. cv.fromarray do this conversion.
  4. 简历。CvtColor不能处理numpy数组,因此两个参数都必须转换为OpenCV类型。cv.fromarray做这个转换。
  5. Both arguments of cv.CvtColor must have the same depth. So I've changed source type to 32bit float to match the ddestination.
  6. 双方的观点都有简历。CvtColor必须具有相同的深度。因此,我将源类型改为32位浮动以匹配ddestination。

Also I recommend you use newer version of OpenCV python API because it uses numpy arrays as primary data type:

另外,我建议您使用最新版本的OpenCV python API,因为它使用numpy数组作为主要数据类型:

import numpy as np, cv2
vis = np.zeros((384, 836), np.float32)
vis2 = cv2.cvtColor(vis, cv2.COLOR_GRAY2BGR)

#2


2  

This is what worked for me...

这就是我的工作…

import cv2
import numpy as np

#Created an image (really an ndarray) with three channels 
new_image = np.ndarray((3, num_rows, num_cols), dtype=int)

#Did manipulations for my project where my array values went way over 255
#Eventually returned numbers to between 0 and 255

#Converted the datatype to np.uint8
new_image = new_image.astype(np.uint8)

#Separated the channels in my new image
new_image_red, new_image_green, new_image_blue = new_image

#Stacked the channels
new_rgb = np.dstack([new_image_red, new_image_green, new_image_blue])

#Displayed the image
cv2.imshow("WindowNameHere", new_rgbrgb)
cv2.waitKey(0)

#1


25  

Your code can be fixed as follows:

你的代码可以固定如下:

import numpy as np, cv
vis = np.zeros((384, 836), np.float32)
h,w = vis.shape
vis2 = cv.CreateMat(h, w, cv.CV_32FC3)
vis0 = cv.fromarray(vis)
cv.CvtColor(vis0, vis2, cv.CV_GRAY2BGR)

Short explanation:

简短说明:

  1. np.uint32 data type is not supported by OpenCV (it supports uint8, int8, uint16, int16, int32, float32, float64)
  2. np。uint32数据类型不支持OpenCV(它支持uint8、int8、uint16、int16、int32、float32、float64)
  3. cv.CvtColor can't handle numpy arrays so both arguments has to be converted to OpenCV type. cv.fromarray do this conversion.
  4. 简历。CvtColor不能处理numpy数组,因此两个参数都必须转换为OpenCV类型。cv.fromarray做这个转换。
  5. Both arguments of cv.CvtColor must have the same depth. So I've changed source type to 32bit float to match the ddestination.
  6. 双方的观点都有简历。CvtColor必须具有相同的深度。因此,我将源类型改为32位浮动以匹配ddestination。

Also I recommend you use newer version of OpenCV python API because it uses numpy arrays as primary data type:

另外,我建议您使用最新版本的OpenCV python API,因为它使用numpy数组作为主要数据类型:

import numpy as np, cv2
vis = np.zeros((384, 836), np.float32)
vis2 = cv2.cvtColor(vis, cv2.COLOR_GRAY2BGR)

#2


2  

This is what worked for me...

这就是我的工作…

import cv2
import numpy as np

#Created an image (really an ndarray) with three channels 
new_image = np.ndarray((3, num_rows, num_cols), dtype=int)

#Did manipulations for my project where my array values went way over 255
#Eventually returned numbers to between 0 and 255

#Converted the datatype to np.uint8
new_image = new_image.astype(np.uint8)

#Separated the channels in my new image
new_image_red, new_image_green, new_image_blue = new_image

#Stacked the channels
new_rgb = np.dstack([new_image_red, new_image_green, new_image_blue])

#Displayed the image
cv2.imshow("WindowNameHere", new_rgbrgb)
cv2.waitKey(0)