|
armadillo与opencv的mat(c++版)相互转换
- #include <iostream>
- #include <armadillo>
- #include <cmath>
- #include <complex>
- #include <vector>
- #include "opencv2/opencv.hpp"
- #include <string>
- using namespace std;
- //armadillo与opencv的mat(c++版)相互转换
- static void Cv_mat_to_arma_mat(const cv::Mat& cv_mat_in, arma::mat& arma_mat_out)
- {//convert unsigned int cv::Mat to arma::Mat<double>
- for(int r=0;r<cv_mat_in.rows;r++){
- for(int c=0;c<cv_mat_in.cols;c++){
- // 这里除以了255.0
- arma_mat_out(r,c)=cv_mat_in.data[r*cv_mat_in.cols+c]/255.0;
- }
- }
- }
- template<typename T>
- static void Arma_mat_to_cv_mat(const arma::Mat<T>& arma_mat_in,cv::Mat_<T>& cv_mat_out)
- {
- //通过 .memptr() 访问 Armadillo 矩阵使用的内存
- cv::transpose(cv::Mat_<T>(static_cast<int>(arma_mat_in.n_cols),
- static_cast<int>(arma_mat_in.n_rows),
- const_cast<T*>(arma_mat_in.memptr())),
- cv_mat_out);
- }
- int main(){
- // opencv 读入一张图片 arma的矩阵 如何保存为图片呢? 用opencv
- string img_path = "/home/jiang/图片/py_雷达算法仿真/bp成像建模与ifft_py_c++对比02.png";
-
- cv::Mat image_cv;
- image_cv = cv::imread(img_path, 0);
- cout << image_cv.channels() << endl;
- // /图像尺寸
- // cout << "size:" << image.size << endl;
- //列宽
- cout << "cols:" << image_cv.cols<<endl;
- //行高
- cout << "rows:" << image_cv.rows << endl;
- //维度
- cout << "dims:" << image_cv.dims << endl;
- //opencv的图片矩阵 转换为 arma::mat
- arma::mat img(image_cv.rows,image_cv.cols, arma::fill::zeros);
- Cv_mat_to_arma_mat(image_cv, img); // opencv的mat 转为 arma的mat
- // img.save("img_arma.txt", arma::raw_ascii);
- cv::namedWindow("Display Image", cv::WINDOW_AUTOSIZE);
- cv::imshow("Display Image", image_cv);
- cv::waitKey(0);
- // arma::imat arma_img = arma::imat(100, 100, arma::fill::ones) * 255;
- // cv::Mat_<int> cv_img;
- // Arma_mat_to_cv_mat<int>(arma_img,cv_img);
- //convert arma::mat to cv::Mat
- cv::Mat_<int> cv_img;
- arma::Mat<int> arma_img = arma::Mat<int>(100, 200, arma::fill::ones) * 255;
- // umat = Mat<uword>
- // imat = Mat<sword>
- //sword is a typedef for a signed integer type
-
- Arma_mat_to_cv_mat<int>(arma_img,cv_img);
- cv::imwrite("b.png", cv_img);
- return 0;
- }
复制代码
|
|