沿y = x有效地翻转/转置图像

epi*_*iac 3 python opencv numpy image-processing

我想沿y = x轴翻转图像.

原版的

翻转

我已经使这个功能做我想要的但我想知道是否有更优化的方法来做到这一点.在处理大图像时,我所做的功能有点慢

def flipImage(img):
    # Get image dimensions
    h, w = img.shape[:2]
    # Create a image
    imgYX = np.zeros((w, h, 3), np.uint8)
    for y in range(w):
        for x in range(h):
            imgYX[y,x,:]=img[x,y,:] #Flip pixels along y=x
    return imgYX
Run Code Online (Sandbox Code Playgroud)

Div*_*kar 5

只需swap the first two axes对应高度和宽度 -

img.swapaxes(0,1) # or np.swapaxes(img,0,1)
Run Code Online (Sandbox Code Playgroud)

我们也可以用transpose它来置换轴-

img.transpose(1,0,2) # or np.transpose(img,(1,0,2))
Run Code Online (Sandbox Code Playgroud)

我们也可以roll axes达到同样的效果 -

np.rollaxis(img,0,-1)
Run Code Online (Sandbox Code Playgroud)

We use the same trick when working with images in MATLAB.