0

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?

2
  • Maybe duplicate of stackoverflow.com/questions/21811633/… Commented Nov 5, 2014 at 12:09
  • Could you please provide a self-contained snippet that shows this behavior? That should not happen, and if it does it is a bug that I'd like to fix. Resizing non-contiguous arrays works on my system. Commented Nov 5, 2014 at 14:08

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Where do you suggest I add the ascontiguousarray part? I'm trying it like this: candidate = resize(np.ascontiguousarray(img[candidate_box[0]:candidate_box[2],candidate_box[1]:candidate_box[3]]), (50,100))
@user961627 Yes, if resize requires the array to be C contiguous, that seems like a good place to use it.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.