我在理解某些编码时遇到了麻烦,对不起,如果这变得愚蠢,但是我有一条代码可以从我的网络摄像头捕获视频,我想从帧中获取RGB valuee,如果不可能,则必须保存一个框架作为图片,然后从中获取值(value)?
const char window_name [] =“网络摄像头”;
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);
最佳答案
哈 ! (显然是被复制粘贴糊涂器捕获了)
GetPixel()是Windows函数,不是opencv函数。对于GetRValue()和姐妹也一样。
您可以在本地的win32 api中使用它们,以从HDC获取像素,但由于HDC和HWND均未公开,因此它不适用于opencv / highgui。
因为您显然是新手(没问题,再次!)让我尝试不让您使用旧的1.0 opencv api(IplImages,cv * Functions),
您应该改用新的(cv::Mat, namespace cv::Functions)。
#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;
}
关于opencv - 从视频流中获取像素数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15384626/