python openCV 实现镜像翻转flip()

版权声明:码字不易,且看且珍惜!交流:Wechat: suihailiang0816 QQ: 931762054 转载并修改内容请与我联系 https://blog.csdn.net/weixin_41010198/article/details/89373328

python openCV 实现镜像翻转flip()

文章目录:

一、基本认知

图像的几何变换是指在不改变图像像素值的前提下对图像像素进行空间几何变换。
常见的几何变换有:
镜像,平移,旋转,缩放,仿射等变换。

二、代码实现

翻转实现的两种方式:

  • 1、遍历像素值
  • 2、用opencv的内置函数cv2.flip()函数
import numpy as np
import cv2 as cv
from matplotlib import pyplot as plt
import sys
import copy

#python 图像翻转,自定义翻转

img = cv.imread('ABC.png')

if img.all()==None:
    print('No Such image!')
    sys.exit(0)
size = img.shape
iCopy = copy.deepcopy(img)
iCopy1 = copy.deepcopy(img)
iCopy2 = copy.deepcopy(img)
h = size[0]
w = size[1]

for i in range(h):
    for j in range(w):
        iCopy[i,w-1-j] = img[i,j]#水平镜像
        iCopy1[h-1-i,j] = img[i,j]#垂直镜像
        iCopy2[h-1-i,w-1-j] = img[i,j]#对角镜像
plt.subplot(221),plt.imshow(img)
plt.title('Original Image'), plt.xticks([]), plt.yticks([])
plt.subplot(222),plt.imshow(iCopy)
plt.title('Remap shuiping Image'), plt.xticks([]), plt.yticks([])
plt.subplot(223),plt.imshow(iCopy1)
plt.title('Remap chuizhi Image'), plt.xticks([]), plt.yticks([])
plt.subplot(224),plt.imshow(iCopy2)
plt.title('Remap duijiao Image'), plt.xticks([]), plt.yticks([])

plt.show()
#python 图像翻转,使用openCV flip()方法翻转
xImg = cv.flip(img,1,dst=None) #水平镜像
xImg1 = cv.flip(img,0,dst=None) #垂直镜像
xImg2 = cv.flip(img,-1,dst=None) #对角镜像
plt.subplot(221),plt.imshow(img)
plt.title('Original Image'), plt.xticks([]), plt.yticks([])
plt.subplot(222),plt.imshow(xImg)
plt.title('Remap shuiping Image'), plt.xticks([]), plt.yticks([])
plt.subplot(223),plt.imshow(xImg1)
plt.title('Remap chuizhi Image'), plt.xticks([]), plt.yticks([])
plt.subplot(224),plt.imshow(xImg2)
plt.title('Remap duijiao Image'), plt.xticks([]), plt.yticks([])

plt.show()

xImg = cv.flip(img,1,dst=None) #水平镜像一般比较常用

cv2.flip()函数的定义:


def flip(src, flipCode, dst=None): # real signature unknown; restored from __doc__
    """
    flip(src, flipCode[, dst]) -> dst
    .   @brief Flips a 2D array around vertical, horizontal, or both axes.
    .   
    .   The function cv::flip flips the array in one of three different ways (row
    .   and column indices are 0-based):
    .   \f[\texttt{dst} _{ij} =
    .   \left\{
    .   \begin{array}{l l}
    .   \texttt{src} _{\texttt{src.rows}-i-1,j} & if\;  \texttt{flipCode} = 0 \\
    .   \texttt{src} _{i, \texttt{src.cols} -j-1} & if\;  \texttt{flipCode} > 0 \\
    .   \texttt{src} _{ \texttt{src.rows} -i-1, \texttt{src.cols} -j-1} & if\; \texttt{flipCode} < 0 \\
    .   \end{array}
    .   \right.\f]
    .   The example scenarios of using the function are the following:
    .   *   Vertical flipping of the image (flipCode == 0) to switch between
    .   top-left and bottom-left image origin. This is a typical operation
    .   in video processing on Microsoft Windows\* OS.
    .   *   Horizontal flipping of the image with the subsequent horizontal
    .   shift and absolute difference calculation to check for a
    .   vertical-axis symmetry (flipCode \> 0).
    .   *   Simultaneous horizontal and vertical flipping of the image with
    .   the subsequent shift and absolute difference calculation to check
    .   for a central symmetry (flipCode \< 0).
    .   *   Reversing the order of point arrays (flipCode \> 0 or
    .   flipCode == 0).
    .   @param src input array.
    .   @param dst output array of the same size and type as src.
    .   @param flipCode a flag to specify how to flip the array; 0 means
    .   flipping around the x-axis and positive value (for example, 1) means
    .   flipping around y-axis. Negative value (for example, -1) means flipping
    .   around both axes.
    .   @sa transpose , repeat , completeSymm
    """
    pass

猜你喜欢

转载自blog.csdn.net/weixin_41010198/article/details/89373328