Ask Your Question
1

"TypeError: expected a single-segment buffer object" when calling cv.SaveImage

asked 2012-12-11 05:17:56 -0600

nuriah gravatar image

Hi there,

I am trying to convert a numpy array into a cvmat and then save it. The following commandos executed in the interpreter produce an error:

In [21]: type(background)
Out[21]: numpy.ndarray

In [22]: background = cv.fromarray(background)

In [23]: type(background)
Out[23]: cv2.cv.cvmat

In [24]: cv.SaveImage(os.path.join(image_path,"background.tif"),background)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
TypeError: expected a single-segment buffer object

There is an image created even when the error is thrown but the image is warped. Also, this error doesn't occur if I call the script containing the commandos but the resulting image is warped nevertheless.

When I create a dummy array, the converting and saving process are working perfectly fine:

In [38]: a = reshape(np.array(xrange(16)),(4,4))

In [39]: type(a)
Out[39]: numpy.ndarray

In [40]: a = cv.fromarray(a)

In [41]: type(a)
Out[41]: cv2.cv.cvmat

In [42]: cv.SaveImage(os.path.join(image_path,"test.tif"),a)

Is there a difference between one numpy.ndarray and the other? Could the data type contained in the arrays cause the problem? I am really lost here and would greatly appreciate any help!

openCV version 2.4.3; Python 2.7; Windows 7 64 bit

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

answered 2018-08-29 04:10:48 -0600

Add this line,

background =  np.ascontiguousarray(background)

and it would work.

edit flag offensive delete link more

Question Tools

Stats

Asked: 2012-12-11 05:17:56 -0600

Seen: 1,453 times

Last updated: Dec 11 '12