OpenCV中的transpose函数实现图像转置,公式为:

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

实现代码transpose.hpp:

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

#ifndef FBC_CV_TRANSPOSE_HPP_
#define FBC_CV_TRANSPOSE_HPP_

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

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

namespace fbc {

// transposes the matrix
// \f[\texttt{dst} (i,j) =  \texttt{src} (j,i)\f]
// support type: uchar/float, multi-channels
template <typename _Tp, int chs>
int transpose(const Mat_<_Tp, chs>& src, Mat_<_Tp, chs>& dst)
{
	FBC_Assert(typeid(uchar).name() == typeid(_Tp).name() || typeid(float).name() == typeid(_Tp).name()); // uchar || float
	if (dst.empty()) {
		dst = Mat_<_Tp, chs>(src.cols, src.rows);
	} else {
		FBC_Assert(src.rows == dst.cols && src.cols == dst.rows);
	}

	if (src.empty()) {
		dst.release();
		return 0;
	}

	// handle the case of single-column/single-row matrices, stored in STL vectors.
	if (src.rows != dst.cols || src.cols != dst.rows) {
		FBC_Assert(src.size() == dst.size() && (src.cols == 1 || src.rows == 1));
		src.copyTo(dst);
		return 0;
	}

	if (dst.data == src.data) {
		FBC_Assert(dst.cols == dst.rows);
		int n = dst.rows;
		int  step = dst.step;
		uchar* data = dst.ptr();

		for (int i = 0; i < n; i++) {
			_Tp* row = (_Tp*)(data + step*i);
			int i_ = i * chs;

			for (int j = i + 1; j < n; j++) {
				_Tp* data1 = (_Tp*)(data + step * j);
				int j_ = j * chs;

				for (int ch = 0; ch < chs; ch++) {
					std::swap(row[j_ + ch], data1[i_ + ch]);
				}
			}
		}
	} else {
		const uchar* src_ = src.ptr();
		size_t sstep = src.step;
		uchar* dst_ = dst.ptr();
		size_t dstep = dst.step;
		int m = src.cols, n = src.rows;

		for (int i = 0; i < n; i++) {
			const _Tp* s = (const _Tp*)(src_ + sstep*i);
			int i_ = i * chs;

			for (int j = 0; j < m; j++) {
				_Tp* d = (_Tp*)(dst_ + dstep*j);
				int j_ = j * chs;

				for (int ch = 0; ch < chs; ch++) {
					d[i_ + ch] = s[j_ + ch];
				}
			}
		}
	}

	return 0;
}

} // namespace fbc

#endif // FBC_CV_TRANSPOSE_HPP_

测试代码test_transpose.cpp:

#include "test_transpose.hpp"
#include <assert.h>
#include <iostream>
#include <string>
#include <opencv2/opencv.hpp>
#include <transpose.hpp>

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

	int width = matSrc.cols;
	int height = matSrc.rows;
	cv::Mat matSrc_;
	cv::resize(matSrc, matSrc_, cv::Size(width, width));

	fbc::Mat_<uchar, 3> mat1(width, width);
	memcpy(mat1.data, matSrc_.data, width * width * 3);
	fbc::transpose(mat1, mat1);

	cv::Mat mat1_(width, width, CV_8UC3);
	memcpy(mat1_.data, matSrc_.data, width * width * 3);
	cv::transpose(mat1_, mat1_);

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

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

	cv::Mat matSave(width, width, CV_8UC3, mat1.data);
	cv::imwrite("E:/GitCode/OpenCV_Test/test_images/transpose_fbc.jpg", matSave);
	cv::imwrite("E:/GitCode/OpenCV_Test/test_images/transpose_cv.jpg", mat1_);

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

	width = matSrc1.cols;
	height = matSrc1.rows;

	fbc::Mat_<uchar, 3> mat2(height, width, matSrc1.data);
	fbc::Mat_<uchar, 3> mat3(width, height);
	fbc::transpose(mat2, mat3);

	cv::Mat mat2_(height, width, CV_8UC3, matSrc1.data);
	cv::Mat mat3_;
	cv::transpose(mat2_, mat3_);

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

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

	cv::Mat matSave1(width, height, CV_8UC3, mat3.data);
	cv::imwrite("E:/GitCode/OpenCV_Test/test_images/transpose1_fbc.jpg", matSave1);
	cv::imwrite("E:/GitCode/OpenCV_Test/test_images/transpose1_cv.jpg", mat3_);

	return 0;
}

