【问题标题】:opencv cv::mat not returning the same resultopencv cv::mat 不返回相同的结果
【发布时间】:2015-04-01 21:43:44
【问题描述】:
    int sizeOfChannel = (_width / 2) * (_height / 2);
    double* channel_gr = new double[sizeOfChannel];

  // filling the data into channel_gr....

    cv::Mat my( _width/2, _height/2, CV_32F,channel_gr);        
    cv::Mat src(_width/2, _height/2, CV_32F);
    for (int i = 0; i < (_width/2) * (_height/2); ++i)
    {
        src.at<float>(i) = channel_gr[i];       
    }
    cv::imshow("src",src);
    cv::imshow("my",my);
    cv::waitKey(0);

我想知道为什么我在我的和 src 的 imshow 中没有得到相同的图像
更新: 我已将数组更改为 double* 仍然相同的结果; 我认为这与步骤有关?
我的图像输出

src 图像输出

【问题讨论】:

  • 可能是因为 channel_gr 是一个 int 数组,而不是一个 float 数组。
  • 用CV_64F怎么样?或者使用 int 数组的问题可能是?
  • @ha9u63ar 抛出异常
  • 这个 _width/2 是什么东西?还要注意,它是 Mat(row,cols,type),你把它反过来了。 (路上有很多坑坑洼洼,-恭喜,你都撞到了!)
  • @berak int sizeOfChannel = (_width / 2) * (_height / 2);获取原始图像并将其剪切成,因为我需要一个来自拜耳模式的通道。我对 cv::Mat(rows,cols,type); 没有问题我的问题是带有输入数组的ctor。像 Mat(int ndims, const int* sizes, int type, void* data, const size_t* steps=0);

标签: opencv


【解决方案1】:

这个对我有用:

int halfWidth = _width/2;
int halfHeight = _height/2;
int sizeOfChannel = halfHeight*halfWidth;

// ******************************* //
// you use CV_321FC1 later so it is single precision float
float* channel_gr = new float[sizeOfChannel];

// filling the data into channel_gr....
for(int i=0; i<sizeOfChannel; ++i) channel_gr[i] = i/(float)sizeOfChannel;



// ******************************* //
// changed row/col ordering, but this shouldnt be important
cv::Mat my( halfHeight , halfWidth , CV_32FC1,channel_gr);        
cv::Mat src(halfHeight , halfWidth, CV_32FC1);


// ******************************* //
// changed from 1D indexing to 2D indexing
for(int y=0; y<src.rows; ++y)
for(int x=0; x<src.cols; ++x)
{
    int arrayPos = y*halfWidth + x;
    // you have a 2D mat so access it in 2D
    src.at<float>(y,x) = channel_gr[arrayPos ];       
}


cv::imshow("src",src);
cv::imshow("my",my);

// check for differences
cv::imshow("diff1 > 0",src-my > 0);
cv::imshow("diff2 > 0",my-src > 0);
cv::waitKey(0);

【讨论】:

  • 谢谢,你认为问题只是我的channel_gr?哪个需要浮动而不是加倍?
  • 可能。但如果 src 不是连续的,如果您使用 1D 索引,则必须考虑 .step 值来访问正确的像素位置!
  • 已经为每个地区标记了哪些内容发生了变化。最重要的部分将是 float-instead-of-double 数组。 row-vs-col 可能重要也可能不重要。 1D-vs-2D 索引应该只在矩阵不连续的情况下成为问题,但对于稳定的代码可能应该关心。例如。如果您使用 IPP 支持,我猜矩阵不会一直是连续的。
【解决方案2】:

'my' 是浮点数组,但你给它一个指向双精度数组的指针。它无法正确地从这个数组中获取数据。

【讨论】:

    【解决方案3】:

    看来你使用的构造函数版本是

     Mat::Mat(int rows, int cols, int type, const Scalar& s)
    

    这是来自 OpenCV 文档。好像您使用floatsrc 并从channel_gr 分配(声明为double)。这不是某种形式的精度损失吗?

    【讨论】:

    • 是的,我知道这是一个精确损失。没关系
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多