Created
April 25, 2016 20:45
-
-
Save Seagor/058d088eab754a5d708249a0a918b32b to your computer and use it in GitHub Desktop.
This file contains 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 | |
from skimage.morphology import binary_opening | |
import pandas as pd | |
import matplotlib.pyplot as plt | |
import matplotlib | |
matplotlib.style.use('ggplot') | |
%matplotlib inline | |
def show_region(r): | |
print 'Region:', r | |
counts = [] | |
for i, image in enumerate(image_regions): | |
objects = found_objects[i][r] | |
print 'image:', i, 'Objects:', objects['count'] | |
counts.append(objects['count']) | |
vis = image[r].copy() | |
for cnt in objects['contours']: | |
x,y,w,h = cv2.boundingRect(cnt) | |
cv2.rectangle(vis,(x,y),(x+w,y+h),(255,0,0),1) | |
render_array(vis, width=6, height=6) | |
def find_objects(r, r_img, show=False): | |
vis = r_img.copy() | |
t_mask = saturation_mask( r_img, threshold=threshold ) | |
mask = region_masks[r].copy() | |
diff = mask - t_mask | |
diff[ diff < 255 ] = 0 | |
diff = binary_opening(diff).astype(np.uint8) | |
labels = measure.label(diff) | |
props = measure.regionprops(labels) | |
if show == True: | |
render_array(vis, r_img, width=10, height=10) | |
#render_array(r_img) | |
contours, hierarchy = cv2.findContours(diff.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE) | |
filter_contours = [] | |
areas = [] | |
for cnt in contours: | |
area = cv2.contourArea(cnt) | |
if area > 20.0: | |
areas.append(area) | |
filter_contours.append(cnt) | |
x,y,w,h = cv2.boundingRect(cnt) | |
cv2.rectangle(vis,(x,y),(x+w,y+h),(255,0,0),1) | |
#cv2.drawContours(vis, cnt, -1, (255,0,0), 2) | |
return filter_contours, areas | |
threshold = 170 | |
found_objects = [] | |
for i, regions in enumerate(image_regions): | |
region_objs = [] | |
for r, img in enumerate(regions): | |
contours, areas = find_objects(r, img.copy()) | |
cnt = len(contours) | |
region_objs.append({'areas': areas, 'contours': contours, 'count': cnt}) | |
found_objects.append(region_objs) | |
cnts = [[o['count'] for o in img] for img in found_objects] | |
df2 = pd.DataFrame(np.array(cnts).transpose(), columns=['2016', '2015-1', '2015-2', '2014','2013']) | |
df2.plot.bar(figsize=(15, 7), width=.5, ylim=(0,20), stacked=True) | |
plt.show() | |
show_region(3) | |
#c,a = find_objects(1, image_regions[4][1], show=True) | |
#c,a = find_objects(5, image_regions[4][5], show=True) | |
#c,a = find_objects(0, image_regions[3][0], show=True) | |
#time.sleep(1) | |
#show_region(3) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment