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

OpenCV代码提取:flip函数的实现的更多相关文章

  1. OpenCV代码提取:transpose函数的实现

    OpenCV中的transpose函数实现图像转置,公式为: 目前fbc_cv库中也实现了transpose函数,支持多通道,uchar和float两种数据类型,经测试,与OpenCV3.1结果完全一 ...

  2. OpenCV代码提取:dft函数的实现

    The Fourier Transform will decompose an image into its sinus and cosines components. In other words, ...

  3. OpenCV代码提取: threshold函数的实现

    threshold algorithm: The simplest image segmentation method. All thresholding algorithms take a sour ...

  4. OpenCV代码提取:遍历指定目录下指定文件的实现

    前言 OpenCV 3.1之前的版本,在contrib目录下有提供遍历文件的函数,用起来比较方便.但是在最新的OpenCV 3.1版本给去除掉了.为了以后使用方便,这里将OpenCV 2.4.9中相关 ...

  5. opencv: flip函数的使用;

    flip函数用于图像翻转,比较方便.在opencv中有几种形式: C++: void flip(InputArray src, OutputArray dst, int flipCode) Pytho ...

  6. OpenCV中的绘图函数-OpenCV步步精深

    OpenCV 中的绘图函数 画线 首先要为画的线创造出环境,就要生成一个空的黑底图像 img=np.zeros((512,512,3), np.uint8) 这是黑色的底,我们的画布,我把窗口名叫做i ...

  7. 基础学习笔记之opencv(24):imwrite函数的使用

    http://www.cnblogs.com/tornadomeet/archive/2012/12/26/2834336.html 前言 OpenCV中保存图片的函数在c++版本中变成了imwrit ...

  8. (转)Uri详解之——Uri结构与代码提取

    前言:依然没有前言…… 相关博客:1.<Uri详解之——Uri结构与代码提取>2.<Uri详解之二——通过自定义Uri外部启动APP与Notification启动> 上几篇给大 ...

  9. Uri详解之——Uri结构与代码提取

    目录(?)[+] 前言:依然没有前言…… 相关博客:1.<Uri详解之——Uri结构与代码提取>2.<Uri详解之二——通过自定义Uri外部启动APP与Notification启动& ...

随机推荐

  1. IOS NSLayoutConstraint 页面布局(通过代码添加约束)

    #import "ViewController.h" @interface ViewController () @property (nonatomic, strong) UIVi ...

  2. pthread 的几个结构体

    http://blog.csdn.net/yangzhongxuan/article/details/7397139 /* Copyright (C) 2002,2003,2004,2005,2006 ...

  3. 从零开始Vue项目实战(三)-项目结构

    目录结构 ├── README.md 项目介绍 ├── index.html 入口页面 ├── build 构建脚本目录 │ ├── build-server.js 运行本地构建服务器,可以访问构建后 ...

  4. Android学习笔记_JNI_c调用java代码

    1.编写native方法(java2c)和非native方法(c2java): package com.example.provider; public class CallbackJava { // ...

  5. HDU1069 Monkey and Banana

    HDU1069 Monkey and Banana 题目大意 给定 n 种盒子, 每种盒子无限多个, 需要叠起来, 在上面的盒子的长和宽必须严格小于下面盒子的长和宽, 求最高的高度. 思路 对于每个方 ...

  6. Python中使用list和tuple

    list: Python内置的一种数据类型是列表:list.list是一种有序的集合,可以随时添加和删除其中的元素. 比如,列出班里所有同学的名字,就可以用一个list表示: 变量classmates ...

  7. Python常用模块之time和datetime

    1.时间的格式化 结构化时间 ##把字符串时间转换成结构化时间 time.strptime("2017-06-21","%Y-%m-%d") ##把结构化时间转 ...

  8. 以ZMQ为基础的通信模型

    最近使用了一下ZMQ的java版本,先不评述其它,网上已经有很多内容了. 我通过ZMQ的模式,在MsgClient,MsgServer中封装了基础ZMQ的使用.以此扩展了使用模型: 主要是基于2类分布 ...

  9. HDU 6330--Visual Cube(构造,计算)

    Visual Cube 将这个立方体分块,分成上中下三个部分,利用长宽高计算行列,最后输出即可. 每个部分都分成奇偶行来输出,总共有\(2*(b+c)+1\)行,共\(2*(a+b)+1\)列.设当前 ...

  10. MySQL中Date,DateTime,TimeStamp和Time的比较

    名称 显示格式 显示范围 应用场景 后台取值 Date YYYY-MM-DD 1601-01-01 到 9999-01-01 当业务需求中只需要精确到天时, 可以用这个时间格式 @JSONField( ...