scikit-image transform ValueError: Buffer not C contiguous

前提是你 提交于 2019-12-13 04:44:01

问题


I'm using the skimage transform module's resize method.

Not always, but sometimes, I'm getting an error on this line:

candidate = resize(np.copy(img[candidate_box[0]:candidate_box[2],candidate_box[1]:candidate_box[3]]), (50,100))

It tells me:

ValueError: Buffer not C contiguous

How can I fix this?


回答1:


Reshaping (and other operations) will sometimes disrupt the contiguity of an array. You can check whether this has happened by looking at the flags:

>>> a = np.arange(10).reshape(5, 2).T
>>> a.flags
  C_CONTIGUOUS : False # reshaped array is no longer C contiguous
  F_CONTIGUOUS : True
  OWNDATA : False
  WRITEABLE : True
  ALIGNED : True
  UPDATEIFCOPY : False

Try making a C contiguous copy of the array with np.ascontiguousarray:

 >>> b = np.ascontiguousarray(a)
 >>> b.flags
  C_CONTIGUOUS : True # array b is a C contiguous copy of array a
  F_CONTIGUOUS : False
  OWNDATA : True
  WRITEABLE : True
  ALIGNED : True
  UPDATEIFCOPY : False

The function returns an array with the same shape and values as the target array, but the returned array is stored as a C contiguous array.




回答2:


I found a error may raise this exception. Make sure your region is within your image. For example, let's say your image is 300x200, and your region is [199:299, 100:199]. Note 299>200. If you perform resize(image[100:199, 199:299]), you will see this error.

Hope it could help you.



来源:https://stackoverflow.com/questions/26756772/scikit-image-transform-valueerror-buffer-not-c-contiguous

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!