簡體   English   中英

opencv矩陣除以標量會產生非常大/小的數字

[英]opencv matrix division by scalar produces very large/small numbers

我拍攝了RGB圖像。 我嘗試將其轉換為向量(代碼如下)。 當我讀取向量時,我會得到非常大或非常小的值。 我認為這與從函數返回的向量有關,但我無法弄清楚。 我得到的值是:2.36943e-38 2.36943e-38 -8256.25 -81920等。

#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui_c.h>
#include <iostream>
#include <string>
#include <vector>


using namespace cv;
using namespace std;

vector<float> mat_to_vec(Mat M){
    vector<float> array_temp;

    for (int i=0;i<M.cols;i++){
    for(int j=0;j<M.rows;j++)
    {
        array_temp.push_back(M.at<float>(j,i));
    }  
}
return array_temp;
}


int main(int argc, char** argv)
{

// read the image

Mat src=imread(argv[1],CV_LOAD_IMAGE_COLOR);

// separate the channels 

Mat bgr[3];   //destination array
split(src,bgr);//split source 

// convert to individual arrays and divide by 256
Mat temp_b=bgr[0]/256.0;
Mat temp_g=bgr[1]/256.0;
Mat temp_r=bgr[2]/256.0;

vector<float> array_b=mat_to_vec(temp_b);
vector<float> array_g=mat_to_vec(temp_g);
vector<float> array_r=mat_to_vec(temp_r);

// merge the arrays 

vector<float> array_rgb;
array_rgb.insert(array_rgb.end(), array_r.begin(), array_r.end());
array_rgb.insert(array_rgb.end(), array_g.begin(), array_g.end());
array_rgb.insert(array_rgb.end(), array_b.begin(), array_b.end());

for (int i=0; i<array_rgb.size();i++){
cout << array_rgb[i] << endl;
}
return 0;    
}

您需要將src圖像轉換為float元素類型的圖像,請使用src.convertTo(src,CV_32FC1);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM