Ask Your Question
1

Obtaining Pixelvalue with various formats

asked 2016-02-04 11:46:34 -0600

goldroses gravatar image

updated 2016-02-04 12:08:58 -0600

I tried to get the value underneath an cv::Mat dImg in the format 16UC1 but only get rubbish when obtaining one pixel value. I expect an int ranging inbetween 0 and 2^16-1 (65535) but this is what i get:

135530515
583506521733990419
4.45466e-34
1.18455e-269

with

       cout<<dImg.at<int>(240,320)<<endl;
       cout<<dImg.at<long>(240,320)<<endl;
       cout<<dImg.at<float>(240,320)<<endl;
       cout<<dImg.at<double>(240,320)<<endl;

EDIT: The image resolution is 640x480. I choose the position randomly, for others i get the same. (c++). EDIT2: Using ansers solution i get:

    125
    11
    157
    8
    0
    0

This looks fine yay :)

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
3

answered 2016-02-04 11:57:21 -0600

berak gravatar image

updated 2016-02-04 12:01:34 -0600

wait, if your image is CV_16UC, the only valid way to access it is:

cout<<dImg.at<ushort>(y,x)<<endl;

really, you can't choose types there at will, but have to stick to what's inside your Mat.

running a DEBUG build is also recommended here, it should throw an exception, if you do something wrong.

also, if your image is 320x240, you cannot access a pixel at (240,320), the largest(last) valid pixel is at (239,319) , remember, we start counting from 0.

edit flag offensive delete link more

Comments

2

I made a PR with extra info, to many people are still making this mistake. Feel free to comment with extra suggestions!

StevenPuttemans gravatar imageStevenPuttemans ( 2016-02-05 06:24:07 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2016-02-04 11:46:34 -0600

Seen: 450 times

Last updated: Feb 04 '16