Ask Your Question

Memory error accessing grayscale pixel data

asked 2016-01-21 22:25:14 -0500

MartinTarrou gravatar image

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.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)<uchar>(639, 399);

Thanks in advance for any help.

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2016-01-21 23:28:49 -0500

berak gravatar image

updated 2016-01-21 23:30:06 -0500

welcome to row/col world ;)

cout << (int)<uchar>(399, 639);

you have to use y,x not x,y ..

also, please have a look at basic tutorials

edit flag offensive delete link more


This is the code that breaks down my projects like 9 times of 10 :D I hate it!

StevenPuttemans gravatar imageStevenPuttemans ( 2016-01-22 04:29:06 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower


Asked: 2016-01-21 22:25:14 -0500

Seen: 185 times

Last updated: Jan 21 '16