Gilad
Gilad

Reputation: 6575

opencv cv::mat not returning the same result

    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);

I'm wondering why i'm not getting the same image in my and src imshow
update: I have changed my array into double* still same result; I think it is something to do with steps?
my image output enter image description here

src image output enter image description here

Upvotes: 0

Views: 121

Answers (3)

Micka
Micka

Reputation: 20130

this one works for me:

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);

Upvotes: 2

Michael Burdinov
Michael Burdinov

Reputation: 4428

'my' is array of floats but you give it pointer to arrays of double. There no way it can get data from this array properly.

Upvotes: 1

ha9u63a7
ha9u63a7

Reputation: 6804

It seems that the constructor version that you are using is

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

This is from OpenCV docs. Seems like you are using float for src and assigning from channel_gr (declared as double). Isn't that some form of precision loss?

Upvotes: 0

Related Questions