user961627
user961627

Reputation: 12747

scikit-image transform ValueError: Buffer not C contiguous

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?

Upvotes: 0

Views: 2777

Answers (2)

Tengerye
Tengerye

Reputation: 1964

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.

Upvotes: 0

Alex Riley
Alex Riley

Reputation: 176978

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.

Upvotes: 2

Related Questions