Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Memory error accessing grayscale pixel data

Hi all,

Somewhat of a simple question, but haven't found the answer from searching around. I'm trying to work with direct pixel data, and am having trouble figuring out what determines the size of the mat so that I can determine my indexes. I set my capture to be 640x400, but attempting to access any pixel with an x-coordinate larger than 365 throws an exception for access violation. Resizing the mat seems to have no effect. Am I missing something simple here? My full code is below:

#include <opencv2/objdetect/objdetect.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>

#include <iostream>
#include <stdio.h>

using namespace std;
using namespace cv;

VideoCapture vid;
int main(int argc, const char** argv)
{

    vid.open(0);
    vid.set(CV_CAP_PROP_FRAME_WIDTH, 640);
    vid.set(CV_CAP_PROP_FRAME_HEIGHT, 400);
    if (!vid.isOpened()) // check if we succeeded
        return -1;


    for (;;)
    {
        Mat frame;
        Mat gray;
        vid >> frame;
        cvtColor(frame, gray, CV_BGR2GRAY);
        //resize(gray, gray, Size(640, 400), 0, 0, INTER_CUBIC);
        imshow("webcam", gray);
        cout << (int) gray.at<uchar>(639, 399);
        waitKey(30);
    }
}

Thanks in advance for any help.