jashliao 用 VC++ 實現 fanfuhan OpenCV 教學013 ~ opencv-013-圖片XY翻轉[flip()-(顛倒/轉向/旋轉)]
jashliao 用 VC++ 實現 fanfuhan OpenCV 教學013 ~ opencv-013-圖片XY翻轉[flip()-(顛倒/轉向/旋轉)]
資料來源: https://fanfuhan.github.io/
https://fanfuhan.github.io/2019/03/27/opencv-013/
GITHUB:https://github.com/jash-git/fanfuhan_ML_OpenCV
https://github.com/jash-git/jashliao-implements-FANFUHAN-OPENCV-with-VC
★前言:

★主題:
OPENCV提供一個直接對圖像X/Y軸進行翻轉的函數flip(),其翻轉參數條列如下:
X軸翻轉,flipcode = 0
Y軸翻轉, flipcode = 1
XY軸翻轉, flipcode = -1
★C++
// VC_FANFUHAN_OPENCV013.cpp : 定義主控台應用程式的進入點。
//
/*
// Debug | x32
通用屬性
| C/C++
| | 一般
| | 其他 Include 目錄 -> C:\opencv\build\include
|
| 連結器
| |一一般
| | 其他程式庫目錄 -> C:\opencv\build\x64\vc15\lib
|
| |一輸入
| | 其他相依性 -> opencv_world411d.lib;%(AdditionalDependencies)
// Releas | x64
組態屬性
| C/C++
| | 一般
| | 其他 Include 目錄 -> C:\opencv\build\include
|
| 連結器
| |一般
| | 其他程式庫目錄 -> C:\opencv\build\x64\vc15\lib
|
| |一輸入
| | 其他相依性 -> opencv_world411.lib;%(AdditionalDependencies)
*/
#include "stdafx.h"
#include <iostream>
#include <opencv2/opencv.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace std;
using namespace cv;
void pause()
{
printf("Press Enter key to continue...");
fgetc(stdin);
}
int main()
{
Mat src_bgr = imread("../../images/test.png");
if (src_bgr.empty())
{
cout << "could not load image.." << endl;
pause();
return -1;
}
else
{
imshow("input_bgr", src_bgr);
Mat dst;
/*
flipCode Anno
1 水平翻转
0 垂直翻转
-1 水平垂直翻转
*/
// X轴 倒影
flip(src_bgr, dst, 0);
imshow("x_flip", dst);
// Y轴 镜像
flip(src_bgr, dst, 1);
imshow("y_flip", dst);
// XY轴 对角
flip(src_bgr, 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()
★結果圖:

★延伸說明/重點回顧: