PageRenderTime 80ms CodeModel.GetById 33ms RepoModel.GetById 1ms app.codeStats 0ms

/high/RPS/src/main.cpp

https://github.com/edilello/OpenPNL
C++ | 861 lines | 651 code | 166 blank | 44 comment | 157 complexity | c0f77420163ea37a1eaff8cce4192b8a MD5 | raw file
  1. #include <windows.h>
  2. #include <time.h>
  3. #include "cv.h"
  4. #include "highgui.h"
  5. #include "cvcam.h"
  6. #include "RPSGame.h"
  7. #include <string>
  8. #include <iostream>
  9. const int DiffThreshold = 40;
  10. const double mhiDuration = 1500.0;
  11. const double ProcentOfPoints = 0.25;
  12. int BufferSize = 10;
  13. const int MaxBufferSize = 25;
  14. const int AuthenticFrameThreshold = 100;
  15. bool TimeToStopCapture = false;
  16. enum Modes
  17. {
  18. Undefined, Intro, MoveSelection, MoveShowing, Settings
  19. };
  20. enum Buttons
  21. {
  22. NoButton, Start, Exit, RockButton, PaperButton, ScissorsButton, SettingsButton,
  23. InfoButton, SpeedButton, ResolutionButton
  24. };
  25. enum Resolutions
  26. {
  27. r640, r800, r1024
  28. };
  29. void UpdateMHIImages(IplImage *pMHI, IplImage *pGrayMHI, IplImage *pImage, IplImage *pLastImage)
  30. {
  31. IplImage *pGrayImage = cvCreateImage(cvSize(pImage->width, pImage->height), IPL_DEPTH_8U, 1 );
  32. IplImage *pGrayImageLast = cvCreateImage(cvSize(pImage->width, pImage->height), IPL_DEPTH_8U, 1 );
  33. IplImage *pTempDifference = cvCreateImage( cvSize(pImage->width, pImage->height), IPL_DEPTH_8U, 1);
  34. cvCvtColor( pImage, pGrayImage, CV_BGR2GRAY );
  35. cvCvtColor( pLastImage, pGrayImageLast, CV_BGR2GRAY );
  36. cvAbsDiff( pGrayImage, pGrayImageLast, pTempDifference );
  37. cvFlip( pTempDifference, pTempDifference , 0 );
  38. //cvFlip( pGrayImageLast, pGrayImageLast , -1 );
  39. cvThreshold( pTempDifference, pTempDifference, DiffThreshold, 255, CV_THRESH_BINARY );
  40. double timestamp = static_cast<double>(clock());
  41. cvUpdateMotionHistory( pTempDifference, pMHI, timestamp, mhiDuration );
  42. for (int i = 0; i < pGrayMHI->imageSize; i++)
  43. {
  44. if (pMHI->imageData[i*4] || pMHI->imageData[i*4+1] || pMHI->imageData[i*4+2] || pMHI->imageData[i*4+3])
  45. {
  46. pGrayMHI->imageData[i] = static_cast<char>(255);
  47. }
  48. else
  49. {
  50. pGrayMHI->imageData[i] = 0;
  51. };
  52. };
  53. if (pGrayImage != 0)
  54. {
  55. cvReleaseImage(&pGrayImage);
  56. };
  57. if (pGrayImageLast != 0)
  58. {
  59. cvReleaseImage(&pGrayImageLast);
  60. };
  61. if (pTempDifference != 0)
  62. {
  63. cvReleaseImage(&pTempDifference);
  64. };
  65. };
  66. Buttons GetButton(IplImage *pGrayMHI, Modes Mode, int SubImageWidth, int SubImageHeight, int HStepLength, int VStepLength, IplImage *pImage)
  67. {
  68. CvRect RectROI;
  69. bool bStart;
  70. bool bExit;
  71. bool bSettings;
  72. bool bRock;
  73. bool bPaper;
  74. bool bScissors;
  75. bool bInfo;
  76. bool bSpeed;
  77. bool bResolution;
  78. if (Mode == Intro)
  79. {
  80. //Check sequentially start and exit button
  81. //start
  82. RectROI = cvRect(HStepLength, /*pGrayMHI->height - SubImageHeight - VStepLength*/SubImageHeight + VStepLength, SubImageWidth, SubImageHeight);
  83. cvSetImageROI(pGrayMHI, RectROI);
  84. bStart = cvCountNonZero(pGrayMHI) >= ProcentOfPoints*SubImageWidth*SubImageHeight;
  85. cvResetImageROI(pGrayMHI);
  86. //settings
  87. RectROI = cvRect(HStepLength*2 + SubImageWidth, /*pGrayMHI->height - SubImageHeight - VStepLength*/ SubImageHeight + VStepLength, SubImageWidth, SubImageHeight);
  88. cvSetImageROI(pGrayMHI, RectROI);
  89. bSettings = cvCountNonZero(pGrayMHI) >= ProcentOfPoints*SubImageWidth*SubImageHeight;
  90. cvResetImageROI(pGrayMHI);
  91. //exit
  92. RectROI = cvRect(HStepLength*3 + SubImageWidth*2, /*pGrayMHI->height - SubImageHeight - VStepLength*/ SubImageHeight + VStepLength, SubImageWidth, SubImageHeight);
  93. cvSetImageROI(pGrayMHI, RectROI);
  94. bExit = cvCountNonZero(pGrayMHI) >= ProcentOfPoints*SubImageWidth*SubImageHeight;
  95. cvResetImageROI(pGrayMHI);
  96. if (bStart && !bExit && !bSettings)
  97. {
  98. return Start;
  99. };
  100. if (bExit && !bStart &&!bSettings)
  101. {
  102. return Exit;
  103. };
  104. if (bSettings && !bExit && !bStart)
  105. {
  106. return SettingsButton;
  107. };
  108. };
  109. if (Mode == MoveSelection)
  110. {
  111. //Check r, p, s, exit
  112. //Rock
  113. RectROI = cvRect(HStepLength, /*pGrayMHI->height - SubImageHeight - VStepLength*/SubImageHeight + VStepLength, SubImageWidth, SubImageHeight);
  114. cvSetImageROI(pGrayMHI, RectROI);
  115. bRock = cvCountNonZero(pGrayMHI) >= ProcentOfPoints*SubImageWidth*SubImageHeight;
  116. cvResetImageROI(pGrayMHI);
  117. //Paper
  118. RectROI = cvRect(HStepLength*2 + SubImageWidth, /*pGrayMHI->height - SubImageHeight - VStepLength*/SubImageHeight + VStepLength, SubImageWidth, SubImageHeight);
  119. cvSetImageROI(pGrayMHI, RectROI);
  120. bPaper = cvCountNonZero(pGrayMHI) >= ProcentOfPoints*SubImageWidth*SubImageHeight;
  121. cvResetImageROI(pGrayMHI);
  122. //Scissors
  123. RectROI = cvRect(HStepLength*3 + SubImageWidth*2, /*pGrayMHI->height - SubImageHeight - VStepLength*/SubImageHeight + VStepLength, SubImageWidth, SubImageHeight);
  124. cvSetImageROI(pGrayMHI, RectROI);
  125. bScissors = cvCountNonZero(pGrayMHI) >= ProcentOfPoints*SubImageWidth*SubImageHeight;
  126. cvResetImageROI(pGrayMHI);
  127. //Exit
  128. RectROI = cvRect(HStepLength*4 + SubImageWidth*3, /*pGrayMHI->height - SubImageHeight - VStepLength*/SubImageHeight + VStepLength, SubImageWidth, SubImageHeight);
  129. cvSetImageROI(pGrayMHI, RectROI);
  130. bExit = cvCountNonZero(pGrayMHI) >= ProcentOfPoints*SubImageWidth*SubImageHeight;
  131. cvResetImageROI(pGrayMHI);
  132. if (bRock && !bPaper && !bScissors && !bExit)
  133. {
  134. return RockButton;
  135. };
  136. if (!bRock && bPaper && !bScissors && !bExit)
  137. {
  138. return PaperButton;
  139. };
  140. if (!bRock && !bPaper && bScissors && !bExit)
  141. {
  142. return ScissorsButton;
  143. };
  144. if (!bRock && !bPaper && !bScissors && bExit)
  145. {
  146. return Exit;
  147. };
  148. };
  149. if (Mode == Settings)
  150. {
  151. //Info
  152. RectROI = cvRect(HStepLength, /*pGrayMHI->height - SubImageHeight - VStepLength*/SubImageHeight + VStepLength, SubImageWidth, SubImageHeight);
  153. cvSetImageROI(pGrayMHI, RectROI);
  154. bInfo = cvCountNonZero(pGrayMHI) >= ProcentOfPoints*SubImageWidth*SubImageHeight;
  155. cvResetImageROI(pGrayMHI);
  156. //Resolution
  157. RectROI = cvRect(HStepLength*2 + SubImageWidth, /*pGrayMHI->height - SubImageHeight - VStepLength*/SubImageHeight + VStepLength, SubImageWidth, SubImageHeight);
  158. cvSetImageROI(pGrayMHI, RectROI);
  159. bResolution = cvCountNonZero(pGrayMHI) >= ProcentOfPoints*SubImageWidth*SubImageHeight;
  160. cvResetImageROI(pGrayMHI);
  161. //Response
  162. RectROI = cvRect(HStepLength*3 + SubImageWidth*2, /*pGrayMHI->height - SubImageHeight - VStepLength*/SubImageHeight + VStepLength, SubImageWidth, SubImageHeight);
  163. cvSetImageROI(pGrayMHI, RectROI);
  164. bSpeed = cvCountNonZero(pGrayMHI) >= ProcentOfPoints*SubImageWidth*SubImageHeight;
  165. cvResetImageROI(pGrayMHI);
  166. //Exit
  167. RectROI = cvRect(HStepLength*4 + SubImageWidth*3, /*pGrayMHI->height - SubImageHeight - VStepLength*/SubImageHeight + VStepLength, SubImageWidth, SubImageHeight);
  168. cvSetImageROI(pGrayMHI, RectROI);
  169. bExit = cvCountNonZero(pGrayMHI) >= ProcentOfPoints*SubImageWidth*SubImageHeight;
  170. cvResetImageROI(pGrayMHI);
  171. if (bInfo && !bResolution && !bSpeed && !bExit)
  172. {
  173. return InfoButton;
  174. };
  175. if (!bInfo && bResolution && !bSpeed && !bExit)
  176. {
  177. return ResolutionButton;
  178. };
  179. if (!bInfo && !bResolution && bSpeed && !bExit)
  180. {
  181. return SpeedButton;
  182. };
  183. if (!bInfo && !bResolution && !bSpeed && bExit)
  184. {
  185. return Exit;
  186. };
  187. };
  188. return NoButton;
  189. };
  190. void Process(CvCapture* pCapture)
  191. {
  192. int ComputerWon = 0;
  193. int HumanWon = 0;
  194. Modes Mode = Intro;
  195. IplImage *pRock = cvLoadImage( "..\\rps\\pix\\rock.bmp", 1 );
  196. IplImage *pPaper = cvLoadImage( "..\\rps\\pix\\paper.bmp", 1);
  197. IplImage *pScissors = cvLoadImage( "..\\rps\\pix\\scissors.bmp", 1);
  198. IplImage *pExit = cvLoadImage( "..\\rps\\pix\\exit.bmp", 1);
  199. IplImage *pStart = cvLoadImage( "..\\rps\\pix\\start.bmp", 1 );
  200. IplImage *pSettings = cvLoadImage( "..\\rps\\pix\\settings.bmp", 1 );
  201. IplImage *pInfo = cvLoadImage( "..\\rps\\pix\\info.bmp", 1);
  202. IplImage *pResolution = cvLoadImage( "..\\rps\\pix\\resolution.bmp", 1 );
  203. IplImage *pSpeed = cvLoadImage( "..\\rps\\pix\\speed.bmp", 1 );
  204. cvFlip( pRock, NULL, 0);
  205. cvFlip( pPaper, NULL, 0);
  206. cvFlip( pScissors, NULL, 0);
  207. cvFlip( pExit, NULL, 0);
  208. cvFlip( pStart, NULL, 0);
  209. cvFlip( pSettings, NULL, 0);
  210. cvFlip( pInfo, NULL, 0);
  211. cvFlip( pResolution, NULL, 0);
  212. cvFlip( pSpeed, NULL, 0);
  213. //Sizes of the full image
  214. int Width = 0;
  215. int Height = 0;
  216. //Sizes of button for the case of mode = MoveSelection
  217. int WidthButtonSelection;
  218. int HeightButtonSelection;
  219. int StepLengthSelection;
  220. int StepLengthIntro;
  221. //MHI images: pBlackWhiteMHI is black and white MHI for internal purposes
  222. IplImage * pBlackWhiteMHI = 0;
  223. //MHI image
  224. IplImage * pMHI = 0;
  225. IplImage *pLastImage = 0;
  226. Modes *BufferModes = new Modes[MaxBufferSize];
  227. Buttons *BufferButtons = new Buttons[MaxBufferSize];
  228. int LastIndex = -1;
  229. for (int index = 0; index < MaxBufferSize; index++)
  230. {
  231. BufferModes[index] = Undefined;
  232. BufferButtons[index] = NoButton;
  233. };
  234. int FrameCounter = 0;
  235. int FrameToSave = 0;
  236. RPSMove ComputerMove;
  237. RPSMove HumanMove;
  238. //Current distribution
  239. float fPaper;
  240. float fRock;
  241. float fScissors;
  242. SetRandomDistribution();
  243. bool NeedToShowDistribution = false;
  244. Resolutions Resolution = r640;
  245. bool RecreateImages = false;
  246. //Set the current distribution
  247. GetCurrentDistribution(fRock, fPaper, fScissors);
  248. CvFont font;
  249. cvInitFont( &font, CV_FONT_HERSHEY_SIMPLEX , 1.0f, 1.0f, 0.0f, 3);
  250. while (!TimeToStopCapture)
  251. {
  252. IplImage* pImage2;
  253. IplImage* pImage;
  254. if( !cvGrabFrame( pCapture ))
  255. {
  256. TimeToStopCapture = true;
  257. continue;
  258. };
  259. pImage2 = cvRetrieveFrame( pCapture );
  260. switch (Resolution)
  261. {
  262. case r640:
  263. pImage = cvCreateImage( cvSize( 640,480 ), pImage2->depth, pImage2->nChannels );
  264. break;
  265. case r800:
  266. pImage = cvCreateImage( cvSize( 800,600 ), pImage2->depth, pImage2->nChannels );
  267. break;
  268. case r1024:
  269. pImage = cvCreateImage( cvSize( 1024,768 ), pImage2->depth, pImage2->nChannels );
  270. break;
  271. default:
  272. pImage = cvCreateImage( cvSize( 640,480 ), pImage2->depth, pImage2->nChannels );
  273. break;
  274. };
  275. pImage->origin = 1;
  276. cvResize(pImage2, pImage);
  277. //cvResize(pImage, pImage);
  278. cvFlip(pImage, 0, 1);
  279. if (!pMHI)
  280. {
  281. Width = pImage->width;
  282. Height = pImage->height;
  283. WidthButtonSelection = pRock->width;
  284. HeightButtonSelection = pRock->height;
  285. StepLengthSelection = (Width - 4*WidthButtonSelection) / 5;
  286. StepLengthIntro = (Width - 3*WidthButtonSelection) / 4;
  287. pMHI = cvCreateImage( cvSize( Width,Height ), IPL_DEPTH_32F, 1 );
  288. cvZero( pMHI );
  289. pBlackWhiteMHI = cvCreateImage( cvSize( Width,Height ), IPL_DEPTH_8U, 1 );
  290. cvZero( pBlackWhiteMHI );
  291. pLastImage = cvCreateImage( cvSize( Width,Height ), IPL_DEPTH_8U, 3 );
  292. cvZero( pLastImage );
  293. };
  294. UpdateMHIImages(pMHI, pBlackWhiteMHI, pImage, pLastImage);
  295. cvCopy(pImage, pLastImage);
  296. CvRect RectROI;
  297. if (FrameCounter >= AuthenticFrameThreshold)
  298. {
  299. char buffer[256];
  300. switch (Mode)
  301. {
  302. case Intro:
  303. //Show start and exit on the top of the screen
  304. //Start
  305. RectROI = cvRect(StepLengthIntro, Height - StepLengthSelection/2 - HeightButtonSelection, WidthButtonSelection, HeightButtonSelection);
  306. cvSetImageROI(pImage, RectROI);
  307. cvCopy( pStart, pImage );
  308. cvResetImageROI(pImage);
  309. //Settings
  310. RectROI = cvRect(StepLengthIntro*2 + WidthButtonSelection, Height - StepLengthSelection/2 - HeightButtonSelection, WidthButtonSelection, HeightButtonSelection);
  311. cvSetImageROI(pImage, RectROI);
  312. cvCopy( pSettings, pImage );
  313. cvResetImageROI(pImage);
  314. //Exit
  315. RectROI = cvRect(StepLengthIntro*3 + WidthButtonSelection*2, Height - StepLengthSelection/2 - HeightButtonSelection, WidthButtonSelection, HeightButtonSelection);
  316. cvSetImageROI(pImage, RectROI);
  317. cvCopy( pExit, pImage );
  318. cvResetImageROI(pImage);
  319. break;
  320. case Settings:
  321. //Info
  322. RectROI = cvRect(StepLengthSelection, Height - StepLengthSelection/2 - HeightButtonSelection, WidthButtonSelection, HeightButtonSelection);
  323. cvSetImageROI(pImage, RectROI);
  324. cvCopy( pInfo, pImage );
  325. cvResetImageROI(pImage);
  326. //Resolution
  327. RectROI = cvRect(StepLengthSelection*2 + WidthButtonSelection, Height - StepLengthSelection/2 - HeightButtonSelection, WidthButtonSelection, HeightButtonSelection);
  328. cvSetImageROI(pImage, RectROI);
  329. cvCopy( pResolution, pImage );
  330. cvResetImageROI(pImage);
  331. //Speed
  332. RectROI = cvRect(StepLengthSelection*3 + WidthButtonSelection*2, Height - StepLengthSelection/2 - HeightButtonSelection, WidthButtonSelection, HeightButtonSelection);
  333. cvSetImageROI(pImage, RectROI);
  334. cvCopy( pSpeed, pImage );
  335. cvResetImageROI(pImage);
  336. //Exit
  337. RectROI = cvRect(StepLengthSelection*4 + WidthButtonSelection*3, Height - StepLengthSelection/2 - HeightButtonSelection, WidthButtonSelection, HeightButtonSelection);
  338. cvSetImageROI(pImage, RectROI);
  339. cvCopy( pExit, pImage );
  340. cvResetImageROI(pImage);
  341. //Show text information about selected options
  342. if (true)
  343. {
  344. sprintf(buffer, "Show distribution: %s", (NeedToShowDistribution)?("yes"):("no"));
  345. std::string ShowDistribution = buffer;
  346. switch (Resolution)
  347. {
  348. case r640:
  349. sprintf(buffer, "Resolution: 640*480" );
  350. break;
  351. case r800:
  352. sprintf(buffer, "Resolution: 800*600" );
  353. break;
  354. case r1024:
  355. sprintf(buffer, "Resolution: 1024*768" );
  356. break;
  357. default:
  358. sprintf(buffer, "Resolution: 640*480" );
  359. break;
  360. };
  361. std::string Resolution = buffer;
  362. sprintf(buffer, "Motion duration (response): %i", BufferSize);
  363. std::string MotionDuration = buffer;
  364. cvPutText( pImage, ShowDistribution.c_str(), cvPoint(StepLengthSelection, Height - StepLengthSelection/2 - HeightButtonSelection*3+60), &font, CV_RGB(255,255,255) );
  365. cvPutText( pImage, Resolution.c_str(), cvPoint(StepLengthSelection, Height - StepLengthSelection/2 - HeightButtonSelection*3+30), &font, CV_RGB(255,255,255) );
  366. cvPutText( pImage, MotionDuration.c_str(), cvPoint(StepLengthSelection, Height - StepLengthSelection/2 - HeightButtonSelection*3), &font, CV_RGB(255,255,255) );
  367. };
  368. break;
  369. case MoveSelection:
  370. case MoveShowing:
  371. //Show r, p, s on the top of the screen
  372. //Rock
  373. RectROI = cvRect(StepLengthSelection, Height - StepLengthSelection/2 - HeightButtonSelection, WidthButtonSelection, HeightButtonSelection);
  374. cvSetImageROI(pImage, RectROI);
  375. cvCopy( pRock, pImage );
  376. cvResetImageROI(pImage);
  377. //Paper
  378. RectROI = cvRect(StepLengthSelection*2 + WidthButtonSelection, Height - StepLengthSelection/2 - HeightButtonSelection, WidthButtonSelection, HeightButtonSelection);
  379. cvSetImageROI(pImage, RectROI);
  380. cvCopy( pPaper, pImage );
  381. cvResetImageROI(pImage);
  382. //Scissors
  383. RectROI = cvRect(StepLengthSelection*3 + WidthButtonSelection*2, Height - StepLengthSelection/2 - HeightButtonSelection, WidthButtonSelection, HeightButtonSelection);
  384. cvSetImageROI(pImage, RectROI);
  385. cvCopy( pScissors, pImage );
  386. cvResetImageROI(pImage);
  387. //Exit
  388. RectROI = cvRect(StepLengthSelection*4 + WidthButtonSelection*3, Height - StepLengthSelection/2 - HeightButtonSelection, WidthButtonSelection, HeightButtonSelection);
  389. cvSetImageROI(pImage, RectROI);
  390. cvCopy( pExit, pImage );
  391. cvResetImageROI(pImage);
  392. if ((Mode == MoveSelection)&&(NeedToShowDistribution))
  393. {
  394. sprintf(buffer, "Rock: %.3f", fRock);
  395. std::string RockStr = buffer;
  396. sprintf(buffer, "Paper: %.3f", fPaper);
  397. std::string PaperStr = buffer;
  398. sprintf(buffer, "Scissors: %.3f", fScissors);
  399. std::string ScissorsStr = buffer;
  400. //Show probability distribution
  401. cvPutText( pImage, RockStr.c_str(), cvPoint(StepLengthSelection, Height - StepLengthSelection/2 - HeightButtonSelection*3), &font, CV_RGB(255,255,255) );
  402. cvPutText( pImage, PaperStr.c_str(), cvPoint(StepLengthSelection, Height - StepLengthSelection/2 - HeightButtonSelection*3+30), &font, CV_RGB(255,255,255) );
  403. cvPutText( pImage, ScissorsStr.c_str(), cvPoint(StepLengthSelection, Height - StepLengthSelection/2 - HeightButtonSelection*3+60), &font, CV_RGB(255,255,255) );
  404. };
  405. if (Mode == MoveShowing)
  406. {
  407. //Show selected moves on the screen
  408. IplImage *pComputerMove;
  409. IplImage *pHumanMove;
  410. switch (ComputerMove)
  411. {
  412. case Rock:
  413. pComputerMove = pRock;
  414. break;
  415. case Paper:
  416. pComputerMove = pPaper;
  417. break;
  418. case Scissors:
  419. pComputerMove = pScissors;
  420. break;
  421. };
  422. switch (HumanMove)
  423. {
  424. case Rock:
  425. pHumanMove = pRock;
  426. break;
  427. case Paper:
  428. pHumanMove = pPaper;
  429. break;
  430. case Scissors:
  431. pHumanMove = pScissors;
  432. break;
  433. };
  434. //Human's move
  435. RectROI = cvRect(StepLengthSelection*2 + WidthButtonSelection, Height - StepLengthSelection/2 - HeightButtonSelection*3, WidthButtonSelection, HeightButtonSelection);
  436. cvSetImageROI(pImage, RectROI);
  437. cvCopy( pHumanMove, pImage );
  438. cvResetImageROI(pImage);
  439. //Computer's move
  440. RectROI = cvRect(StepLengthSelection*3 + WidthButtonSelection*2, Height - StepLengthSelection/2 - HeightButtonSelection*3, WidthButtonSelection, HeightButtonSelection);
  441. cvSetImageROI(pImage, RectROI);
  442. cvCopy( pComputerMove, pImage );
  443. cvResetImageROI(pImage);
  444. if (FrameCounter - FrameToSave > BufferSize*2)
  445. {
  446. Mode = MoveSelection;
  447. //Set the current distribution
  448. GetCurrentDistribution(fRock, fPaper, fScissors);
  449. };
  450. };
  451. //Show the score
  452. char buffer[256];
  453. std::string HumanStr = "Human: ";
  454. itoa(HumanWon, buffer, 10);
  455. HumanStr += buffer;
  456. std::string ComputerStr = "Computer: ";
  457. itoa(ComputerWon, buffer, 10);
  458. ComputerStr += buffer;
  459. cvPutText( pImage, HumanStr.c_str(), cvPoint(StepLengthSelection, 30), &font, CV_RGB(255,255,255) );
  460. cvPutText( pImage, ComputerStr.c_str(), cvPoint(StepLengthSelection*3 + WidthButtonSelection*2, 30), &font, CV_RGB(255,255,255) );
  461. break;
  462. };
  463. };
  464. Buttons CurrentButton = GetButton(pBlackWhiteMHI, Mode, WidthButtonSelection, HeightButtonSelection,
  465. (Mode == Intro)?(StepLengthIntro):(StepLengthSelection), StepLengthSelection/2, pImage);
  466. bool InfoAuthentic = true;
  467. if (FrameCounter < AuthenticFrameThreshold)
  468. {
  469. InfoAuthentic = false;
  470. };
  471. for (int index = 0; (index < BufferSize)&&(InfoAuthentic); index ++)
  472. {
  473. if (BufferModes[index] != Mode)
  474. {
  475. InfoAuthentic = false;
  476. };
  477. if (BufferButtons[index] != CurrentButton)
  478. {
  479. InfoAuthentic = false;
  480. };
  481. };
  482. if (Mode == Intro)
  483. {
  484. if ((CurrentButton == Start) && InfoAuthentic)
  485. {
  486. Mode = MoveSelection;
  487. };
  488. if ((CurrentButton == SettingsButton) && InfoAuthentic)
  489. {
  490. Mode = Settings;
  491. };
  492. if ((CurrentButton == Exit) && InfoAuthentic)
  493. {
  494. TimeToStopCapture = true;
  495. };
  496. }
  497. else
  498. {
  499. if (Mode == MoveSelection)
  500. {
  501. if ((CurrentButton == Exit) && InfoAuthentic)
  502. {
  503. Mode = Intro;
  504. ComputerWon = 0;
  505. HumanWon = 0;
  506. SetRandomDistribution();
  507. GetCurrentDistribution(fRock, fPaper, fScissors);
  508. cvZero(pMHI);
  509. };
  510. if ((CurrentButton == RockButton) && InfoAuthentic)
  511. {
  512. HumanMove = Rock;
  513. };
  514. if ((CurrentButton == PaperButton) && InfoAuthentic)
  515. {
  516. HumanMove = Paper;
  517. };
  518. if ((CurrentButton == ScissorsButton) && InfoAuthentic)
  519. {
  520. HumanMove = Scissors;
  521. };
  522. if (((CurrentButton == RockButton)||(CurrentButton == PaperButton)
  523. ||(CurrentButton == ScissorsButton)) && InfoAuthentic)
  524. {
  525. Mode = MoveShowing;
  526. ComputerMove = GetNextMove(HumanMove);
  527. FrameToSave = FrameCounter;
  528. //Who won?
  529. if ((ComputerMove == HumanMove+1) || ((ComputerMove == Rock)&&(HumanMove == Scissors)))
  530. {
  531. ComputerWon++;
  532. };
  533. if ((HumanMove == ComputerMove+1) || ((HumanMove == Rock)&&(ComputerMove == Scissors)))
  534. {
  535. HumanWon++;
  536. };
  537. };
  538. }
  539. else
  540. if (Mode == Settings)
  541. {
  542. if ((CurrentButton == Exit) && InfoAuthentic)
  543. {
  544. Mode = Intro;
  545. ComputerWon = 0;
  546. HumanWon = 0;
  547. SetRandomDistribution();
  548. GetCurrentDistribution(fRock, fPaper, fScissors);
  549. cvZero(pMHI);
  550. RecreateImages = true;
  551. };
  552. if ((CurrentButton == InfoButton) && InfoAuthentic)
  553. {
  554. NeedToShowDistribution = !NeedToShowDistribution;
  555. RecreateImages = true;
  556. };
  557. if ((CurrentButton == SpeedButton) && InfoAuthentic)
  558. {
  559. BufferSize = (BufferSize-1+5)%MaxBufferSize + 1;
  560. cvZero(pMHI);
  561. RecreateImages = true;
  562. };
  563. if ((CurrentButton == ResolutionButton) && InfoAuthentic)
  564. {
  565. switch (Resolution)
  566. {
  567. case r640:
  568. Resolution = r800;
  569. break;
  570. case r800:
  571. Resolution = r1024;
  572. break;
  573. case r1024:
  574. Resolution = r640;
  575. break;
  576. };
  577. RecreateImages = true;
  578. };
  579. };
  580. };
  581. cvShowImage("RPS Game", pImage);
  582. //cvShowImage("RPS Game 2", pBlackWhiteMHI);
  583. LastIndex = (LastIndex + 1)%BufferSize;
  584. BufferModes[LastIndex] = Mode;
  585. BufferButtons[LastIndex] = CurrentButton;
  586. FrameCounter++;
  587. if (pImage)
  588. {
  589. cvReleaseImage( &pImage );
  590. };
  591. if (RecreateImages)
  592. {
  593. if (pMHI)
  594. {
  595. cvReleaseImage( &pMHI );
  596. pMHI = 0;
  597. };
  598. if (pBlackWhiteMHI)
  599. {
  600. cvReleaseImage( &pBlackWhiteMHI );
  601. pBlackWhiteMHI = 0;
  602. };
  603. if (pLastImage)
  604. {
  605. cvReleaseImage( &pLastImage);
  606. pLastImage = 0;
  607. };
  608. RecreateImages = false;
  609. };
  610. cvWaitKey(1);
  611. };
  612. if (pRock)
  613. {
  614. cvReleaseImage( &pRock );
  615. };
  616. if (pPaper)
  617. {
  618. cvReleaseImage( &pPaper );
  619. };
  620. if (pScissors)
  621. {
  622. cvReleaseImage( &pScissors );
  623. };
  624. if (pExit)
  625. {
  626. cvReleaseImage( &pExit );
  627. };
  628. if (pStart)
  629. {
  630. cvReleaseImage( &pStart );
  631. };
  632. if (pMHI)
  633. {
  634. cvReleaseImage( &pMHI );
  635. };
  636. if (pBlackWhiteMHI)
  637. {
  638. cvReleaseImage( &pBlackWhiteMHI );
  639. };
  640. if (pLastImage)
  641. {
  642. cvReleaseImage( &pLastImage );
  643. };
  644. if (BufferModes)
  645. {
  646. delete[] BufferModes;
  647. };
  648. if (BufferButtons)
  649. {
  650. delete[] BufferButtons;
  651. };
  652. if (pInfo)
  653. {
  654. cvReleaseImage( &pInfo);
  655. };
  656. if (pResolution)
  657. {
  658. cvReleaseImage(&pResolution);
  659. };
  660. if (pSpeed)
  661. {
  662. cvReleaseImage(&pSpeed);
  663. };
  664. };
  665. int main(int argc, char *argv[])
  666. {
  667. std::cout << "List of connected cameras:\n";
  668. int ncams = cvcamGetCamerasCount();
  669. int camera;
  670. for (camera = 0; camera < ncams; camera++)
  671. {
  672. CameraDescription CDesrp;
  673. cvcamGetProperty(camera, CVCAM_DESCRIPTION, &CDesrp);
  674. std::cout << camera << ". " <<CDesrp.DeviceDescription << "\n";
  675. };
  676. std::cout << "\nDefault camera is 0. \nIf you want to use another camera please run \nthe application with parameter \"camera index\".\n";
  677. std::cout << "\nTo exit the application please select exit icon\nin the visual menu.\n";
  678. std::cout << "\nThe application works correct when a folder \n..\\RPS\\pix exists.\n";
  679. int NumberOfCamera = 0;
  680. if (argc == 2)
  681. {
  682. NumberOfCamera = atoi(argv[1]);
  683. };
  684. cvNamedWindow( "RPS Game", 1 );
  685. //cvNamedWindow( "RPS Game 2", 1 );
  686. CvCapture* pCapture = cvCaptureFromCAM(NumberOfCamera);
  687. cvSetCaptureProperty(pCapture, CV_CAP_PROP_FRAME_WIDTH, 640 );
  688. cvSetCaptureProperty(pCapture, CV_CAP_PROP_FRAME_HEIGHT, 480);
  689. Process(pCapture);
  690. if (pCapture)
  691. {
  692. cvReleaseCapture(&pCapture);
  693. };
  694. return 0;
  695. };