0
votes

Well, I'm new at openCV and image processing and I definitely need help. I need to get every pixel values of the Mat . This image is captured by webcam (real time). I've tried some of the solution from some people's problems, but it's not working on my project. This is some of my code :

    Mat blackWhite;
    Mat binary;
    Mat distanceTr;
    cvtColor(frame, blackWhite, CV_BGR2GRAY);
    GaussianBlur(blackWhite, blackWhite, Size(15,15), 6.5, 6.5);
    threshold(blackWhite, binary, 80, 255, CV_THRESH_BINARY);
    Mat invBinary =  Scalar(255) - binary;
    distanceTransform(invBinary, distanceTr, CV_DIST_L2,3);

And I for the pixel value accessing, I made this :

for(int i = 0; i < distanceTr.rows; i++)
{
    for(int j = 0; j < distanceTr.cols; j++)
    {
        if(distanceTr.at<uchar>(0,0) == 0)
        {
            cout << 1;
        }
    }
}

For the webcam show, I use

    imshow("Project", distanceTr);

As far as I know, the distanceTr is a grayscale image, so it's a 1-channel image. Based on some answers, I tried the .at<> but it's not working :( I'm doing a test case, if the (0,0) pixel is black, it'll print 1. What I get is an unhandled exception error. Could you give any help, please?

Thank you very much. (Sorry for bad English)

1
The output image of distanceTrandform is of type CV_32FC1, so may be you should do this: if(distanceTr.at<float>(0,0) == 0) cout<<1sgarizvi
@sgar91 Yep, thank you very much. It's working now. :) Where do I get the information about what type of image give what kind of output? Like this case distanceTransform makes CV_32FC1.user3567271
why are you checking the same location(0,0) over and over in that loop ? does not make any sense.berak
@berak It probably is (i,j)a-Jays
@berak It's still in the beginning of the process, so I just want to check whether the (0,0) pixel is black or not. If black, then I just print 1, else not. Just a simple checking. Later I'll change it to (i,j). Please correct me, if I made some mistakes.user3567271

1 Answers

2
votes

The output image of distanceTransform is of type CV_32FC1, so each pixel is of type float. To access the pixel value, you have to do the following:

if(distanceTr.at<float>(0,0) == 0)
{
   cout<<1;
}

In the opencv documentation, the description of each function parameter is given, like what is the size and type of the image etc.