Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import cv2
- import numpy as np
- img = cv2.imread('box.png')
- img = cv2.blur(img,(5,5))
- hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
- h,s,v = cv2.split(hsv)
- cv2.imwrite('box_h.png', cv2.applyColorMap(h, cv2.COLORMAP_JET))
- cv2.imwrite('box_s.png', cv2.applyColorMap(s, cv2.COLORMAP_JET))
- cv2.imwrite('box_v.png', cv2.applyColorMap(v, cv2.COLORMAP_JET))
- thresh0 = cv2.adaptiveThreshold(s,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY_INV,11,2)
- thresh1 = cv2.adaptiveThreshold(v,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY_INV,11,2)
- thresh2 = cv2.adaptiveThreshold(v,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY_INV,11,2)
- thresh = cv2.bitwise_or(thresh0, thresh1)
- cv2.imwrite('box_ht.png', thresh2)
- cv2.imwrite('box_st.png', thresh0)
- cv2.imwrite('box_vt.png', thresh1)
- kernel = np.ones((3, 3), np.uint8)
- thresh = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, kernel)
- thresh = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, kernel)
- mask = np.ones_like(v) * 255
- contours,heirarchy = cv2.findContours(thresh,cv2.RETR_LIST,cv2.CHAIN_APPROX_NONE)
- for contour in contours:
- hull = cv2.convexHull(contour)
- area = cv2.contourArea(contour)
- if area > 10.0:
- cv2.drawContours(mask,[hull],-1,0,-1)
- mask = cv2.morphologyEx(255 - mask, cv2.MORPH_ERODE, kernel, iterations=3)
- result = cv2.bitwise_and(img, cv2.merge([mask]*3))
- cv2.imwrite('box_masked.png', result)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement