Created
June 22, 2020 11:59
-
-
Save otakbeku/143a1f849d3f0f15d2982a387bddd420 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import cv2 | |
| import matplotlib.pyplot as plt | |
| img = cv2.imread('/path/') | |
| img_blur = cv2.medianBlur(img[:,:,1], 5) # Using Green channel color | |
| plt.imshow(img_blur) | |
| _, test_img = cv2.threshold(img_blur, 160, 255, cv2.THRESH_BINARY) | |
| test_contour, _ = cv2.findContours(test_img.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE) | |
| largest_contour = max(test_contour, key=cv2.contourArea) | |
| contour_img = img.copy() | |
| cv2.drawContours(contour_img, largest_contour, -1, (0, 255, 0), 25) | |
| x,y,w,h = cv2.boundingRect(largest_contour) # Dari contour ke bounding box | |
| rect_img = img.copy() | |
| cv2.rectangle(rect_img, (x,y), (x+w,y+h), (250, 250, 0), 25) # Naruh bounding box | |
| crop_img = img[y:y+h, x:x+w] | |
| plt.imshow(contour_img) | |
| plt.imshow(rect_img) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment