C++ OpenCV C++;无法保存捕获的图像
首先,很抱歉我的英语不好。 我是OpenCV新手,希望保存从网络摄像头捕获的图像。 我从这个链接得到了代码C++ OpenCV C++;无法保存捕获的图像,c++,eclipse,opencv,C++,Eclipse,Opencv,首先,很抱歉我的英语不好。 我是OpenCV新手,希望保存从网络摄像头捕获的图像。 我从这个链接得到了代码 #包括 #包括 #包括“opencv2/highgui/highgui.hpp” #包括“opencv2/imgproc/imgproc_c.h” #包括“opencv2/imgproc/imgproc.hpp” int main(int argc,字符**argv) { 字符路径[255]; //对于Linux,保存捕获图像的路径 //strcpy(路径,“/home/samundra/
#包括
#包括
#包括“opencv2/highgui/highgui.hpp”
#包括“opencv2/imgproc/imgproc_c.h”
#包括“opencv2/imgproc/imgproc.hpp”
int main(int argc,字符**argv)
{
字符路径[255];
//对于Linux,保存捕获图像的路径
//strcpy(路径,“/home/samundra/sample.jpg”);
//对于Windows,保存捕获图像的路径
strcpy(路径,“c:\\home.jpg”);
//指向当前帧的指针
IplImage*框架;
//将图像大小调整为小尺寸
IplImage*小;
//创建捕获设备就绪
//这里0表示我们希望在第0个索引处使用摄影机
CvCapture*capture=cvCaptureFromCAM(0);
//highgui.h是必需的
cvNamedWindow(“捕获”,CV_窗口_自动调整大小);
帧=cvQueryFrame(捕获);
//必须在实际使用cvResize之前初始化
//我们正在创建的图像大小是捕获的原始帧的一半
//8=位3=通道
小=cvCreateImage(cvSize(帧->宽度/2,帧->高度/2),8,3);
而(1)
{
//从摄影机查询帧
帧=cvQueryFrame(捕获);
//调整抓取的帧的大小
cvResize(帧,小);//cvResize(源,目标)
//显示捕获的图像
cvShowImage(“捕获”,小);
char ch=cvWaitKey(25);//等待25毫秒,让用户按任意键
if(ch==27)break;//如果按了Escape键,则退出循环
//如果s是键盘,则保存图像
如果(ch='s')
{
cvSaveImage(路径,小);
}
}
//释放所有图像和窗口
cvReleaseImage(&frame);
cvReleaseImage(&小);
CVD窗口(“捕获”);
返回0;
}
我使用的是OpenCV 2.3.1和Eclipse,在编译完这段代码后,网络摄像头成功打开,但我的图像没有保存。谢谢你的帮助。在Linux下,它工作得很好。。。尝试将文件保存到.exe文件所在的目录。您可能没有在C驱动器上写入的权限。如果您想要绝对路径,我认为问题在于双反斜杠。
#include <iostream>
#include <opencv/cv.h>
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc_c.h"
#include "opencv2/imgproc/imgproc.hpp"
int main(int argc,char** argv)
{
char path[255];
// For Linux, path to save the captured image
//strcpy(path,"/home/samundra/sample.jpg");
// For Windows, path to save the captured image
strcpy(path,"c:\\home.jpg");
// Pointer to current frame
IplImage *frame;
//Resize image to small size
IplImage *small;
// Create capture device ready
// here 0 indicates that we want to use camera at 0th index
CvCapture *capture = cvCaptureFromCAM(0);
// highgui.h is required
cvNamedWindow("capture",CV_WINDOW_AUTOSIZE);
frame = cvQueryFrame(capture);
// Must be initialized before actually cvResize is used
// we are creating image of size that is half of the original frame captured
// 8 = Bits 3 = Channel
small = cvCreateImage(cvSize(frame->width/2,frame->height/2), 8, 3);
while(1)
{
// Query for Frame From Camera
frame = cvQueryFrame(capture);
// Resize the grabbed frame
cvResize(frame, small); //cvResize(source, destination)
// Display the captured image
cvShowImage("capture", small);
char ch = cvWaitKey(25); // Wait for 25 ms for user to hit any key
if(ch==27) break; // If Escape Key was hit just exit the loop
// Save image if s was keyboard
if(ch=='s')
{
cvSaveImage(path,small);
}
}
// Release All Images and Windows
cvReleaseImage(&frame);
cvReleaseImage(&small);
cvDestroyWindow("capture");
return 0;
}