Skip to content

Instantly share code, notes, and snippets.

@hoyeunglee
Created August 3, 2017 01:57
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 hoyeunglee/f371f66d55f90dda043f7e7fea38ffa2 to your computer and use it in GitHub Desktop.
Save hoyeunglee/f371f66d55f90dda043f7e7fea38ffa2 to your computer and use it in GitHub Desktop.
from PIL import Image
from functools import partial
import sys
import numpy as np
import cv2
im = cv2.imread(r'C:\Users\mas\Documents\roster.png')
out = np.zeros(im.shape,np.uint8)
gray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
thresh = cv2.adaptiveThreshold(gray,255,1,1,11,2)
m5 = cv2.imread(r'C:\Users\mas\Documents\roster.png')
im = cv2.imread(r'C:\Users\mas\Documents\roster.png')
im3 = im.copy()
ma = Image.open("roster.png")
color1 = ma.load()
print ma.size
print color1[1,1]
color1 = ma.load()
print ma.size
print color1[1,1]
colortolocation = {}
def addtogroupkey(keyandmemory, key1, memorycontent):
k = key1
if k in keyandmemory:
keyandmemory[k].append(memorycontent)
else:
keyandmemory[k] = [memorycontent]
return keyandmemory
for ii in range(0, ma.size[0]):
for jj in range(0, ma.size[1]):
colortolocation = addtogroupkey(colortolocation, color1[ii,jj], (ii,jj))
def isneighborlocation(lo1, lo2):
if abs(lo1[0] - lo2[0]) == 1 and lo1[1] == lo2[1]:
return 1
elif abs(lo1[1] - lo2[1]) == 1 and lo1[0] == lo2[0]:
return 1
else:
return 0
#for eachcolor in colortolocation:
if 1 == 1:
eachcolor = (0,0,0)
testing1 = list(colortolocation[eachcolor])
testing1.sort(key=lambda x: x[0])
locations = testing1
locationsgroup = {}
continueconnect = 0
for ii in range(0,len(locations)-1):
if isneighborlocation(locations[ii], locations[ii+1]) == 1:
if continueconnect == 0:
keyone = len(locationsgroup)+1
if keyone in locationsgroup:
if locations[ii] not in locationsgroup[keyone]:
locationsgroup = addtogroupkey(locationsgroup, keyone, locations[ii])
if locations[ii+1] not in locationsgroup[keyone]:
locationsgroup = addtogroupkey(locationsgroup, keyone, locations[ii+1])
else:
locationsgroup = addtogroupkey(locationsgroup, keyone, locations[ii])
locationsgroup = addtogroupkey(locationsgroup, keyone, locations[ii+1])
#for kk in locationsgroup:
if 1 == 1:
for oo in locationsgroup[keyone]:
if isneighborlocation(oo, locations[ii]) == 1:
if oo not in locationsgroup[keyone]:
locationsgroup = addtogroupkey(locationsgroup, keyone, oo)
if locations[ii] not in locationsgroup[keyone]:
locationsgroup = addtogroupkey(locationsgroup, keyone, locations[ii])
if isneighborlocation(oo, locations[ii+1]) == 1:
if oo not in locationsgroup[keyone]:
locationsgroup = addtogroupkey(locationsgroup, keyone, oo)
if locations[ii+1] not in locationsgroup[keyone]:
locationsgroup = addtogroupkey(locationsgroup, keyone, locations[ii+1])
continueconnect = 1
else:
if len(locationsgroup) > 0:
if locations[ii] not in locationsgroup[len(locationsgroup)]:
locationsgroup = addtogroupkey(locationsgroup, len(locationsgroup)+1, locations[ii])
else:
locationsgroup = addtogroupkey(locationsgroup, len(locationsgroup)+1, locations[ii])
continueconnect = 0
colortolocation[eachcolor] = locationsgroup
#for kk in colortolocation[(0,0,0)]:
#if len(colortolocation[(0,0,0)][kk]) > 0:
#print (min(colortolocation[(0,0,0)][kk])[0],min(colortolocation[(0,0,0)][kk])[1]),(max(colortolocation[(0,0,0)][kk])[0],max(colortolocation[(0,0,0)][kk])[1])
for kk in colortolocation[(0,0,0)]:
if len(colortolocation[(0,0,0)][kk]) > 0:
cv2.rectangle(im,(min(colortolocation[(0,0,0)][kk])[0],min(colortolocation[(0,0,0)][kk])[1]),(max(colortolocation[(0,0,0)][kk])[0],max(colortolocation[(0,0,0)][kk])[1]),(0,0,255),2)
cv2.imshow('norm',im)
cv2.waitKey(0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment