OpenCV中实现图像翻转的函数flip,公式为:

目前fbc_cv库中也实现了flip函数,支持多通道,uchar和float两种数据类型,经测试,与OpenCV3.1结果完全一致。

实现代码flip.hpp:

// fbc_cv is free software and uses the same licence as OpenCV
// Email: fengbingchun@163.com

#ifndef FBC_CV_FLIP_HPP_
#define FBC_CV_FLIP_HPP_

/* reference: include/opencv2/core.hpp
              modules/core/src/copy.cpp
*/

#include <typeinfo>
#include "core/mat.hpp"

namespace fbc {

template<typename _Tp, int chs> static int flipHoriz(const Mat_<_Tp, chs>& src, Mat_<_Tp, chs>& dst);
template<typename _Tp, int chs> static int flipVert(const Mat_<_Tp, chs>& src, Mat_<_Tp, chs>& dst);

// Flips a 2D array around vertical, horizontal, or both axes
// flipCode: 0 means flipping around the x - axis and positive value means flipping around y - axis.
//	     Negative value means flipping around both axes
// support type: uchar/float, multi-channels
template <typename _Tp, int chs>
int flip(const Mat_<_Tp, chs>& src, Mat_<_Tp, chs>& dst, int flipCode)
{
	FBC_Assert(typeid(uchar).name() == typeid(_Tp).name() || typeid(float).name() == typeid(_Tp).name()); // uchar || float
	if (dst.empty()) {
		dst = Mat_<_Tp, chs>(src.rows, src.cols);
	} else {
		FBC_Assert(src.rows == dst.rows && src.cols == dst.cols);
	}

	Size size = src.size();

	if (flipCode < 0) {
		if (size.width == 1)
			flipCode = 0;
		if (size.height == 1)
			flipCode = 1;
	}

	if ((size.width == 1 && flipCode > 0) ||
		(size.height == 1 && flipCode == 0) ||
		(size.height == 1 && size.width == 1 && flipCode < 0)) {
		src.copyTo(dst);
		return 0;
	}

	if (flipCode <= 0)
		flipVert(src, dst);
	else
		flipHoriz(src, dst);

	if (flipCode < 0)
		flipHoriz(dst, dst);

	return 0;
}

template<typename _Tp, int chs>
static int flipHoriz(const Mat_<_Tp, chs>& src, Mat_<_Tp, chs>& dst)
{
	Size size = src.size();
	size_t esz = sizeof(_Tp) * chs;
	int i, j, limit = (int)(((size.width + 1) / 2) *esz );
	AutoBuffer<int> _tab(size.width * esz);
	int* tab = _tab;
	size_t sstep = src.step;
	size_t dstep = dst.step;
	const uchar* src_ = src.ptr();
	uchar* dst_ = dst.ptr();

	for (i = 0; i < size.width; i++) {
		for (size_t k = 0; k < esz; k++) {
			tab[i*esz + k] = (int)((size.width - i - 1) * esz + k);
		}
	}

	for (; size.height--; src_ += sstep, dst_ += dstep) {
		for (i = 0; i < limit; i++) {
			j = tab[i];
			uchar t0 = src_[i], t1 = src_[j];
			dst_[i] = t1; dst_[j] = t0;
		}
	}

	return 0;
}

template<typename _Tp, int chs>
static int flipVert(const Mat_<_Tp, chs>& src, Mat_<_Tp, chs>& dst)
{
	const uchar* src0 = src.ptr();
	uchar* dst0 = dst.ptr();
	Size size = src.size();
	size_t sstep = src.step;
	size_t dstep = dst.step;
	size_t esz = sizeof(_Tp) * chs;
	const uchar* src1 = src0 + (size.height - 1)*sstep;
	uchar* dst1 = dst0 + (size.height - 1)*dstep;
	size.width *= (int)esz;

	for (int y = 0; y < (size.height + 1) / 2; y++, src0 += sstep, src1 -= sstep, dst0 += dstep, dst1 -= dstep) {
		int i = 0;
		if (((size_t)src0 | (size_t)dst0 | (size_t)src1 | (size_t)dst1) % sizeof(int) == 0) {
			for (; i <= size.width - 16; i += 16) {
				int t0 = ((int*)(src0 + i))[0];
				int t1 = ((int*)(src1 + i))[0];

				((int*)(dst0 + i))[0] = t1;
				((int*)(dst1 + i))[0] = t0;

				t0 = ((int*)(src0 + i))[1];
				t1 = ((int*)(src1 + i))[1];

				((int*)(dst0 + i))[1] = t1;
				((int*)(dst1 + i))[1] = t0;

				t0 = ((int*)(src0 + i))[2];
				t1 = ((int*)(src1 + i))[2];

				((int*)(dst0 + i))[2] = t1;
				((int*)(dst1 + i))[2] = t0;

				t0 = ((int*)(src0 + i))[3];
				t1 = ((int*)(src1 + i))[3];

				((int*)(dst0 + i))[3] = t1;
				((int*)(dst1 + i))[3] = t0;
			}

			for (; i <= size.width - 4; i += 4) {
				int t0 = ((int*)(src0 + i))[0];
				int t1 = ((int*)(src1 + i))[0];

				((int*)(dst0 + i))[0] = t1;
				((int*)(dst1 + i))[0] = t0;
			}
		}

		for (; i < size.width; i++) {
			uchar t0 = src0[i];
			uchar t1 = src1[i];

			dst0[i] = t1;
			dst1[i] = t0;
		}
	}

	return 0;
}

} // namespace fbc