int test_transpose_float()
{
	cv::Mat matSrc = cv::imread("E:/GitCode/OpenCV_Test/test_images/lena.png", 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;
	cv::Mat matSrc_;
	cv::resize(matSrc, matSrc_, cv::Size(width, width));

	fbc::Mat_<float, 1> mat1(width, width);
	memcpy(mat1.data, matSrc_.data, width * width * sizeof(float));
	fbc::transpose(mat1, mat1);

	cv::Mat mat1_(width, width, CV_32FC1);
	memcpy(mat1_.data, matSrc_.data, width * width * sizeof(float));
	cv::transpose(mat1_, mat1_);

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

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

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

	width = matSrc1.cols;
	height = matSrc1.rows;

	fbc::Mat_<float, 1> mat2(height, width, matSrc1.data);
	fbc::Mat_<float, 1> mat3(width, height);
	fbc::transpose(mat2, mat3);

	cv::Mat mat2_(height, width, CV_32FC1, matSrc1.data);
	cv::Mat mat3_;
	cv::transpose(mat2_, mat3_);

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

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

	return 0;
}

GitHubhttps://github.com/fengbingchun/OpenCV_Test

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

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

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

  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中的绘图函数-OpenCV步步精深

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

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

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

  7. tf.transpose函数解析

    tf.transpose函数解析 觉得有用的话,欢迎一起讨论相互学习~Follow Me tf.transpose(a, perm = None, name = 'transpose') 解释 将a进 ...

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

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

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

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

随机推荐

  1. 开源重磅,java内容管理系统CMS,点击就可以编辑,保存,轻松构建自己的站点

    买的暂时空间不给力.内存不足,老给关闭,先转到京东云上了,免费的,也不知免费多久. 这是地址2  http://java4cms.jd-app.com/index.html 这是地址  http:// ...

  2. 【洛谷5294】[HNOI2019] 序列(主席树维护单调栈+二分)

    点此看题面 大致题意: 给你一个长度为\(n\)的序列\(A\),每次询问修改一个元素(只对当前询问有效),然后让你找到一个不下降序列\(B\),使得这两个序列相应位置之差的平方和最小,并输出这个最小 ...

  3. Uva 11491 暴力贪心

    题意:给一个n长度的整数,删掉 k 个数字,使得剩下的数字最大. 分析:还剩 n-k 个数字,就是在原序列里面,相对顺序不变的情况下,这个n-k个数字组成的数最大. 感觉没有什么特别好的方法策略,看了 ...

  4. HDU 2048 错排

    错排递推公式: d(n) = (n-1)*(d[n-1]+d[n-2]): 证明:将第n个元素放到第k处,第k处的元素如果放到第n处,就是d(n-2),否则,先假设放到第n处,然后错排,就是d(n-1 ...

  5. BestCoder Round #81 (div.2)

    HDU:5670~5764 A题: 是一个3进制计数: #include <bits/stdc++.h> using namespace std; ]; int calc(long lon ...

  6. LocalDB的奇怪问题

    属性 MasterDBPath 不可用于 信息“Microsoft.SqlServer.Management.Smo.Information”.该对象可能没有此属性,也可能是访问权限不足而无法检索. ...

  7. python-函数的使用

    一.函数的定义 首先,我们来看一个简单的例子来定义函数: def test(): print('hello') 在其中 def  : 关键字,用来告诉解释器,接下来的一段代码是一个函数 test : ...

  8. 【luogu P1307 数字反转】 题解

    题目链接:https://www.luogu.org/problemnew/show/P1307 刚入门的一道字符串模拟,分四种情况讨论来做比较好. #include<iostream> ...

  9. C# 动态改变webservice的访问地址

    1.添加一个App.config配置文件. 2.配置服务http://Lenovo-PC:80/EvisaWS/WharfService?wsdl,那么在上面的文件中就会自动生成服务的配置: < ...

  10. HDU 1254 推箱子(BFS加优先队列)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1254 推箱子 Time Limit: 2000/1000 MS (Java/Others)    Me ...