Opencv-图像翻转

知识点

图像翻转(Image Flip)
图像翻转的本质像素映射,OpenCV支持三种图像翻转方式

  • X轴翻转,flipcode = 0
  • Y轴翻转, flipcode = 1
  • XY轴翻转, flipcode = -1

相关的API:flip

  • src输入参数
  • dst 翻转后图像
  • flipcode

python代码

import cv2 as cv
import numpy as np

src = cv.imread("C:/Users/qqxd/Desktop/opencvcode/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)


#custom x-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[h-row-1, col] = [b, g, r]
cv.imshow("custom-x-flip", dst)

#custom xy-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[h-row-1, w - col - 1] = [b, g, r]
cv.imshow("custom-xy-flip", dst)
cv.waitKey(0)
cv.destroyAllWindows()

c++代码

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

using namespace cv;
using namespace std;

int main(int argc, char** argv) {
	Mat src = imread("C:/Users/qqxd/Desktop/opencvcode/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;
}

运行效果如下:
在这里插入图片描述
在这里插入图片描述
无论直接调用Opencv api,还是自己对像素直接操作,得到的结果是一样的。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值