#endif // FBC_CV_FLIP_HPP_

测试代码test_flip.cpp:

#include "test_flip.hpp"
#include <assert.h>
#include <iostream>
#include <string>

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

int test_flip_uchar()
{
	cv::Mat matSrc = cv::imread("E:/GitCode/OpenCV_Test/test_images/1.jpg", 1);
	if (!matSrc.data) {
		std::cout << "read image fail" << std::endl;
		return -1;
	}

	int width = matSrc.cols;
	int height = matSrc.rows;

	int flipCode[3] {-1, 0, 1}; // both axes, x axis, y axis

	for (int i = 0; i < 3; i++) {
		fbc::Mat_<uchar, 3> mat1(height, width, matSrc.data);
		fbc::Mat_<uchar, 3> mat2(height, width);
		fbc::flip(mat1, mat2, flipCode[i]);

		cv::Mat mat1_(height, width, CV_8UC3, matSrc.data);
		cv::Mat mat2_;
		cv::flip(mat1_, mat2_, flipCode[i]);

		assert(mat2.rows == mat2_.rows && mat2.cols == mat2_.cols && mat2.step == mat2_.step);
		for (int y = 0; y < mat2.rows; y++) {
			const fbc::uchar* p1 = mat2.ptr(y);
			const uchar* p2 = mat2_.ptr(y);

			for (int x = 0; x < mat2.step; x++) {
				assert(p1[x] == p2[x]);
			}
		}

		std::string name = std::to_string(i);
		std::string file_path = "E:/GitCode/OpenCV_Test/test_images/";
		std::string name_fbc = file_path + "flip_fbc_" + name + ".jpg";
		std::string name_cv = file_path + "flip_cv_" + name + ".jpg";
		cv::Mat matSave(height, width, CV_8UC3, mat2.data);
		cv::imwrite(name_fbc, matSave);
		cv::imwrite(name_cv, mat2_);
	}

	return 0;
}

int test_flip_float()
{
	cv::Mat matSrc = cv::imread("E:/GitCode/OpenCV_Test/test_images/1.jpg", 1);
	if (!matSrc.data) {
		std::cout << "read image fail" << std::endl;
		return -1;
	}
	cv::cvtColor(matSrc, matSrc, CV_BGR2GRAY);
	matSrc.convertTo(matSrc, CV_32FC1);

	int width = matSrc.cols;
	int height = matSrc.rows;

	int flipCode[3] {-1, 0, 1}; // both axes, x axis, y axis

	for (int i = 0; i < 3; i++) {
		fbc::Mat_<float, 1> mat1(height, width, matSrc.data);
		fbc::Mat_<float, 1> mat2(height, width);
		fbc::flip(mat1, mat2, flipCode[i]);

		cv::Mat mat1_(height, width, CV_32FC1, matSrc.data);
		cv::Mat mat2_;
		cv::flip(mat1_, mat2_, flipCode[i]);

		assert(mat2.rows == mat2_.rows && mat2.cols == mat2_.cols && mat2.step == mat2_.step);
		for (int y = 0; y < mat2.rows; y++) {
			const fbc::uchar* p1 = mat2.ptr(y);
			const uchar* p2 = mat2_.ptr(y);

			for (int x = 0; x < mat2.step; x++) {
				assert(p1[x] == p2[x]);
			}
		}
	}

	return 0;
}

GitHubhttps://github.com/fengbingchun/OpenCV_Test

最新文章

  1. Android Toast cancel和show 不踩中不会知道的坑
  2. Atititcmd cli环境变量的调用设置与使用
  3. 【BZOJ-2436】嘉年华 DP + 优化
  4. git使用中遇到的常见问题
  5. DMG提取安装文件
  6. phpcms源码解析(2)
  7. gnu make
  8. ASP.NET Core Razor 视图组件
  9. [TJOI2017]可乐
  10. ~/Library/MobileDevice/Provisioning Profiles
  11. OpenStack搭建Q版在控制节点上的环境准备(step2)
  12. linux驱动 之 module_init解析 (上)【转】
  13. mysql学习笔记四 —— AB复制
  14. MyBatis入门程序(1)
  15. js filter关键字
  16. 为什么mysqlbinlog --database选项不起作用
  17. vue-cli 3.0 开启 Gzip 方法
  18. Linux下mysql的远程连接(转)
  19. Textrank权值提取文本标签提取:
  20. 爬虫浅谈一:一个简单c#爬虫程序

热门文章

  1. 笔记本win8系统共享wifi上网方法
  2. 关于bootstrap-table服务端分页问题
  3. Android(java)学习笔记35:如何改变Spinner系统自带的字体和颜色
  4. nbu集群Alwayson相关问题
  5. Yii2 指定log存放的文件
  6. 【luogu T34117 打油门】 题解
  7. 中小学信息学奥林匹克竞赛-理论知识考点--IP地址
  8. 在文件中的AngularJS模块
  9. 1486: [HNOI2009]最小圈
  10. 洛谷P2439 [SDOI2005]阶梯教室设备利用(带权区间覆盖)