{"id":2102,"date":"2016-08-08T10:55:13","date_gmt":"2016-08-08T14:55:13","guid":{"rendered":"https:\/\/blogs.mathworks.com\/steve\/?p=2102"},"modified":"2019-11-01T16:46:02","modified_gmt":"2019-11-01T20:46:02","slug":"pokemon-go-meets-matlab","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/steve\/2016\/08\/08\/pokemon-go-meets-matlab\/","title":{"rendered":"Pok\u00e9mon Go meets MATLAB"},"content":{"rendered":"<div class=\"content\"><p>Until about two weeks ago, I had given absolutely no thought to the possibility of a relationship between MATLAB and Pok&eacute;mon Go, this summer's worldwide mobile gaming phenomenon. But then I heard about someone using some basic image processing techniques to hack the game by automating the search for Pok&eacute;Stops.<\/p><p>Well, I don't play Pok&eacute;mon Go and I don't know what a Pok&eacute;Stop is, but I understood the basic idea behind the search automation.<\/p><p>Here is a screen shot from the game.<\/p><pre class=\"codeinput\">url = <span class=\"string\">'https:\/\/blogs.mathworks.com\/steve\/files\/pokemon-go-screen.jpg'<\/span>;\r\nrgb = imread(url);\r\nimshow(rgb)\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/steve\/files\/pokemon_go_01.png\" alt=\"\"> <p>(Thanks very much to the people who sent me screen shots, especially Haripriya and Chris.)<\/p><p>That geometric pattern of nested blue circles is a Pok&eacute;Stop. Our task is to find those. I'm going to show the basic outline of a solution with these steps:<\/p><p>1. Segment the image by color.<\/p><p>2. Clean up the segmentation using a morphological closing.<\/p><p>3. Compute the areas and centroids of the connected components in the segmentation.<\/p><p>4. Choose the largest object.<\/p><p>To get a quick idea of how to segment the image by color, I like to use the Color Thresholder, usually with the Lab color space. Here's a screen shot that shows how I have adjusted the a* and b* sliders to pick out the range of colors associated with the Pok&eacute;Stop.<\/p><p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/steve\/files\/color-thresholder-screen-shot.png\" alt=\"\"> <\/p><p>Here's the resulting mask that I saved from the <a href=\"https:\/\/www.mathworks.com\/help\/images\/ref\/colorthresholder-app.html\">Color Thresholder<\/a>.<\/p><pre class=\"codeinput\">imshow(BW)\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/steve\/files\/pokemon_go_02.png\" alt=\"\"> <p>Now let's use morphological closing to turn the Pok&eacute;Stop pattern into a single, connected blob.<\/p><pre class=\"codeinput\">BW2 = imclose(BW,strel(<span class=\"string\">'disk'<\/span>,20));\r\nimshow(BW2)\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/steve\/files\/pokemon_go_03.png\" alt=\"\"> <p>Next, use <a href=\"https:\/\/www.mathworks.com\/help\/images\/ref\/regionprops.html\"><tt>regionprops<\/tt><\/a> to find all the connected components and compute their areas and centroids. In recent versions of the Image Processing Toolbox, you can tell <tt>regionprops<\/tt> to return the result as a table, which makes the results easier to read.<\/p><pre class=\"codeinput\">t = regionprops(<span class=\"string\">'table'<\/span>,BW2,<span class=\"string\">'area'<\/span>,<span class=\"string\">'centroid'<\/span>)\r\n<\/pre><pre class=\"codeoutput\">\r\nt = \r\n\r\n    Area         Centroid    \r\n    _____    ________________\r\n\r\n     1157     37.43    665.14\r\n    13259    260.56    313.99\r\n        2       217      49.5\r\n        6       259      31.5\r\n        1       265        82\r\n        6     356.5    31.667\r\n       12     387.5      31.5\r\n\r\n<\/pre><p>Finally, identify the table row with the biggest area, and get the corresponding centroid.<\/p><pre class=\"codeinput\">[~,j] = max(t.Area);\r\nlocation = t.Centroid(j,:)\r\n<\/pre><pre class=\"codeoutput\">\r\nlocation =\r\n\r\n  260.5581  313.9893\r\n\r\n<\/pre><p>Let's see how well that worked by superimposing the location on the original image.<\/p><pre class=\"codeinput\">imshow(rgb)\r\nhold <span class=\"string\">on<\/span>\r\nplot(location(1),location(2),<span class=\"string\">'dy'<\/span>,<span class=\"string\">'MarkerSize'<\/span>,10,<span class=\"string\">'MarkerFaceColor'<\/span>,<span class=\"string\">'y'<\/span>)\r\nhold <span class=\"string\">off<\/span>\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/steve\/files\/pokemon_go_04.png\" alt=\"\"> <p>There you go.<\/p><p>Catch 'em all!<\/p><script language=\"JavaScript\"> <!-- \r\n    function grabCode_201f5d4adb1941158113b68b5bd905b7() {\r\n        \/\/ Remember the title so we can use it in the new page\r\n        title = document.title;\r\n\r\n        \/\/ Break up these strings so that their presence\r\n        \/\/ in the Javascript doesn't mess up the search for\r\n        \/\/ the MATLAB code.\r\n        t1='201f5d4adb1941158113b68b5bd905b7 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 201f5d4adb1941158113b68b5bd905b7';\r\n    \r\n        b=document.getElementsByTagName('body')[0];\r\n        i1=b.innerHTML.indexOf(t1)+t1.length;\r\n        i2=b.innerHTML.indexOf(t2);\r\n \r\n        code_string = b.innerHTML.substring(i1, i2);\r\n        code_string = code_string.replace(\/REPLACE_WITH_DASH_DASH\/g,'--');\r\n\r\n        \/\/ Use \/x3C\/g instead of the less-than character to avoid errors \r\n        \/\/ in the XML parser.\r\n        \/\/ Use '\\x26#60;' instead of '<' so that the XML parser\r\n        \/\/ doesn't go ahead and substitute the less-than character. \r\n        code_string = code_string.replace(\/\\x3C\/g, '\\x26#60;');\r\n\r\n        copyright = 'Copyright 2016 The MathWorks, Inc.';\r\n\r\n        w = window.open();\r\n        d = w.document;\r\n        d.write('<pre>\\n');\r\n        d.write(code_string);\r\n\r\n        \/\/ Add copyright line at the bottom if specified.\r\n        if (copyright.length > 0) {\r\n            d.writeln('');\r\n            d.writeln('%%');\r\n            if (copyright.length > 0) {\r\n                d.writeln('% _' + copyright + '_');\r\n            }\r\n        }\r\n\r\n        d.write('<\/pre>\\n');\r\n\r\n        d.title = title + ' (MATLAB code)';\r\n        d.close();\r\n    }   \r\n     --> <\/script><p style=\"text-align: right; font-size: xx-small; font-weight:lighter;   font-style: italic; color: gray\"><br><a href=\"javascript:grabCode_201f5d4adb1941158113b68b5bd905b7()\"><span style=\"font-size: x-small;        font-style: italic;\">Get \r\n      the MATLAB code <noscript>(requires JavaScript)<\/noscript><\/span><\/a><br><br>\r\n      Published with MATLAB&reg; R2016a<br><\/p><\/div><!--\r\n201f5d4adb1941158113b68b5bd905b7 ##### SOURCE BEGIN #####\r\n%%\r\n% Until about two weeks ago, I had given absolutely no thought to\r\n% the possibility of a relationship between MATLAB and Pok\u00c3\u00a9mon Go,\r\n% this summer's worldwide mobile gaming phenomenon. But then I heard\r\n% about someone using some basic image processing techniques to hack\r\n% the game by automating the search for Pok\u00c3\u00a9Stops.\r\n%\r\n% Well, I don't play Pok\u00c3\u00a9mon Go and I don't know what a Pok\u00c3\u00a9Stop\r\n% is, but I understood the basic idea behind the search automation.\r\n%\r\n% Here is a screen shot from the game.\r\n\r\nurl = 'https:\/\/blogs.mathworks.com\/steve\/files\/pokemon-go-screen.jpg';\r\nrgb = imread(url);\r\nimshow(rgb)\r\n\r\n%%\r\n% (Thanks very to the people who sent me screen shots, especially\r\n% Haripriya and Chris.)\r\n\r\n%%\r\n% That geometric pattern of nested blue circles is a Pok\u00c3\u00a9Stop. Our\r\n% task is to find those. I'm going to show the basic outline of a\r\n% solution with these steps:\r\n%\r\n% 1. Segment the image by color.\r\n%\r\n% 2. Clean up the segmentation using a morphological closing.\r\n%\r\n% 3. Compute the areas and centroids of the connected components in\r\n% the segmentation.\r\n%\r\n% 4. Choose the largest object.\r\n%\r\n% To get a quick idea of how to segment the image by color, I like\r\n% to use the Color Thresholder, usually with the Lab color space.\r\n% Here's a screen shot that shows how I have adjusted the a* and b*\r\n% sliders to pick out the range of colors associated with the\r\n% Pok\u00c3\u00a9Stop.\r\n%\r\n% <<https:\/\/blogs.mathworks.com\/steve\/files\/color-thresholder-screen-shot.png>>\r\n%\r\n% Here's the resulting mask that I saved from the\r\n% <https:\/\/www.mathworks.com\/help\/images\/ref\/colorthresholder-app.html\r\n% Color Thresholder>.\r\n\r\nimshow(BW)\r\n\r\n%%\r\n% Now let's use morphological closing to turn the Pok\u00c3\u00a9Stop pattern\r\n% into a single, connected blob.\r\n\r\nBW2 = imclose(BW,strel('disk',20));\r\nimshow(BW2)\r\n\r\n%%\r\n% Next, use\r\n% <https:\/\/www.mathworks.com\/help\/images\/ref\/regionprops.html\r\n% |regionprops|> to find all the connected components and compute\r\n% their areas and centroids. In recent versions of the Image\r\n% Processing Toolbox, you can tell |regionprops| to return the\r\n% result as a table, which makes the results easier to read.\r\n\r\nt = regionprops('table',BW2,'area','centroid')\r\n\r\n%%\r\n% Finally, identify the table row with the biggest area, and get the\r\n% corresponding centroid.\r\n\r\n[~,j] = max(t.Area);\r\nlocation = t.Centroid(j,:)\r\n\r\n%%\r\n% Let's see how well that worked by superimposing the location on\r\n% the original image.\r\n\r\nimshow(rgb)\r\nhold on\r\nplot(location(1),location(2),'dy','MarkerSize',10,'MarkerFaceColor','y')\r\nhold off\r\n\r\n%%\r\n% There you go.\r\n%\r\n% Catch 'em all!\r\n\r\n\r\n##### SOURCE END ##### 201f5d4adb1941158113b68b5bd905b7\r\n-->","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img decoding=\"async\"  class=\"img-responsive\" src=\"https:\/\/blogs.mathworks.com\/steve\/files\/pokemon_go_04.png\" onError=\"this.style.display ='none';\" \/><\/div><p>Until about two weeks ago, I had given absolutely no thought to the possibility of a relationship between MATLAB and Pok&eacute;mon Go, this summer's worldwide mobile gaming phenomenon. But then I... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/steve\/2016\/08\/08\/pokemon-go-meets-matlab\/\">read more >><\/a><\/p>","protected":false},"author":42,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[90,144,76,36,122,68,168,106],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/2102"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/users\/42"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/comments?post=2102"}],"version-history":[{"count":2,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/2102\/revisions"}],"predecessor-version":[{"id":2116,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/2102\/revisions\/2116"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/media?parent=2102"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/categories?post=2102"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/tags?post=2102"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}