Skip to content

Instantly share code, notes, and snippets.

@cancan101
Last active August 29, 2015 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cancan101/cb6a22142fc5fc4d149c to your computer and use it in GitHub Desktop.
Save cancan101/cb6a22142fc5fc4d149c to your computer and use it in GitHub Desktop.
Get Solid-Filled Image for State Map v1
def get_state_v1(state):
url = make_url(state)
IN = load_gif_url(url)
#Drop the text at the top
IN = IN[150:]
#Convert 3 color channels to 1
IN_bw = get_bw(IN)
#invert colors (per docs for findContour)
IMG = 255-IN_bw
# This seems to bre required for Massachusetts
kernel = np.ones((3,3),np.uint8)
IMG = cv2.dilate(IMG,kernel,iterations = 1)
out_img = IMG.copy()
contours, hierarchy = cv2.findContours(out_img, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
threshold = 0.02
img = 255*np.ones(IN.shape, dtype=np.uint8)
for i in xrange(len(contours)):
cnt = contours[i]
cnt_len = cv2.arcLength(cnt, True)
cc = cv2.approxPolyDP(cnt, threshold * cnt_len, True)
area = cv2.contourArea(cc)
if cnt_len > 50 and area > 500:
cv2.drawContours(img, contours, i, (0,0,0),thickness=cv2.cv.CV_FILLED)
return img
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment