// ConsoleApplication2.cpp : 定義控制臺應用程式的入口點。
//
#include "stdafx.h"
#include "ConsoleApplication3.h"
#include "stdafx.h"
#include <direct.h>
#include <opencv2/opencv.hpp>
#include <iostream>
#include <string.h>
#include <windows.h>
#include <stdio.h>
#include <ctime>
#include <atlstr.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// 唯一的應用程式物件
CWinApp theApp;
using namespace std;
using namespace cv;
int main()
{
int nRetCode = 0;
HMODULE hModule = ::GetModuleHandle(nullptr);
if (hModule != nullptr)
{
// 初始化 MFC 并在失敗時顯示錯誤
if (!AfxWinInit(hModule, nullptr, ::GetCommandLine(), 0))
{
// TODO: 更改錯誤代碼以符合您的需要
wprintf(L"錯誤: MFC 初始化失敗\n");
nRetCode = 1;
}
else
{
// TODO: 在此處為應用程式的行為撰寫代碼。
}
}
else
{
// TODO: 更改錯誤代碼以符合您的需要
wprintf(L"錯誤: GetModuleHandle 失敗\n");
nRetCode = 1;
}
VideoCapture cap;
int cnt = 0;
char pname[100];
std::cout << "請輸入攝像頭序號" << std::endl;
std::cin.getline(pname, 100);
int capNum = atoi(pname);
std::cout << "請輸入解析度大小:\
1:640*480 2:800*600 3:1024*768" << std::endl;
std::cin.getline(pname, 100);
int frameNum = atoi(pname);
cap.open(capNum);
switch (frameNum)
{
case 1:
{
cap.set(CV_CAP_PROP_FRAME_WIDTH, 640);
cap.set(CV_CAP_PROP_FRAME_HEIGHT, 480);
break;
}
case 2:
{
cap.set(CV_CAP_PROP_FRAME_WIDTH, 800);
cap.set(CV_CAP_PROP_FRAME_HEIGHT, 600);
break;
}
case 3:
{
cap.set(CV_CAP_PROP_FRAME_WIDTH, 1024);
cap.set(CV_CAP_PROP_FRAME_HEIGHT, 768);
break;
}
default:
cap.set(CV_CAP_PROP_FRAME_WIDTH, 640);
cap.set(CV_CAP_PROP_FRAME_HEIGHT, 480);
}
if (!cap.isOpened())
{
std::cerr << "Cannot read video. Try moving video file to sample directory." << std::endl;
return -1;
}
namedWindow("FG Segmentation", WINDOW_NORMAL);
Mat frame;
Mat edges;
CTime time = CTime::GetCurrentTime(); ///構造CTime物件
int m_nYear = time.GetYear(); ///年
int m_nMonth = time.GetMonth(); ///月
int m_nDay = time.GetDay(); ///日
int m_nHour = time.GetHour(); ///小時
int m_nMinute = time.GetMinute(); ///分鐘
int m_nSecond = time.GetSecond(); ///秒
CString m_strTime = time.Format("%Y-%m-%d-%H-%M-%S");
string path;
string filename;
string a;
a = m_strTime.GetBuffer(0);
m_strTime.ReleaseBuffer();
filename = "D\\PHOTOS_M\\" + a;
path = "md D:\\PHOTOS_M\\" + a;
system(path.c_str());
cout << "檔案夾" << filename << "已創建" << endl;
for (;;)
{
cap >> frame;
cap >> frame;
if (frame.empty())
break;
frame.copyTo(edges);
imshow("FG Segmentation", edges);
//std::cout << edges.type() << std::endl;
char c = waitKey(30);
switch (c)
{
case 's':
{
//OFSTRUCT ofStruct;
//OpenFile(filename.c_str(), &ofStruct, OF_READWRITE);
string filefolder;
filefolder = "D\\PHOTOS_M\\" +a + "\\picture%04d.bmp";
sprintf_s(pname, 100, filefolder.c_str(), cnt);
cnt++;
imwrite(pname, edges);
cvWaitKey(50);
cout << "save photos success!" << endl;
break;
}
case 'q':
{
return 0;
}
default:
break;
}
}
return nRetCode;
}
uj5u.com熱心網友回復:
imshow一下要保存的圖片,然后檢查下路徑寫成c:\\1.jpg這樣的試試uj5u.com熱心網友回復:
需要檢查有沒有相應的檔案夾轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/119421.html
標籤:基礎類
