OpenCV中的transpose函数实现图像转置,公式为:
目前fbc_cv库中也实现了transpose函数,支持多通道,uchar和float两种数据类型,经测试,与OpenCV3.1结果完全一致。
实现代码transpose.hpp:
// fbc_cv is free software and uses the same licence as OpenCV
// Email: [email protected]
#ifndef FBC_CV_TRANSPOSE_HPP_
#define FBC_CV_TRANSPOSE_HPP_
/* reference: include/opencv2/core.hpp
modules/core/src/matrix.cpp
*/
#include
#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
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_
#include "test_transpose.hpp"
#include
#include
#include
#include
#include
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_ 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_ mat2(height, width, matSrc1.data);
fbc::Mat_ 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_ 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_ mat2(height, width, matSrc1.data);
fbc::Mat_ 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;
}
GitHub:https://github.com/fengbingchun/OpenCV_Test