Ask Your Question
0

one pixel's color

asked 2013-03-10 06:36:46 -0600

this post is marked as community wiki

This post is a wiki. Anyone with karma >50 is welcome to improve it.

I use color Webcamera and I want to know one pixel's color I'm looking for an opencv function that define one pixel's color. for example:

int RED, GREEN, BLUE;

x=1;

y=1;

RED=RedColorFunction(x,y);

GREEN=GreenColorFunction(x,y);

BLUE=BlueColorFunction(x,y);

so I get the first pixel's color. Do you know similar fuction?

edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted
2

answered 2013-03-10 07:34:56 -0600

this post is marked as community wiki

This post is a wiki. Anyone with karma >50 is welcome to improve it.

oh, please don't use the old api(IplImages, cvfunctions) but instead the new one (cv::Mat, cv::functions)

using namespace cv;
using namespace std;

int main()
{
    int x=1, y=1; // pixel location
    Mat frame;
    namedWindow("cam");
    VideoCapture cap(0);
    while( cap.isOpened() )
    {
         cap >> frame;
         if ( frame.empty() )
              break;
         Vec3b pixel = frame.at<Vec3b>( y, x );  // row,col index (NOT x,y)
         int b = pixel[0];
         int g = pixel[1];
         int r = pixel[2];
         cout << "r:" << r << " g:" << g << " b:" << b << endl;

         imshow("cam",frame);
         if ( waitKey(30) == 27) break;
    }
    return 0;
}
edit flag offensive delete link more

Comments

which libraries do you use?

Ecku01 gravatar imageEcku01 ( 2013-03-10 07:47:02 -0600 )edit

opencv_core244.lib opencv_highgui244.lib at least ( the number must fit your opencv version, and don't forget the 'd' at the end in the debug case )

berak gravatar imageberak ( 2013-03-10 07:59:17 -0600 )edit
0

answered 2013-03-10 06:50:09 -0600

this post is marked as community wiki

This post is a wiki. Anyone with karma >50 is welcome to improve it.

if you got a cv::Mat from your camera, get the pixel:

Vec3b pixel = frame.at<Vec3b>( y, x );

then you can access the colours, note that it's bgr-order here:

int b = pixel[0];
int g = pixel[1];
int r = pixel[2];
edit flag offensive delete link more

Comments

so I have this code:

include "opencv/cv.h"

include "opencv/highgui.h"

include <stdio.h>

include <iostream>

using namespace std;

int b,g,r;

int main() { CvCapture* capture = cvCaptureFromCAM(0);

if ( !capture ) {

 fprintf( stderr, "ERROR: capture is NULL \n" );

 getchar();

 return -1;

} cvNamedWindow( "mywindow", CV_WINDOW_AUTOSIZE );

  while ( 1 ) {

 IplImage* frame = cvQueryFrame( capture );


 cout &lt;&lt; b &lt;&lt;" " ;

 cout &lt;&lt; g &lt;&lt;" " ;

 cout &lt;&lt; r &lt;&lt; endl;

 cvShowImage( "mywindow", frame );

 if ( (cvWaitKey(10) &amp; 255) == 27 ) break;

}

cvReleaseCapture( &capture );

cvDestroyWindow( "mywindow" );

return 0; }

How can I do this? ( Sorry, I'm very beginner but I must to do it. and thanks for your help.) :)

Ecku01 gravatar imageEcku01 ( 2013-03-10 07:13:10 -0600 )edit

Question Tools

Stats

Asked: 2013-03-10 06:36:46 -0600

Seen: 2,877 times

Last updated: Mar 10 '13