mat2gray、interp1

本文介绍了MATLAB中用于图像处理的函数mat2gray及im2uint8的工作原理,解释了如何将数值映射到特定区间,并探讨了利用interp1进行图像插值的方法及其应用场景。

p26:

“函数mat2gray会将值限定在[0,1]范围内”,我的理解是该函数会自动检测输入的值的类型,0对应该类型的最小值,1对应该类型的最大值,将该数值进行变换。

例如输入uint8类型的数字(范围为0-255),该函数就会自动将0对应0,1对应255来进行变换。

im2uint8也同理。


p27:

interp1(z,T,f)

该函数的z与T为长度相同的列向量,他们其中的值一一对应。

例如z数组为:[1 2 3 4 5 6] 

T数组为:[0 20 60 68 77 100]

则1对应0,2对应20,3对应60,4对应68,5对应77,6对应100,通过该对应来对图像f进行变换。如原图像中灰度值在0至20的,线性变换为1至2;而20至60的,线性变换为2至3。


linspace(a,b,numel(T))

a为起始值,b为结束值,numel函数可获取数组的元素个数。该函数会根据T数组的元素个数来对a至b的值进行等距分割。

例如t为6元素的数组,a=0,b=1,该函数会输出[0 0.2 0.4 0.6 0.8 1]。

