0

I am having trouble if understanding certain coding i am sorry if this comes off as stupid but i have a code to capture a video from my webcam i want to get the RGB valuee from the frame, if this is impossible would have to to save a frame as a picture and then get values from it?

const char window_name[]="Webcam";

int main(int argc, char* argv[]) {

/* attempt to capture from any connected device */
CvCapture *capture=cvCaptureFromCAM(CV_CAP_ANY);
if(!capture)
{
    printf("Failed to initialise webcam\n");
    return -1;
}

/* create the output window */
cvNamedWindow(window_name, CV_WINDOW_NORMAL);

do
{
    /* attempt to grab a frame */

    IplImage *frame=cvQueryFrame(capture);
    if(!frame)
    {
        printf("Failed to get frame\n");
        break;
    }


    COLORREF myColAbsolute = GetPixel(frame, 10,10);//error in saying frame is not compatible with HDC.

    cout << "Red - " << (int)GetRValue(myColAbsolute) << endl;
    cout << "Green - " << (int)GetGValue(myColAbsolute) << endl;
    cout << "Blue - " << (int)GetBValue(myColAbsolute) << endl;


    /* show the frame */
    cvShowImage(window_name, frame);
Multimedia Mike
  • 10,946
  • 4
  • 44
  • 57
  • I think there might be some code missing since there is no end to the loop (I would expect a closing brace and a while condition). – Multimedia Mike Mar 13 '13 at 18:52

1 Answers1

1

ha ! ( obviously caught with a copy & paste bummer )

GetPixel() is a windows function, not an opencv one. same for GetRValue() and sisters.

you'd use them in the native win32 api, to get a pixel from an HDC, but it won't work with opencv/highgui, since neither HDC, nor HWND are exposed.

since you're obviously a beginner(nothing wrong with that, again!) let me try to talk you out of using the old, 1.0 opencv api(IplImages, cv*Functions) as well, you should be using the new one(cv::Mat, namespace cv::Functions) instead.

#include "opencv2/opencv.hpp"
#include "opencv2/highgui/highgui.hpp"

using namespace cv;
using namespace std;

int main()
{
    Mat frame;
    namedWindow("video", 1);

    VideoCapture cap(0);
    while ( cap.isOpened() )
    {
        cap >> frame;
        if(frame.empty()) break;

                int x=3, y=5;
                // Ladies and Gentlemen, the PIXEL!
                Vec3b pixel = frame.at<Vec3b>(y,x); // row,col, not x,y!
                cerr << "b:" << int(pixel[0]) << " g:" << int(pixel[1]) << " r:" << int(pixel[2]) << endl;

        imshow("video", frame);
        if(waitKey(30) >= 0) break;
    }   
    return 0;
}
berak
  • 35,979
  • 8
  • 85
  • 85