Home > database >  Can I uncrop a numpy array/OpenCV image?
Can I uncrop a numpy array/OpenCV image?

Time:08-07

In C , when I cropped an image, OpenCV simply kept a reference to the original image and added some info about the crop. (If I remember correctly.) So, there one could "uncrop" the crop and get back the original image.

Is it possible using the Python cv2 interface to "uncrop" the image?

In terms of code, I would like to achieve the following:

   cropped = image[y: y h, x: x w]
   uncropped = mat_uncrop(cropped)

where uncropped is to be equal to image, pixel-by-pixel.

CodePudding user response:

I believe you can do that by accessing the .base attribute of a slice.

A = np.eye(10)

B = A[2:8, 2:8]
B[2,2] = 999

assert B.base is A

print(B.flags)

print(B.base)
  C_CONTIGUOUS : False
  F_CONTIGUOUS : False
  OWNDATA : False
  WRITEABLE : True
  ALIGNED : True
  WRITEBACKIFCOPY : False
  UPDATEIFCOPY : False

[[  1.   0.   0.   0.   0.   0.   0.   0.   0.   0.]
 [  0.   1.   0.   0.   0.   0.   0.   0.   0.   0.]
 [  0.   0.   1.   0.   0.   0.   0.   0.   0.   0.]
 [  0.   0.   0.   1.   0.   0.   0.   0.   0.   0.]
 [  0.   0.   0.   0. 999.   0.   0.   0.   0.   0.]
 [  0.   0.   0.   0.   0.   1.   0.   0.   0.   0.]
 [  0.   0.   0.   0.   0.   0.   1.   0.   0.   0.]
 [  0.   0.   0.   0.   0.   0.   0.   1.   0.   0.]
 [  0.   0.   0.   0.   0.   0.   0.   0.   1.   0.]
 [  0.   0.   0.   0.   0.   0.   0.   0.   0.   1.]]
  • Related