├── Readme.md ├── Readme.txt ├── crop_objects.py ├── cropped ├── 1.png ├── 2.png ├── 3.png ├── 4.png ├── 5.png ├── 6.png ├── 7.png └── 8.png ├── images ├── code.png └── output.png └── sample_image.jpg /Readme.md: -------------------------------------------------------------------------------- 1 | ### Find Contours and Crop Objects 2 | 3 | Hey there! today i assembled this quick ready to use code which can help ease the problem of sorting objects from images this project is very exciting, you just fetch the program with an image containing lot of objects with different shapes and it will automatically find them, crop them and save them separately for you. 4 | 5 | #### Required Module 6 | 7 | ````python 8 | >> pip install opencv-contrib-python 9 | ```` 10 | 11 | #### How to Run 12 | 13 | Just run the ``crop_objects.py`` and see the magic, It will pickup ``sample_image.jpg`` will find the objects such as book,cup, badges and save them separately after cropping in a ``cropped`` directory. 14 | 15 | #### Illustrations 16 | 17 | > Input Image ``sample_image.jpg`` 18 | > 19 | > ![](sample_image.jpg) 20 | 21 | 22 | 23 | #### Cropped Objects 24 | 25 | > 1.png 26 | > 27 | > ![](cropped/1.png) 28 | > 29 | > 2.png 30 | > 31 | > ![](cropped/2.png) 32 | > 33 | > 3.png 34 | > 35 | > ![](cropped/3.png) 36 | > 37 | > 4.png 38 | > 39 | > ![](cropped/4.png) 40 | > 41 | > 5.png 42 | > 43 | > ![](cropped/5.png) 44 | > 45 | > 6.png 46 | > 47 | > ![](cropped/6.png) 48 | > 49 | > 7.png 50 | > 51 | > ![](cropped/7.png) 52 | > 53 | > 8.png 54 | > 55 | > ![](cropped/8.png) -------------------------------------------------------------------------------- /Readme.txt: -------------------------------------------------------------------------------- 1 | Hey there! 2 | Today i assembled this quick ready to use code which can help ease the problem of sorting objects from images 3 | this project is very exciting, 4 | you just fetch the program with an image containing lot of objects with different shapes and it will 5 | automatially find them, crop them and save them separately for you. 6 | 7 | #======= Required Modules ========= 8 | pip install cv3 9 | #================================== 10 | 11 | >> Just run the "find_and_crop_objects.py" and see the magic 12 | >> It will pickup "the sample_image.jpg" will find the objects such as book,cup, badges 13 | and save them separately after croping in a separate directory 14 | -------------------------------------------------------------------------------- /crop_objects.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | 3 | #reading image 4 | image = cv2.imread("sample_image.jpg") 5 | 6 | #converting to gray scale 7 | gray=cv2.cvtColor(image,cv2.COLOR_BGR2GRAY) 8 | 9 | #applying canny edge detection 10 | edged = cv2.Canny(image, 10, 250) 11 | 12 | #finding contours 13 | (_, cnts, _) = cv2.findContours(edged.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) 14 | idx = 0 15 | for c in cnts: 16 | x,y,w,h = cv2.boundingRect(c) 17 | if w>50 and h>50: 18 | idx+=1 19 | new_img=image[y:y+h,x:x+w] 20 | #cropping images 21 | cv2.imwrite("cropped/"+str(idx) + '.png', new_img) 22 | #cv2.imshow("Original Image",image) 23 | #cv2.imshow("Canny Edge",edged) 24 | #cv2.waitKey(0) 25 | print('>> Objects Cropped Successfully!') 26 | print(">> Check out 'cropped' Directory") 27 | -------------------------------------------------------------------------------- /cropped/1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/imneonizer/Find-and-crop-objects-From-images-using-OpenCV-and-Python/1bf0a46a90d1c47076f61ce0966a7535c285f2eb/cropped/1.png -------------------------------------------------------------------------------- /cropped/2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/imneonizer/Find-and-crop-objects-From-images-using-OpenCV-and-Python/1bf0a46a90d1c47076f61ce0966a7535c285f2eb/cropped/2.png -------------------------------------------------------------------------------- /cropped/3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/imneonizer/Find-and-crop-objects-From-images-using-OpenCV-and-Python/1bf0a46a90d1c47076f61ce0966a7535c285f2eb/cropped/3.png -------------------------------------------------------------------------------- /cropped/4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/imneonizer/Find-and-crop-objects-From-images-using-OpenCV-and-Python/1bf0a46a90d1c47076f61ce0966a7535c285f2eb/cropped/4.png -------------------------------------------------------------------------------- /cropped/5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/imneonizer/Find-and-crop-objects-From-images-using-OpenCV-and-Python/1bf0a46a90d1c47076f61ce0966a7535c285f2eb/cropped/5.png -------------------------------------------------------------------------------- /cropped/6.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/imneonizer/Find-and-crop-objects-From-images-using-OpenCV-and-Python/1bf0a46a90d1c47076f61ce0966a7535c285f2eb/cropped/6.png -------------------------------------------------------------------------------- /cropped/7.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/imneonizer/Find-and-crop-objects-From-images-using-OpenCV-and-Python/1bf0a46a90d1c47076f61ce0966a7535c285f2eb/cropped/7.png -------------------------------------------------------------------------------- /cropped/8.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/imneonizer/Find-and-crop-objects-From-images-using-OpenCV-and-Python/1bf0a46a90d1c47076f61ce0966a7535c285f2eb/cropped/8.png -------------------------------------------------------------------------------- /images/code.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/imneonizer/Find-and-crop-objects-From-images-using-OpenCV-and-Python/1bf0a46a90d1c47076f61ce0966a7535c285f2eb/images/code.png -------------------------------------------------------------------------------- /images/output.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/imneonizer/Find-and-crop-objects-From-images-using-OpenCV-and-Python/1bf0a46a90d1c47076f61ce0966a7535c285f2eb/images/output.png -------------------------------------------------------------------------------- /sample_image.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/imneonizer/Find-and-crop-objects-From-images-using-OpenCV-and-Python/1bf0a46a90d1c47076f61ce0966a7535c285f2eb/sample_image.jpg --------------------------------------------------------------------------------