

{"id":126090,"date":"2024-04-08T18:00:49","date_gmt":"2024-04-08T12:30:49","guid":{"rendered":"https:\/\/data-flair.training\/blogs\/?p=126090"},"modified":"2026-06-01T12:30:17","modified_gmt":"2026-06-01T07:00:17","slug":"lego-brick-finder-using-opencv","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/lego-brick-finder-using-opencv\/","title":{"rendered":"OpenCV Project &#8211; LEGO Brick Finder"},"content":{"rendered":"<div class='__iawmlf-post-loop-links' style='display:none;' data-iawmlf-post-links='[{&quot;id&quot;:2541,&quot;href&quot;:&quot;https:\\\/\\\/drive.google.com\\\/file\\\/d\\\/1A1_URgY6tf-jusT5a7vDNhPTJ5VdyGmg\\\/view?usp=drive_link&quot;,&quot;archived_href&quot;:&quot;http:\\\/\\\/web-wp.archive.org\\\/web\\\/20260601070041\\\/https:\\\/\\\/drive.google.com\\\/file\\\/d\\\/1A1_URgY6tf-jusT5a7vDNhPTJ5VdyGmg\\\/view?usp=drive_link&quot;,&quot;redirect_href&quot;:&quot;&quot;,&quot;checks&quot;:[{&quot;date&quot;:&quot;2026-06-01 18:53:43&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-06-05 10:04:42&quot;,&quot;http_code&quot;:200}],&quot;broken&quot;:false,&quot;last_checked&quot;:{&quot;date&quot;:&quot;2026-06-05 10:04:42&quot;,&quot;http_code&quot;:200},&quot;process&quot;:&quot;done&quot;}]'><\/div>\n<p>The LEGO brick finder is an innovative computer vision application designed to identify and locate LEGO bricks within images or real-world scenes. Using advanced color-based detection and image processing techniques, it can distinguish various LEGO brick colors and shapes, aiding in the quick and efficient detection of LEGO pieces in diverse contexts. Whether for sorting, building, or inventory management, this tool simplifies the task of pinpointing LEGO bricks of interest.<\/p>\n<h2>Prerequisites For OpenCV Lego Brick Finder<\/h2>\n<p>To utilize the LEGO brick finder, a solid grasp of Python and OpenCV is essential, along with the following system requirements.<\/p>\n<p>1. Python 3.7 and above<br \/>\n2. Any Python editor (VS code, Pycharm, etc.)<\/p>\n<h3>Download OpenCV Lego Brick Finder Project<\/h3>\n<p>Please download the source code of OpenCV Lego Brick Finder Project from the following link: <a href=\"https:\/\/drive.google.com\/file\/d\/1A1_URgY6tf-jusT5a7vDNhPTJ5VdyGmg\/view?usp=drive_link\"><strong>OpenCV Lego Brick Finder Project Code.<\/strong><\/a><\/p>\n<h3>Installation<\/h3>\n<p>Open windows cmd as administrator<\/p>\n<p>1. Install OpenCV library.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">pip install opencv-python<\/pre>\n<h3>Let\u2019s Implement It<\/h3>\n<p>1. Import all the necessary packages<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">import cv2\r\nimport numpy as np<\/pre>\n<p>2. It reads an image and resizes it to specified width. Next, It converts it to the HSV color space for further image processing and analysis.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">img = cv2.imread('img.jpg')\r\nwidth = 600\r\nscale = width \/ img.shape[1]\r\nimg = cv2.resize(img, None, fx=scale, fy=scale)\r\nhsv_img = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)<\/pre>\n<p>3. It defines HSV color ranges for different colored LEGO brick detection.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">range_color = {\r\n    'red': [(0, 100, 100), (10, 255, 255)],\r\n    'dark_green': [(30, 80, 80), (75, 255, 255)], \r\n    'yellow': [(20, 100, 100), (30, 255, 255)],\r\n    'blue': [(90, 100, 100), (140, 255, 255)], \r\n}<\/pre>\n<p>4. It creates an empty mask and for each defined color range, it generates masks based on HSV values, combining them into \u2018mask_comb\u2019.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">mask_comb = np.zeros(img.shape[:2], dtype=np.uint8)\r\n\r\nfor color, (lower, upper) in range_color.items():\r\n    mask = cv2.inRange(hsv_img, np.array(lower), np.array(upper))\r\n    mask_comb |= mask<\/pre>\n<p>5. It finds contours in the combined mask, filtering them based on a minimum area of 500 pixels.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">contours, _ = cv2.findContours(mask_comb, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)\r\nmin_area = 500\r\ncontour_filter = [c for c in contours if cv2.contourArea(c) &gt; min_area]<\/pre>\n<p>6. It iterates through the filtered contours and calculates the bounding rectangles. Next, it draws a green rectangle around the detected LEGO bricks from the image.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">for contour in contour_filter:\r\n    x, y, w, h = cv2.boundingRect(contour)\r\n    cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 2)<\/pre>\n<p>7. It displays the modified image with the detected LEGO bricks using OpenCV. It waits for a key press and then closes the image window.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">cv2.imshow('DataFlair', img)\r\ncv2.waitKey(0)\r\ncv2.destroyAllWindows()<\/pre>\n<h3>OpenCV LEGO Brick Finder Output<\/h3>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/11\/opencv-lego-brick-finder.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-132242 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/11\/opencv-lego-brick-finder.webp\" alt=\"opencv lego brick finder\" width=\"896\" height=\"616\" \/><\/a><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/12\/opencv-lego-brick-finder-output.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-132243 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/12\/opencv-lego-brick-finder-output.webp\" alt=\"opencv lego brick finder output\" width=\"896\" height=\"632\" \/><\/a><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/12\/lego-brick-finder.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-132244 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/12\/lego-brick-finder.webp\" alt=\"lego brick finder\" width=\"904\" height=\"704\" \/><\/a><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/12\/lego-brick-finder-opencv-output.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-132245 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/12\/lego-brick-finder-opencv-output.webp\" alt=\"lego brick finder opencv output\" width=\"900\" height=\"880\" \/><\/a><\/p>\n<h3>Conclusion<\/h3>\n<p>In conclusion, the Lego Brick Finder project has demonstrated the successful integration of technology into the realm of play and creativity. This project combines computer vision and machine learning to effortlessly identify and locate specific Lego bricks within vast collections.<\/p>\n<p>This innovative solution streamlines the building process, making it more accessible and enjoyable for enthusiasts of all ages and reaffirming the timeless appeal of Lego in the modern era.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The LEGO brick finder is an innovative computer vision application designed to identify and locate LEGO bricks within images or real-world scenes. Using advanced color-based detection and image processing techniques, it can distinguish various&#46;&#46;&#46;<\/p>\n","protected":false},"author":86671,"featured_media":132247,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[27755],"tags":[30179,30177,30175,30178,30176,21719,30129,30121,30118],"class_list":["post-126090","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-opencv-tutorials","tag-lego-brick-finder","tag-lego-brick-finder-project","tag-lego-brick-finder-using-opencv","tag-opencv-lego-brick-finder","tag-opencv-lego-brick-finder-project","tag-opencv-projects","tag-opencv-projects-for-practice","tag-opencv-projects-ideas","tag-python-opencv-projects"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>OpenCV Project - LEGO Brick Finder - DataFlair<\/title>\n<meta name=\"description\" content=\"This project combines computer vision and machine learning for effortlessly identifying and locating specific lego bricks within vast collections.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/data-flair.training\/blogs\/lego-brick-finder-using-opencv\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"OpenCV Project - LEGO Brick Finder - DataFlair\" \/>\n<meta property=\"og:description\" content=\"This project combines computer vision and machine learning for effortlessly identifying and locating specific lego bricks within vast collections.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/lego-brick-finder-using-opencv\/\" \/>\n<meta property=\"og:site_name\" content=\"DataFlair\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/DataFlairWS\/\" \/>\n<meta property=\"article:published_time\" content=\"2024-04-08T12:30:49+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-06-01T07:00:17+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/12\/lego-brick-finder-opencv.webp\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/webp\" \/>\n<meta name=\"author\" content=\"TechVidvan Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:site\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"TechVidvan Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"OpenCV Project - LEGO Brick Finder - DataFlair","description":"This project combines computer vision and machine learning for effortlessly identifying and locating specific lego bricks within vast collections.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/data-flair.training\/blogs\/lego-brick-finder-using-opencv\/","og_locale":"en_US","og_type":"article","og_title":"OpenCV Project - LEGO Brick Finder - DataFlair","og_description":"This project combines computer vision and machine learning for effortlessly identifying and locating specific lego bricks within vast collections.","og_url":"https:\/\/data-flair.training\/blogs\/lego-brick-finder-using-opencv\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2024-04-08T12:30:49+00:00","article_modified_time":"2026-06-01T07:00:17+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/12\/lego-brick-finder-opencv.webp","type":"image\/webp"}],"author":"TechVidvan Team","twitter_card":"summary_large_image","twitter_creator":"@DataFlairWS","twitter_site":"@DataFlairWS","twitter_misc":{"Written by":"TechVidvan Team","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/data-flair.training\/blogs\/lego-brick-finder-using-opencv\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/lego-brick-finder-using-opencv\/"},"author":{"name":"TechVidvan Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/0e594f928e31fc96628ac40f6ae74f49"},"headline":"OpenCV Project &#8211; LEGO Brick Finder","datePublished":"2024-04-08T12:30:49+00:00","dateModified":"2026-06-01T07:00:17+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/lego-brick-finder-using-opencv\/"},"wordCount":357,"commentCount":0,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/lego-brick-finder-using-opencv\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/12\/lego-brick-finder-opencv.webp","keywords":["lego brick finder","lego brick finder project","lego brick finder using opencv","opencv lego brick finder","opencv lego brick finder project","opencv projects","opencv projects for practice","opencv projects ideas","python opencv projects"],"articleSection":["OpenCV Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/lego-brick-finder-using-opencv\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/lego-brick-finder-using-opencv\/","url":"https:\/\/data-flair.training\/blogs\/lego-brick-finder-using-opencv\/","name":"OpenCV Project - LEGO Brick Finder - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"primaryImageOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/lego-brick-finder-using-opencv\/#primaryimage"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/lego-brick-finder-using-opencv\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/12\/lego-brick-finder-opencv.webp","datePublished":"2024-04-08T12:30:49+00:00","dateModified":"2026-06-01T07:00:17+00:00","description":"This project combines computer vision and machine learning for effortlessly identifying and locating specific lego bricks within vast collections.","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/lego-brick-finder-using-opencv\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/lego-brick-finder-using-opencv\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/lego-brick-finder-using-opencv\/#primaryimage","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/12\/lego-brick-finder-opencv.webp","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/12\/lego-brick-finder-opencv.webp","width":1200,"height":628,"caption":"lego brick finder opencv"},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/lego-brick-finder-using-opencv\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog Home","item":"https:\/\/data-flair.training\/blogs\/"},{"@type":"ListItem","position":2,"name":"OpenCV Tutorials","item":"https:\/\/data-flair.training\/blogs\/category\/opencv-tutorials\/"},{"@type":"ListItem","position":3,"name":"OpenCV Project &#8211; LEGO Brick Finder"}]},{"@type":"WebSite","@id":"https:\/\/data-flair.training\/blogs\/#website","url":"https:\/\/data-flair.training\/blogs\/","name":"DataFlair","description":"Learn Today. Lead Tomorrow.","publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/data-flair.training\/blogs\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/data-flair.training\/blogs\/#organization","name":"DataFlair","url":"https:\/\/data-flair.training\/blogs\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","width":106,"height":48,"caption":"DataFlair"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/DataFlairWS\/","https:\/\/x.com\/DataFlairWS","https:\/\/www.linkedin.com\/company\/dataflair-web-services-pvt-ltd\/","https:\/\/www.youtube.com\/user\/DataFlairWS"]},{"@type":"Person","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/0e594f928e31fc96628ac40f6ae74f49","name":"TechVidvan Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/c89190da3d4010c71ba476b618ab10fdc2335c82cdfa0ad5002d98d0f2473444?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/c89190da3d4010c71ba476b618ab10fdc2335c82cdfa0ad5002d98d0f2473444?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/c89190da3d4010c71ba476b618ab10fdc2335c82cdfa0ad5002d98d0f2473444?s=96&d=mm&r=g","caption":"TechVidvan Team"},"description":"TechVidvan Team provides high-quality content &amp; courses on AI, ML, Data Science, Data Engineering, Data Analytics, programming, Python, DSA, Android, Flutter, full stack web dev, MERN, and many latest technology.","url":"https:\/\/data-flair.training\/blogs\/author\/test001\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/126090","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/users\/86671"}],"replies":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/comments?post=126090"}],"version-history":[{"count":7,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/126090\/revisions"}],"predecessor-version":[{"id":148615,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/126090\/revisions\/148615"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media\/132247"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=126090"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=126090"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=126090"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}