0

I have an image defined as:

imagen = scipy.misc.imread("C:\\Users\\Reymi\\Downloads\\imagen.png")

It has a size of (1929,1280) I need to add 2 columns and rows with zeros to each side of the array so I can use a kernel without worrying about it going outside of the array.

How? Thank you

2 Answers 2

2

Use the pad function:

im_new = scipy.pad(array=imagen, pad_width=[2, 2], mode='constant', constant_values=0)

The above function makes the array bigger by 2 in all directions. If you wanted to replace the outer two pixels with 0's without making the picture larger I don't think there is a built-in function, but I would do something like:

pad = 2
im_new = scipy.zeros_like(imagen)
im_new[pad:-pad, pad:-pad] = imagen[pad:-pad, pad:-pad]

This creates an image of 0's, then fills in the middle with the middle part of imagen.

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

2 Comments

Or, if using numpy, np.pad(imagen, ((0, 0, (2, 2), 'constant')
it is asking for a "mode" argument. Also what is sp? scipy?
0

Option - 1 - Using OpenCV

imagenBorder = cv2.copyMakeBorder(imagen,2,2,2,2,cv2.BORDER_REFLECT)

Option - 2 - Manually padding to numpy array

import numpy as np    
borderWidth = 2
i = 0
while i < borderWidth:
    imagen = np.insert(imagen, 0, 0, axis=1)
    imagen = np.insert(imagen, imagen.shape[1], 0, axis=1)
    imagen = np.insert(imagen, 0, 0, axis=0)
    imagen = np.insert(imagen, imagen.shape[0], 0, axis=0)
    i = i+1

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.