How to speed up apply function to each of array items

Viewed 41

I need to crop image based on the given coordinate of target(x,y). The coordinate of target(0,0) equals to the center of the images.
For example:
if target(-x,0) means it should move to left of the x-axis.
if target(0,y) means it should move down of the y-axis.
x,y are any integer.

I want to speed up the progress to apply each of array items.

This is my function below:

def crop_img(path, target_x, target_y):
    img = cv2.imread(path)
    height, width, channels = img.shape
    target_x_center, target_y_center = width/2+target_x, height/2+target_y

    xmax = target_x_center+1000
    xmin = target_x_center-1000
    ymax = target_y_center+1000
    ymin = target_y_center-1000

    if xmin <= 0: xmin =0
    if ymin <= 0: ymin =0
    if xmax >= width: xmax = width
    if ymax >= height: ymax = height

    img_cropped = img[int(ymin):int(ymax), int(xmin):int(xmax)]

    return img_cropped

My input is an array like this (the length of the array is about 80000) :

array([['path1', target_x1, target_y1], ['path2', target_x2, target_y2], ...]])

I tried this but it has error.

vfunc = np.vectorize(crop_img2)
vfunc(myArray)


'int' object is not subscriptable

How can I do?

0 Answers
Related