#ifndef STEREO_VISION_PROCESSOR_H #define STEREO_VISION_PROCESSOR_H #include <opencv2/opencv.hpp> #include <opencv2/cudaimgproc.hpp> #include <opencv2/cudawarping.hpp> #include <opencv2/cudacodec.hpp> #include <cuda_runtime.h> #include <device_launch_parameters.h> #include <chrono> #include <string> #include <vector> #include "FasterStereoCuda.h" #include "option_manager.h" using namespace cv; using namespace std; // CUDA错误检查宏 #ifndef CUDA_CHECK #define CUDA_CHECK(callstr)\ {\ cudaError_t error_code = callstr;\ if (error_code != cudaSuccess) {\ std::cerr << "CUDA error " << error_code << " at " << __FILE__ << ":" << __LINE__;\ assert(0);\ }\ } #endif // 使用页锁定内存 #define USING_PAGE_LOCKED_MEMORY class StereoVisionProcessor { public: // 构造函数析构函数 StereoVisionProcessor(); ~StereoVisionProcessor(); // 初始化函数 bool Initialize(const std::string& left2right_xml_path, const std::string& left2top_xml_path, int desired_width = 1920, int desired_height = 1080); // 处理单帧图像 bool ProcessFrame(const cv::Mat& input_frame, cv::Mat& disparity_map, cv::Mat& color_disparity_map, double& processing_time_ms); // 获取处理统计信息 struct ProcessingStats { double read_time_ms; double gray_conversion_time_ms; double image_cut_time_ms; double rectification_time_ms; double stereo_matching_time_ms; double normalization_time_ms; double image_registration_time_ms; double fusion_time_ms; double total_time_ms; double fps; }; ProcessingStats GetLastProcessingStats() const { return last_stats_; } // 设置相机参数 void SetCameraParameters(const cv::Mat& cameraMatrix1, const cv::Mat& cameraMatrix2, const cv::Mat& distCoeffs1, const cv::Mat& distCoeffs2, const cv::Mat& R, const cv::Mat& T, const cv::Mat& cameraMatrix1_l2t, const cv::Mat& cameraMatrix2_l2t, const cv::Mat& distCoeffs1_l2t, const cv::Mat& distCoeffs2_l2t, const cv::Mat& R_l2t, const cv::Mat& T_l2t); // 释放资源 void Release(); private: // 相机参数 cv::Mat cameraMatrix1_, cameraMatrix2_; cv::Mat distCoeffs1_, distCoeffs2_; cv::Mat R_, T_; cv::Mat cameraMatrix1_l2t_, cameraMatrix2_l2t_; cv::Mat distCoeffs1_l2t_, distCoeffs2_l2t_; cv::Mat R_l2t_, T_l2t_; // 立体校正参数 cv::Mat R1_, R2_, P1_, P2_, Q1_; cv::Mat R3_, R4_, P3_, P4_, Q2_; // 重映射矩阵 cv::Mat remapm_x_1_, remapm_y_1_, remapm_x_2_, remapm_y_2_; cv::Mat remapml2t_x_1_, remapml2t_y_1_, remapml2t_x_3_, remapml2t_y_3_; cv::Mat map_revert_x_, map_revert_y_; cv::Mat map_revert_t2r_x_, map_revert_t2r_y_; // GPU重映射矩阵 cv::cuda::GpuMat remapm_x_1_gpu_, remapm_y_1_gpu_, remapm_x_2_gpu_, remapm_y_2_gpu_; cv::cuda::GpuMat remapml2t_x_1_gpu_, remapml2t_y_1_gpu_, remapml2t_x_3_gpu_, remapml2t_y_3_gpu_; cv::cuda::GpuMat map_revert_x_gpu_, map_revert_y_gpu_; cv::cuda::GpuMat map_revert_t2r_x_gpu_, map_revert_t2r_y_gpu_; // 图像尺寸 cv::Size img_l2rsize_; cv::Size img_l2tsize_; int width_, height_; // 立体匹配器 FasterStereoCuda stereo1_, stereo2_; FasterStereoCuda::StereoOption1* ste_opt1_; FasterStereoCuda::StereoOption1* ste_opt2_; // CUDA内存 float *d_d_data_; int *d_min_, *d_max_; uchar *d_img_left_, *d_disp_mat_; uchar *d_img1_, *d_trans_, *d_dsts_; // 页锁定内存 unsigned char* img_left_; unsigned char* img_right_; float* d_data_; // 处理统计 ProcessingStats last_stats_; // 内部处理函数 bool InitializeStereoMatchers(); bool InitializeRemapMatrices(); bool InitializeCudaMemory(); void ProcessStereoMatching(int stereo_flag, const cv::Mat& left_img, const cv::Mat& right_img, cv::Mat& disparity_map); void OptimizeSeamCUDA(cv::Mat& img1, cv::Mat& trans, cv::Mat& dst); void ProcessDisparityCUDA(float* d_data, uchar* img_left, cv::Mat& disp_mat, int width, int height, int stereo_flag); void cuda_remap(cv::InputArray _src, cv::OutputArray _dst, cv::InputArray _map1, cv::InputArray _map2, int interpolation); cv::Mat ResizeImage(const cv::Mat& src, double scale, int interp = cv::INTER_LINEAR); }; #endif // STEREO_VISION_PROCESSOR_H 注释上述代码
09-23
/** * @file main.cpp * @author letterso * @brief modified form OroChippw/LightGlue-OnnxRunner * @version 0.5 * @date 2023-11-20 * * @copyright Copyright (c) 2023 * */ #include "transform_dpl.h" cv::Mat NormalizeImage(cv::Mat &Image) { cv::Mat normalizedImage = Image.clone(); if (Image.channels() == 3) { cv::cvtColor(normalizedImage, normalizedImage, cv::COLOR_BGR2RGB); normalizedImage.convertTo(normalizedImage, CV_32F, 1.0 / 255.0); } else if (Image.channels() == 1) { Image.convertTo(normalizedImage, CV_32F, 1.0 / 255.0); } else { throw std::invalid_argument("[ERROR] Not an image"); } return normalizedImage; } std::vector<cv::Point2f> NormalizeKeypoints(std::vector<cv::Point2f> kpts, int h, int w) { cv::Size size(w, h); cv::Point2f shift(static_cast<float>(w) / 2, static_cast<float>(h) / 2); float scale = static_cast<float>((std::max)(w, h)) / 2; std::vector<cv::Point2f> normalizedKpts; for (const cv::Point2f &kpt : kpts) { cv::Point2f normalizedKpt = (kpt - shift) / scale; normalizedKpts.push_back(normalizedKpt); } return normalizedKpts; } cv::Mat ResizeImage(const cv::Mat &Image, int size, float &scale, const std::string &fn, const std::string &interp) { // Resize an image to a fixed size, or according to max or min edge. int h = Image.rows; int w = Image.cols; std::function<int(int, int)> func; if (fn == "max") { func = [](int a, int b) { return (std::max)(a, b); }; ; } else if (fn == "min") { func = [](int a, int b) { return (std::min)(a, b); }; } else { throw std::invalid_argument("[ERROR] Incorrect function: " + fn); } int h_new, w_new; if (size == 512 || size == 1024 || size == 2048) { scale = static_cast<float>(size) / static_cast<float>(func(h, w)); h_new = static_cast<int>(round(h * scale)); w_new = static_cast<int>(round(w * scale)); } else { throw std::invalid_argument("Incorrect new size: " + std::to_string(size)); } int mode; if (interp == "linear") { mode = cv::INTER_LINEAR; } else if (interp == "cubic") { mode = cv::INTER_CUBIC; } else if (interp == "nearest") { mode = cv::INTER_NEAREST; } else if (interp == "area") { mode = cv::INTER_AREA; } else { throw std::invalid_argument("[ERROR] Incorrect interpolation mode: " + interp); } cv::Mat resizeImage; cv::resize(Image, resizeImage, cv::Size(w_new, h_new), 0, 0, mode); return resizeImage; } cv::Mat RGB2Grayscale(cv::Mat &Image) { cv::Mat resultImage; cv::cvtColor(Image, resultImage, cv::COLOR_RGB2GRAY); return resultImage; }请对该代码进行分析,得出其作用
最新发布
11-03
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值