Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 优化性能,以最大像素值调整大小_Python_Image_Resize - Fatal编程技术网

Python 优化性能,以最大像素值调整大小

Python 优化性能,以最大像素值调整大小,python,image,resize,Python,Image,Resize,我想通过只保留每个像素集的最大像素值来减小图像的大小。我用python实现了这一点: def pixel_max_resize(img, h, w): imr = np.zeros((h,w), dtype=np.uint8) r = int(h/w) for j in range(0,w): imr[:,j] = np.amax(img[:,j*r:j*r+r], axis = 1) return imr 此功能比相同大小

我想通过只保留每个像素集的最大像素值来减小图像的大小。我用python实现了这一点:

def pixel_max_resize(img, h, w):    
    imr = np.zeros((h,w),  dtype=np.uint8)
    r = int(h/w)    
    for j in range(0,w):
         imr[:,j] = np.amax(img[:,j*r:j*r+r], axis = 1) 
    return imr

此功能比相同大小的cv2.resize慢得多(5-10倍)。有人知道如何优化此函数的速度吗?是否有一个列表理解公式可以加快这个过程?

我不能100%确定您想要实现什么,因为如果目标高度不等于源高度,您的代码就会抛出错误。无论如何,这里有一个函数,它根据每个子采样区域的最大值调整图像的大小。它大约比你的代码快3-5倍

def pixel_max_resize(img, h, w):
    source_h, source_w = img.shape
    return img.reshape(h,source_h // h,-1,source_w // w).swapaxes(1,2).reshape(h,w,-1).max(axis=2)
(注意:源宽度和高度必须分别为目标宽度和高度的整数倍)

说明:

源2d图像被分割成3d阵列,使得第一和第二轴具有目标宽度和高度的大小,并且第三轴包含针对一个目标像素进行二次采样的所有像素的值<此轴上的code>max()返回每个子样本的最大值