简体   繁体   English

将rasbperry pi的imageproc模块图像转换为(numpy array或cvmat)

[英]convert from imageproc module image of rasbperry pi into ( numpy array or cvmat )

imageproc module in rasbperry pi is very efficient but i couldn't use opencv with it due to the different type of image related to imageproc module. rasbperry pi中的imageproc模块非常有效,但由于与imageproc模块相关的图像类型不同,我无法将opencv与它一起使用。

So , i seek for a method to enable using opencv of image type( cvmat for cv , numpyarray for cv2 , .. ) with the rasbperry pi imageproc module Thanks in advance 因此,我寻求一种方法,可以使用rasbperry pi imageproc模块使用图像类型的opencv(cvmat为cv,numpyarray为cv2,..)

You will first have to convert the imgproc output to a numpy.array . 首先,您必须将imgproc输出转换为numpy.array After that you can use the array with cv2 almost directly. 之后,您几乎可以将阵列与cv2一起使用。

If we assume you have your imgproc image in img , then: 如果我们假设您在imgimgproc图像,则:

import numpy

img_array = numpy.array([ [ img[x,y] for x in range(img.width) ] for y in range(img.height) ] , dtype='uint8')

Unfortunately, there are no faster data access methods in imgproc , you will have to do it one-by-one. 不幸的是, imgproc中没有更快的数据访问方法,您将必须一对一地进行操作。 But after this slowish looping you can use img_array directly with cv2 . 但是在这种缓慢的循环之后,您可以将img_array直接与cv2一起cv2


If this crashes (it shouldn't) with a segfault (it really shouldn't) something goes badly wrong probably in imgproc . 如果此操作因segfault而崩溃(实际上不应该),则可能在imgproc出现严重错误。 The best guess is that img[x,y] points to outside of the image created by imgproc . 最好的猜测是img[x,y]指向imgproc创建的图像imgproc To debug this, let's split the command into more palatable parts. 为了调试它,让我们将命令分成更多可口的部分。

# dimensions ok?
w = img.width
h = img.height
print w,h

# can we fetch pixels at the extremes?
upperleft = img[0,0]
lowerright = img[w-1, h-1]
print upperleft, lowerright

# the loop without conversion to array
img_l = [ [ img[x,y] for x in range(w) ] for y in range(h) ]
print len(img_l)

# convert the image into an array
img_array = numpy.array(img_l, dtype='uint8')
print img_array.shape

Where does this crash? 崩溃在哪里? Note that the print statement is not entirely reliable with segfaults on all platforms, but it indicates that the code has run at least that far. 请注意, print语句对于所有平台上的段错误都不是完全可靠的,但是它表明代码至少已经运行了那么远。

If the code crashes when accessing img dimensions or pixels, the problem may be in the image capture code. 如果在访问img尺寸或像素时代码崩溃,则问题可能出在图像捕获代码中。 If you show a minimal example in the question, the problem is easier to debug. 如果您在问题中显示一个最小的示例,则该问题更容易调试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM