关于OpenCV打开摄像头只显示灰色的三种解决办法

一、源码如下:(注意:将if(!frame) break;注释掉了)

试了好多种方法都没有效果,然后不小心把 if(!frame) break; 注释掉了,结果可以了!
#include "cv.h"
#include "highgui.h"

int main(int argc, char** argv) {
	cvNamedWindow("win");

	CvCapture* capture = cvCreateCameraCapture(0);
	IplImage* frame;

	while(1) {
		frame = cvQueryFrame(capture);
		//if(!frame) break;
		cvShowImage("win", frame);

		char c = cvWaitKey(50);
		if(c==27) break;
	}

	cvReleaseCapture(&capture);
	cvDestroyWindow("win");
	return 0;
}

二、用一种图形处理算法,这里用的是cvCanny算法,虽然不知道什么原因,但是可以显示摄像头的内容

#include "cv.h"
#include "highgui.h"

int main(int argc, char** argv) {
	cvNamedWindow("win");

	CvCapture* capture = cvCreateCameraCapture(0);
	IplImage* frame = cvQueryFrame(capture);

	while(1) {
		frame = cvQueryFrame(capture);
		if(!frame) break;

 		IplImage* out = cvCreateImage(cvGetSize(frame), frame->depth, 1);
 		cvCanny(frame, out, 100,100, 3);

		cvShowImage("win", out);

		cvReleaseImage(&out);

		char c = cvWaitKey(50);
		if(c==27) break;
	}

	cvReleaseCapture(&capture);
	cvDestroyWindow("win");
	return 0;
}

三、这应该是终极解决办法了
加头文件#include "cxcore.h"






你可能感兴趣的:(OpenCV)