我是openCV的新手,我终于打开了一个窗口并显示网络摄像头输出。
我现在正试图在显示帧之前将帧转换为灰度。不幸的是,每次我尝试编译时都会出现以下错误。 error: ‘cvCvtColor’ was not declared in this scope
我正在使用以下行编译
g ++ main.cpp -lopencv_core -lopencv_imgproc -lopencv_highgui -lopencv_objdetect -o camera
我是否错误地链接了我的代码?这是完整的代码清单。抱歉我的格式问题!
#include "opencv2/objdetect/objdetect.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/core/core.hpp"
#include <stdio.h>
#include <unistd.h>
IplImage* getCameraFrame(CvCapture* &camera)
{
IplImage *frame;
int w, h;
// If the camera hasn't been initialized, then open it.
if (!camera) {
printf("Acessing the camera ...\n");
camera = cvCreateCameraCapture( 0 );
if (!camera) {
printf("Couldn't access the camera.\n");
exit(1);
}
// Try to set the camera resolution to 320 x 240.
cvSetCaptureProperty(camera, CV_CAP_PROP_FRAME_WIDTH, 320);
cvSetCaptureProperty(camera, CV_CAP_PROP_FRAME_HEIGHT, 240);
// Get the first frame, to make sure the camera is initialized.
frame = cvQueryFrame( camera );
if (frame) {
w = frame->width;
h = frame->height;
printf("Got the camera at %dx%d resolution.\n", w, h);
}
// Wait a little, so that the camera can auto-adjust its brightness.
sleep(1); // (in milliseconds)
}
// Wait until the next camera frame is ready, then grab it.
frame = cvQueryFrame( camera );
if (!frame) {
printf("Couldn't grab a camera frame.\n");
exit(1);
}
return frame;
}
int main(int argc, char* argv[])
{
cvNamedWindow( "Example2", CV_WINDOW_AUTOSIZE );
CvCapture* capture = cvCreateCameraCapture( 0 );
IplImage* frame;
while(1) {
frame = cvQueryFrame( capture );
if( !frame ) break;
cvCvtColor(frame, frame, CV_RGB2GRAY);
cvShowImage( "Example2", frame );
char c = cvWaitKey(33);
if( c == 27 ) break;
}
cvReleaseCapture( &capture );
cvDestroyWindow( "Example2" );
return 0;
}
答案 0 :(得分:4)
你正在使用c ++ api的标题,但不推荐使用c-api的代码(请不要!!)。
cv::VideoCapture cap(0);
while(cap.IsOpened()) {
cv::Mat f,g;
if ( ! cap.read(f) ) break;
cv::cvtColor(f, g, CV_RGB2GRAY);
cv::imshow("lalala",g);
if ( waitKey(10) == 27 ) break;
}
return 0;