013 图像翻转(Image Flip)

2019-08-15  本文已影响0人  几时见得清梦
  1. flip(src, dst, flipcode)
    src 输入参数
    dst 翻转后图像
    flipcode
  2. 图像翻转的本质像素映射,OpenCV支持三种图像翻转方式
    X轴翻转,flipcode = 0
    Y轴翻转, flipcode = 1
    XY轴翻转, flipcode = -1
三种图像翻转方式的结果

C++

#include<opencv2/opencv.hpp>
#include<iostream>

using namespace cv;
using namespace std;

int main(int argc, char** argv) {
    Mat src = imread("D:/vcprojects/images/test.png");
    if (src.empty()) {
        printf("could not load image...\n");
        return -1;
    }
    imshow("input", src);

    Mat dst;
    // X Flip 倒影
    flip(src, dst, 0);
    imshow("x-flip", dst);

    // Y Flip 镜像
    flip(src, dst, 1);
    imshow("y-flip", dst);

    // XY Flip 对角
    flip(src, dst, -1);
    imshow("xy-flip", dst);

    waitKey(0);
    return 0;
}

Python

import cv2 as cv
import numpy as np

src = cv.imread("D:/vcprojects/images/test.png")
cv.namedWindow("input", cv.WINDOW_AUTOSIZE)
cv.imshow("input", src)

# X Flip 倒影
dst1 = cv.flip(src, 0);
cv.imshow("x-flip", dst1);

# Y Flip 镜像
dst2 = cv.flip(src, 1);
cv.imshow("y-flip", dst2);

# XY Flip 对角
dst3 = cv.flip(src, -1);
cv.imshow("xy-flip", dst3);

# custom y-flip
h, w, ch = src.shape
dst = np.zeros(src.shape, src.dtype)
for row in range(h):
    for col in range(w):
        b, g, r = src[row, col]
        dst[row, w - col - 1] = [b, g, r]
cv.imshow("custom-y-flip", dst)

cv.waitKey(0)
cv.destroyAllWindows()
上一篇下一篇

猜你喜欢

热点阅读