Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.66 KB | None | 0 0
  1. #include "ViZDoom.h"
  2. #include <iostream>
  3. #include <vector>
  4. #include <cstdlib>
  5. #include <ctime>
  6. #include <chrono>
  7. #include <thread>
  8.  
  9. #include <opencv2/core/core.hpp>
  10. #include <opencv2/highgui/highgui.hpp>
  11. #include <opencv2/xfeatures2d.hpp>
  12. #include <opencv2/imgproc/imgproc.hpp>
  13. #include <opencv2/calib3d/calib3d.hpp>
  14. #include <opencv2/sfm.hpp>
  15. #include <opencv2/viz.hpp>
  16.  
  17.  
  18. void sleep(unsigned int time){
  19. std::this_thread::sleep_for(std::chrono::milliseconds(time));
  20. }
  21.  
  22. using namespace vizdoom;
  23. using namespace cv;
  24. using namespace std;
  25.  
  26. int main() {
  27.  
  28. std::cout << "\n\nBASIC EXAMPLE\n\n";
  29.  
  30.  
  31. Ptr < FeatureDetector > detector1 = ORB::create(100);
  32.  
  33. // Create DoomGame instance. It will run the game and communicate with you.
  34. DoomGame *game = new DoomGame();
  35.  
  36. // Sets path to vizdoom engine executive which will be spawned as a separate process. Default is "./vizdoom".
  37. game->setViZDoomPath("/home/projects/ViZDoom/bin/vizdoom");
  38.  
  39. // Sets path to doom2 iwad resource file which contains the actual doom game-> Default is "./doom2.wad".
  40. game->setDoomGamePath("/home/projects/ViZDoom/bin/freedoom2.wad");
  41. //game->setDoomGamePath("../../bin/doom2.wad"); // Not provided with environment due to licences.
  42.  
  43. // Sets path to additional resources iwad file which is basically your scenario iwad.
  44. // If not specified default doom2 maps will be used and it's pretty much useless... unless you want to play doom.
  45. game->setDoomScenarioPath("/home/projects/ViZDoom/scenarios/basic.wad");
  46.  
  47. // Set map to start (scenario .wad files can contain many maps).
  48. game->setDoomMap("map01");
  49.  
  50. // Sets resolution. Default is 320X240
  51. game->setScreenResolution(RES_640X480);
  52.  
  53. // Sets the screen buffer format. Not used here but now you can change it. Default is CRCGCB.
  54. game->setScreenFormat(RGB24);
  55.  
  56. // Sets other rendering options
  57. game->setRenderHud(false);
  58. game->setRenderMinimalHud(false); // If hud is enabled
  59. game->setRenderCrosshair(true);
  60. game->setRenderWeapon(false);
  61. game->setRenderDecals(false);
  62. game->setRenderParticles(false);
  63. game->setRenderEffectsSprites(false);
  64. game->setRenderMessages(false);
  65. game->setRenderCorpses(false);
  66.  
  67. // Adds buttons that will be allowed.
  68. game->addAvailableButton(MOVE_LEFT);
  69. game->addAvailableButton(MOVE_RIGHT);
  70. game->addAvailableButton(ATTACK);
  71.  
  72. // Adds game variables that will be included in state.
  73. game->addAvailableGameVariable(AMMO2);
  74.  
  75. // Causes episodes to finish after 200 tics (actions)
  76. game->setEpisodeTimeout(200);
  77.  
  78. // Makes episodes start after 10 tics (~after raising the weapon)
  79. game->setEpisodeStartTime(10);
  80.  
  81. // Makes the window appear (turned on by default)
  82. game->setWindowVisible(true);
  83.  
  84. // Turns on the sound. (turned off by default)
  85. game->setSoundEnabled(false);
  86.  
  87. // Sets ViZDoom mode (PLAYER, ASYNC_PLAYER, SPECTATOR, ASYNC_SPECTATOR, PLAYER mode is default)
  88. game->setMode(PLAYER);
  89.  
  90. // Enables engine output to console.
  91. //game->setConsoleEnabled(true);
  92.  
  93. // Initialize the game. Further configuration won't take any effect from now on.
  94. game->init();
  95.  
  96.  
  97. // Define some actions. Each list entry corresponds to declared buttons:
  98. // MOVE_LEFT, MOVE_RIGHT, ATTACK
  99. // game.getAvailableButtonsSize() can be used to check the number of available buttons.
  100. // more combinations are naturally possible but only 3 are included for transparency when watching.
  101. std::vector<double> actions[3];
  102. actions[0] = {1, 0, 0};
  103. actions[1] = {0, 1, 0};
  104. actions[2] = {0, 0, 1};
  105.  
  106. std::srand(time(0));
  107.  
  108. // Run this many episodes
  109. int episodes = 10;
  110.  
  111. // Sets time that will pause the engine after each action.
  112. // Without this everything would go too fast for you to keep track of what's happening.
  113. unsigned int sleepTime = 1000 / DEFAULT_TICRATE; // = 28
  114.  
  115. Mat prev, next(480, 640, CV_8UC3);
  116. namedWindow( "Display window", WINDOW_AUTOSIZE );
  117.  
  118. for (int i = 0; i < episodes; ++i) {
  119.  
  120. std::cout << "Episode #" << i + 1 << "\n";
  121.  
  122. // Starts a new episode. It is not needed right after init() but it doesn't cost much and the loop is nicer.
  123. game->newEpisode();
  124.  
  125. while (!game->isEpisodeFinished()) {
  126.  
  127. // Get the state
  128. GameStatePtr state = game->getState(); // GameStatePtr is std::shared_ptr<GameState>
  129.  
  130. // Which consists of:
  131. unsigned int n = state->number;
  132. std::vector<double> vars = state->gameVariables;
  133. BufferPtr screenBuf = state->screenBuffer;
  134. BufferPtr depthBuf = state->depthBuffer;
  135. BufferPtr labelsBuf = state->labelsBuffer;
  136. BufferPtr automapBuf = state->automapBuffer;
  137. // BufferPtr is std::shared_ptr<Buffer> where Buffer is std::vector<uint8_t>
  138.  
  139. std::vector<Label> labels = state->labels;
  140.  
  141. ///opencv block begins
  142. {
  143. /// reading image from buffer into matrix
  144.  
  145. // IMPORTANT: OpenCV uses BGR model to represent colors, not RGB
  146.  
  147. for (int k = 0; k < next.rows; ++k) {
  148. for (int j = 0; j < next.cols; ++j) {
  149. auto vectorCoord = 3 * (k * next.cols + j);
  150. // Mapping from image coordinates to coordinates in vector (array)
  151.  
  152. next.at<uchar>(k, 3 * j) = (*screenBuf)[vectorCoord + 2];
  153. next.at<uchar>(k, 3 * j + 1) = (*screenBuf)[vectorCoord + 1];
  154. next.at<uchar>(k, 3 * j + 2) = (*screenBuf)[vectorCoord];
  155. }
  156. }
  157.  
  158. if (!prev.empty()) {
  159.  
  160. /// feature detection and matching
  161.  
  162. vector<KeyPoint> keypoints1, keypoints2;
  163.  
  164. Ptr<FeatureDetector> detector = ORB::create();
  165. detector->detect(prev, keypoints1);
  166. detector->detect(next, keypoints2);
  167.  
  168. Mat descriptor1, descriptor2;
  169.  
  170. Ptr<DescriptorExtractor> extractor = ORB::create();
  171. extractor->compute(prev, keypoints1, descriptor1);
  172. extractor->compute(next, keypoints2, descriptor2);
  173.  
  174.  
  175. vector<DMatch> matches;
  176. BFMatcher matcher(NORM_L2, true);
  177. matcher.match(descriptor1, descriptor2, matches);
  178.  
  179. vector<DMatch> good_matches;
  180. vector<Point2f> featurePoints1;
  181. vector<Point2f> featurePoints2;
  182.  
  183. for (DMatch match: matches) {
  184. good_matches.push_back(match);
  185. }
  186.  
  187.  
  188. Mat result;
  189.  
  190. //Draw keypoints only: drawKeypoints(next, keypoints2, result);
  191. drawMatches(prev, keypoints1, next, keypoints2, good_matches, result);
  192.  
  193. /// pixel-wise comparison
  194.  
  195. Mat diffImage;
  196. absdiff(prev, next, diffImage);
  197.  
  198. Mat foregroundMask = Mat::zeros(diffImage.rows, diffImage.cols, CV_8UC3);
  199.  
  200. float threshold = 20.0f; // 200.0f works
  201. float dist;
  202.  
  203. for (int k = 0; k < diffImage.rows; ++k) {
  204. for (int j = 0; j < diffImage.cols; ++j) {
  205. cv::Vec3b pix = diffImage.at<cv::Vec3b>(k, j);
  206.  
  207. dist = sqrt(pix[0] * pix[0] + pix[1] * pix[1] + pix[2] * pix[2]);
  208.  
  209. if (dist > threshold) {
  210. foregroundMask.at<unsigned char>(k, 3 * j) = 255;
  211. foregroundMask.at<unsigned char>(k, 3 * j + 1) = 255;
  212. foregroundMask.at<unsigned char>(k, 3 * j + 2) = 255;
  213. }
  214. }
  215. }
  216.  
  217. /// finding clusters
  218.  
  219. /*Mat img;
  220.  
  221. cv::cvtColor(foregroundMask.clone(), img, CV_BGR2GRAY);
  222.  
  223. // Get all non black points
  224. vector<Point> pts;
  225. findNonZero(img, pts);
  226.  
  227. // Define the distance between clusters
  228. int euclidean_distance = 40;
  229.  
  230. // Apply partition
  231. // All pixels within the the given distance will belong to the same cluster
  232.  
  233. vector<int> labels;
  234.  
  235. // With lambda function
  236. int th2 = euclidean_distance * euclidean_distance;
  237. int n_labels = cv::partition(
  238. pts,
  239. labels,
  240. [th2](const Point& lhs, const Point& rhs)
  241. {
  242. return ((lhs.x - rhs.x)*(lhs.x - rhs.x) + (lhs.y - rhs.y)*(lhs.y - rhs.y)) < th2;
  243. }
  244. );
  245.  
  246.  
  247. // Store all points in same cluster, and compute centroids
  248. vector<vector<Point>> clusters(n_labels);
  249. vector<Point> centroids(n_labels, Point(0,0));
  250.  
  251. for (int i = 0; i < pts.size(); ++i)
  252. {
  253. clusters[labels[i]].push_back(pts[i]);
  254. centroids[labels[i]] += pts[i];
  255. }
  256. for (int i = 0; i < n_labels; ++i)
  257. {
  258. centroids[i].x /= clusters[i].size();
  259. centroids[i].y /= clusters[i].size();
  260. }
  261.  
  262. // Draw results
  263.  
  264. // Build a vector of random color, one for each class (label)
  265. vector<Vec3b> colors;
  266. for (int i = 0; i < n_labels; ++i)
  267. {
  268. colors.push_back(Vec3b(rand() & 255, rand() & 255, rand() & 255));
  269. }
  270.  
  271. // Draw the points
  272. Mat3b res(img.rows, img.cols, Vec3b(0, 0, 0));
  273. for (int i = 0; i < pts.size(); ++i)
  274. {
  275. res(pts[i]) = colors[labels[i]];
  276. }
  277.  
  278. // Draw centroids
  279. for (int i = 0; i < n_labels; ++i)
  280. {
  281. circle(res, centroids[i], 3, Scalar(colors[i][0], colors[i][1], colors[i][2]), CV_FILLED);
  282. circle(res, centroids[i], 6, Scalar(255 - colors[i][0], 255 - colors[i][1], 255 - colors[i][2]));
  283.  
  284. }
  285.  
  286.  
  287. if (n_labels == 1)
  288. {
  289. auto a = centroids[0].x;
  290. auto b = centroids[0].y;
  291.  
  292. cout << a << " " << b;
  293. }*/
  294.  
  295.  
  296. imshow("Display window", foregroundMask);
  297. waitKey(40);
  298. }
  299.  
  300. prev = next.clone();
  301. }
  302. /// opencv block ends
  303.  
  304.  
  305.  
  306. // Make random action and get reward
  307. double reward = game->makeAction(actions[std::rand() % game->getAvailableButtonsSize()]);
  308.  
  309. if(sleepTime) sleep(sleepTime);
  310. }
  311.  
  312. std::cout << "Episode finished.\n";
  313. std::cout << "Total reward: " << game->getTotalReward() << "\n";
  314. std::cout << "************************\n";
  315.  
  316. }
  317.  
  318. // It will be done automatically in destructor but after close You can init it again with different settings.
  319. game->close();
  320. delete game;
  321. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement