Ask Your Question
1

Get pixel value of sobel Mat with C++

asked 2019-05-10 01:52:11 -0600

PaulV gravatar image

Hi,

I need to read the value of single pixels in an Mat. Normally thats no problem but I created the Mat with the Sobel command and always get an Error if I try to read the value of an Pixel. I created the Sobel Mat like that:

        // ### Sobel ###
    Mat sobel_x, sobel_y;
    /// Sobel X
    Sobel(src, sobel_x, ddepth, 1, 0, 5, scale, delta, BORDER_DEFAULT);
    /// Sobel Y
    Sobel(src, sobel_y, ddepth, 0, 1, 5, scale, delta, BORDER_DEFAULT);
    imshow("Sobel y", sobel_y);
    imshow("Sobel x", sobel_x);

If I try to read an Pixel I get this Error:

image description

I tried for example

        Scalar intensity1 = sobel_x.at<uchar>(100, 100);
    // or this:
    //Scalar intensity = sobel_x.at<uchar>(Point(100, 100));
    // or this:
    //Mat cropedImage = sobel_x(Rect(100, 100, 1, 1));
    //Scalar intensity1 = cropedImage.at<uchar>(1, 1);
    // even if I try to just the read type of the Mat I get an error:
    //int type = sobel_x.type();

hopefully someone can help me with that.

Thank you very much

Paul

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
3

answered 2019-05-10 03:45:34 -0600

berak gravatar image

updated 2019-05-10 03:50:57 -0600

please AVOID to use Mat::at as good as you can. in 95% of all cases it's the wrong thing to do.

to print out a 1 pixel Mat use:

cout << sobel_x(Rect(100, 100, 1, 1)) << endl;

this solves 3 problems:

  • you don't have to mess with the type. (what is ddepth, and why do you assume uchar here ?)
  • you don't have to mess with indexing (you did it wrong above, in a 1x1 Matrix, (0,0) is the only valid index, (1,1) is out of bounds))
  • you don't have to care, if cout treats your uchar values as ascii
edit flag offensive delete link more

Comments

Thank you for your fast answer. Your code works fine but how can I assign that value to a variable (for example integer)?

PaulV gravatar imagePaulV ( 2019-05-10 08:06:52 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2019-05-10 01:52:11 -0600

Seen: 874 times

Last updated: May 10 '19