{"id":39,"date":"2021-09-13T19:52:17","date_gmt":"2021-09-13T19:52:17","guid":{"rendered":"http:\/\/ouyangminwei.com\/?p=39"},"modified":"2021-10-04T15:03:32","modified_gmt":"2021-10-04T15:03:32","slug":"lyto-different-color-plug-in","status":"publish","type":"post","link":"https:\/\/ouyangminwei.com\/index.php\/2021\/09\/13\/lyto-different-color-plug-in\/","title":{"rendered":"Lyto-Different-Color Plug-in"},"content":{"rendered":"\n<figure class=\"wp-block-image size-large is-resized is-style-rounded\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/i.ytimg.com\/vi\/RK69F4A_XqY\/maxresdefault.jpg\" alt=\"\" width=\"628\" height=\"352\"\/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Lyto-Different-Color<\/strong> It&#8217;s basically a simple color recognition game, when I saw this game for the first time, I had the idea of using <strong>python<\/strong> and <strong>opencv <\/strong>to write this game assistant.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">so first we need python, If you don&#8217;t know how to download python, you can go to this page<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"http:\/\/ouyangminwei.com\/index.php\/2021\/09\/13\/how-to-download-python-in-windows\/\">http:\/\/ouyangminwei.com\/index.php\/2021\/09\/13\/how-to-download-python-in-windows\/<\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">We need <strong>opencv <\/strong>and <strong>pillow <\/strong>for image processing, <strong>numpy <\/strong>for array and <strong>mss <\/strong>for screenshot.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>pip install numpy\npip install opencv-python\npip install pillow\npip install mss==2.0.22<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Next, we need to get the game screen<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># select screen range\nmon = {'top': 179, 'left': 706, 'width': 1214-706, 'height': 1080-179}\n\nsct = mss()\nsct.get_pixels(mon)\n\nwhile True:\n    # transform the screen information to numpy array\n    img = np.array(Image.frombytes('RGB', (sct.width, sct.height), sct.image))\n\n    # show the screen via opencv\n    cv2.imshow('test', np.array(img))\n    if cv2.waitKey(25) &amp; 0xFF == ord('q'):\n        cv2.destroyAllWindows()\n        break<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">I want the program to find the ball in the screen first, and then get the color of the center of the ball, so that it can find the ball with a different color, there is a funcction in opencv, call HoughCircles, it can help us to find the circle in the image.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-style-rounded\"><img decoding=\"async\" src=\"https:\/\/pyimagesearch.com\/wp-content\/uploads\/2014\/07\/detect_circles_8circles.jpg\" alt=\"\"\/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">The usage is very simple, just throw the image into the function, well&#8230; and some parameters<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># transform the image into gray scale to speed up the calculation\ngray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)\n# Houghcircles\ncircles = cv2.HoughCircles(gray, cv2.HOUGH_GRADIENT,1.2,10)\n\n# circles will be something likes \n# &#91;(x1, y1, r1),(x1, y1, r1) ...]\n# where x1 y1 is the position of circle's center and r is the radius<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">with circle&#8217;s center, we can get correspondging color<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>color = &#91;]\n# loop every circle\nfor (x, y, r) in circles:\n    # store the circle's center color into 'colors'\n    colors.append(list(img&#91;y,x,:]))<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">I write a function to find the different one, (there must be a better way of writing, but I only use a very dumb way)<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def find_different(arr):\n    for num,i in enumerate(arr):\n        if (arr.count(i)==1):\n            return num\n    else:\n        return 0<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">At this point, we are done. All we need is to put them all together and draw the balls of different colors.<\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/OuYangMinOa\/96b280615a420b187d74daf485467d81.js\"><\/script>\n\n\n\n<p class=\"wp-block-paragraph\">Let&#8217;s take a look at the results<\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" title=\"using opencv to play Lyto Different Color\" width=\"640\" height=\"360\" src=\"https:\/\/www.youtube.com\/embed\/7O04rMp9vVE?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Github :<a href=\" https:\/\/github.com\/OuYangMinOa\/Lyto-Different-Col\"> <\/a><a href=\"https:\/\/github.com\/OuYangMinOa\/Lyto-Different-Col\" title=\"https:\/\/github.com\/OuYangMinOa\/Lyto-Different-Color\">https:\/\/github.com\/OuYangMinOa\/Lyto-Different-Color<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Lyto-Different-Color It&#8217;s basicall &hellip; <a href=\"https:\/\/ouyangminwei.com\/index.php\/2021\/09\/13\/lyto-different-color-plug-in\/\">\u95b1\u8b80\u5168\u6587 <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[1],"tags":[7,4,3],"post_format":[],"class_list":["post-39","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-en","tag-opencv","tag-python"],"_edit_lock":"1633359812:1","_edit_last":"1","_aioseo_title":"#post_title #separator_sa #site_title","_aioseo_description":"#post_excerpt","_aioseo_keywords":"","_aioseo_og_title":"","_aioseo_og_description":"","_aioseo_og_article_section":"","_aioseo_og_article_tags":"","_aioseo_twitter_title":"","_aioseo_twitter_description":"","_oembed_2544c1d0cb3503ab4c4d558c3b3c8873":"<iframe title=\"using opencv to play Lyto Different Color\" width=\"640\" height=\"360\" src=\"https:\/\/www.youtube.com\/embed\/7O04rMp9vVE?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen><\/iframe>","_oembed_time_2544c1d0cb3503ab4c4d558c3b3c8873":"1631611014","_oembed_99481806ecbe6ce4ee46f8588d320993":"{{unknown}}","_oembed_db663acf973e82e6d9d80df71945dfb8":"","_oembed_16cdfab488f57db73586f4286af2704f":"","_wp_old_slug":"","_links":{"self":[{"href":"https:\/\/ouyangminwei.com\/index.php\/wp-json\/wp\/v2\/posts\/39","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ouyangminwei.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ouyangminwei.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ouyangminwei.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ouyangminwei.com\/index.php\/wp-json\/wp\/v2\/comments?post=39"}],"version-history":[{"count":5,"href":"https:\/\/ouyangminwei.com\/index.php\/wp-json\/wp\/v2\/posts\/39\/revisions"}],"predecessor-version":[{"id":67,"href":"https:\/\/ouyangminwei.com\/index.php\/wp-json\/wp\/v2\/posts\/39\/revisions\/67"}],"wp:attachment":[{"href":"https:\/\/ouyangminwei.com\/index.php\/wp-json\/wp\/v2\/media?parent=39"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ouyangminwei.com\/index.php\/wp-json\/wp\/v2\/categories?post=39"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ouyangminwei.com\/index.php\/wp-json\/wp\/v2\/tags?post=39"},{"taxonomy":"post_format","embeddable":true,"href":"https:\/\/ouyangminwei.com\/index.php\/wp-json\/wp\/v2\/post_format?post=39"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}