Skip to content

Instantly share code, notes, and snippets.

@ibtisamtauhidi
Last active August 29, 2015 14:18
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 ibtisamtauhidi/365d759d3f6590136c58 to your computer and use it in GitHub Desktop.
Save ibtisamtauhidi/365d759d3f6590136c58 to your computer and use it in GitHub Desktop.
ADS ROI Extraction
import sys
import cv2
from Tkinter import *
def callback(event):
sys.exit(0)
def validateArg():
if len(sys.argv) < 3 :
print "Argument Error"
sys.exit(0)
def findNormalHist(w):
maxB = 0
maxG = 0
maxR = 0
hist = [[0 for x in range(256)] for x in range(3)]
print "Calculating Normal Histogram"
img = cv2.imread(sys.argv[1])
height = img.shape[0]
width = img.shape[1]
for i in range(0,height):
for j in range(0,width):
hist[0][img[i,j,0]] = hist[0][img[i,j,0]] + 1
hist[1][img[i,j,1]] = hist[1][img[i,j,1]] + 1
hist[2][img[i,j,2]] = hist[2][img[i,j,2]] + 1
if maxB < hist[0][img[i,j,0]]:
maxB = hist[0][img[i,j,0]]
if maxG < hist[1][img[i,j,1]]:
maxG = hist[1][img[i,j,1]]
if maxR < hist[2][img[i,j,2]]:
maxR = hist[2][img[i,j,2]]
for i in range(0,255):
w.create_line(50+i, 450-(hist[0][i]/(maxB*1.0))*300, 50+i, 450, fill="blue")
w.create_line(315+i, 450-(hist[1][i]/(maxG*1.0))*300, 315+i, 450, fill="green")
w.create_line(570+i, 450-(hist[2][i]/(maxR*1.0))*300, 570+i, 450, fill="red")
def findRegion(h,w,h_max,w_max,n):
h_pp = (h_max/2)/n
w_pp = (w_max/2)/n
h_dist = mod((h_max/2)-h)
w_dist = mod((w_max/2)-w)
div_h = h_dist/h_pp
div_w = w_dist/w_pp
div = max(div_h,div_w)
if div==n:
div=div-1
return div
def mod(x):
if x<0:
return x*-1
else:
return x
def findSpatialHist(n,w):
maxB = 0
maxG = 0
maxR = 0
hist = [[0 for x in range(256)] for x in range(3)]
print "Calculating Spatial Histogram"
img = cv2.imread(sys.argv[1])
height = img.shape[0]
width = img.shape[1]
for i in range(0,height):
for j in range(0,width):
div = findRegion(i,j,height,width,n)
bin_size = 255/n
hist[0][(img[i,j,0]/n)+bin_size*div] = hist[0][(img[i,j,0]/n)+bin_size*div]+1
hist[1][(img[i,j,1]/n)+bin_size*div] = hist[1][(img[i,j,0]/n)+bin_size*div]+1
hist[2][(img[i,j,2]/n)+bin_size*div] = hist[2][(img[i,j,0]/n)+bin_size*div]+1
if maxB < hist[0][(img[i,j,0]/n)+bin_size*div]:
maxB = hist[0][(img[i,j,0]/n)+bin_size*div]
if maxG < hist[1][(img[i,j,1]/n)+bin_size*div]:
maxG = hist[1][(img[i,j,1]/n)+bin_size*div]
if maxR < hist[2][(img[i,j,2]/n)+bin_size*div]:
maxR = hist[2][(img[i,j,2]/n)+bin_size*div]
for i in range(0,255):
w.create_line(50+i, 450-(hist[0][i]/(maxB*1.0))*300, 50+i, 450, fill="blue")
w.create_line(315+i, 450-(hist[1][i]/(maxG*1.0))*300, 315+i, 450, fill="green")
w.create_line(570+i, 450-(hist[2][i]/(maxR*1.0))*300, 570+i, 450, fill="red")
def drawHist():
master = Tk()
w = Canvas(master, width=960, height=480)
w.bind("<Button-1>", callback)
w.pack()
w.create_line(49, 100, 49, 450, fill="black")
w.create_line(49, 450, 900, 450, fill="black")
return w
if __name__ == "__main__":
validateArg()
w = drawHist()
if(sys.argv[2] == 'n'):
findNormalHist(w)
elif(sys.argv[2] == 's'):
n = 10
findSpatialHist(n,w)
mainloop()
import sys
import cv2
from Tkinter import *
import Image
import ImageTk
def callback(event):
sys.exit(0)
def validateArg():
if len(sys.argv) < 2 :
print "Argument Error"
sys.exit(0)
def setupGUI(master,h,w):
w = Canvas(master, width=w, height=h)
w.bind("<Button-1>", callback)
w.pack()
return w
def setupHistGUI(master):
frame = Frame(master)
w = Canvas(frame, width=960, height=480)
w.bind("<Button-1>", callback)
w.pack()
w.create_line(49, 100, 49, 450, fill="black")
w.create_line(49, 450, 900, 450, fill="black")
frame.pack()
return w
def findROI(img,height,width):
mat = [[0 for x in range(width)] for x in range(height)]
top = -1
for i in range(0,height-1):
for j in range(0,width-1):
if img[i,j,0]!=img[i,j+1,0] or img[i,j,1]!=img[i,j+1,1] or img[i,j,2]!=img[i,j+1,2]:
mat[i][j]=1
if img[i,j,0]!=img[i+1,j,0] or img[i,j,1]!=img[i+1,j,1] or img[i,j,2]!=img[i+1,j,2]:
mat[i][j]=1
if img[i,j,0]!=img[i+1,j+1,0] or img[i,j,1]!=img[i+1,j+1,1] or img[i,j,2]!=img[i+1,j+1,2]:
mat[i][j]=1
return mat
def drawEdge(w,mat):
max_i = -1
min_i = height+1
max_j = -1
min_j = width+1
for i in range(0,height):
for j in range(0,width):
if mat[i][j]==1:
w.create_line(j,i,j+1,i+1, width=1,fill="#000")
max_i = max(max_i,i)
min_i = min(min_i,i)
max_j = max(max_j,j)
min_j = min(min_j,j)
return [max_i,min_i,max_j,min_j]
def drawLine(w,points):
max_i = points[0]
min_i = points[1]
max_j = points[2]
min_j = points[3]
#::: [j -> width] ::: [i -> height] :::#
w.create_line(min_j, min_i, min_j, max_i)
w.create_line(min_j, min_i, max_j, min_i)
w.create_line(max_j, max_i, min_j, max_i)
w.create_line(max_j, max_i, max_j, min_i)
def findNormalHist(img,w,points):
max_i = points[0]
min_i = points[1]
max_j = points[2]
min_j = points[3]
maxB = 0
maxG = 0
maxR = 0
hist = [[0 for x in range(256)] for x in range(3)]
print "Calculating Normal Histogram"
height = img.shape[0]
width = img.shape[1]
for i in range(min_i,max_i):
for j in range(min_j,max_j):
hist[0][img[i,j,0]] = hist[0][img[i,j,0]] + 1
hist[1][img[i,j,1]] = hist[1][img[i,j,1]] + 1
hist[2][img[i,j,2]] = hist[2][img[i,j,2]] + 1
if maxB < hist[0][img[i,j,0]]:
maxB = hist[0][img[i,j,0]]
if maxG < hist[1][img[i,j,1]]:
maxG = hist[1][img[i,j,1]]
if maxR < hist[2][img[i,j,2]]:
maxR = hist[2][img[i,j,2]]
for i in range(0,255):
w.create_line(50+i, 450-(hist[0][i]/(maxB*1.0))*300, 50+i, 450, fill="blue")
w.create_line(315+i, 450-(hist[1][i]/(maxG*1.0))*300, 315+i, 450, fill="green")
w.create_line(570+i, 450-(hist[2][i]/(maxR*1.0))*300, 570+i, 450, fill="red")
def findRegion(h,w,h_max,w_max,n):
h_pp = (h_max/2.0)/n
w_pp = (w_max/2.0)/n
h_dist = mod((h_max/2.0)-h)
w_dist = mod((w_max/2.0)-w)
div_h = h_dist/h_pp
div_w = w_dist/w_pp
div = max(div_h,div_w)
if div==n:
div=div-1
return int(div)
def mod(x):
if x<0:
return x*-1
else:
return x
def findSpatialHist(img,n,w,points):
max_i = points[0]
min_i = points[1]
max_j = points[2]
min_j = points[3]
maxB = 0
maxG = 0
maxR = 0
hist = [[0 for x in range(256)] for x in range(3)]
height = max_i-min_i
width = max_j-min_j
#print str(height)+","+str(width)
for i in range(min_i,max_i):
for j in range(min_j,max_j):
div = findRegion(max_i-i,max_j-j,height,width,n)
bin_size = 255/n
b = (img[i,j,0]/n)+bin_size*div
g = (img[i,j,1]/n)+bin_size*div
r = (img[i,j,2]/n)+bin_size*div
hist[0][b] = hist[0][b]+1
hist[1][g] = hist[1][g]+1
hist[2][r] = hist[2][r]+1
if maxB < hist[0][b]:
maxB = hist[0][b]
if maxG < hist[1][g]:
maxG = hist[1][g]
if maxR < hist[2][r]:
maxR = hist[2][r]
for i in range(0,255):
w.create_line(50+i, 450-(hist[0][i]/(maxB*1.0))*300, 50+i, 450, fill="blue")
w.create_line(315+i, 450-(hist[1][i]/(maxG*1.0))*300, 315+i, 450, fill="green")
w.create_line(570+i, 450-(hist[2][i]/(maxR*1.0))*300, 570+i, 450, fill="red")
if __name__ == "__main__":
validateArg()
img = cv2.imread(sys.argv[1])
height = img.shape[0]
width = img.shape[1]
master = Tk()
w = setupGUI(master,height,width)
bg = Image.open(sys.argv[1])
anchor = ImageTk.PhotoImage(bg)
w.create_image(0, 0, anchor=NW, image=anchor)
w.pack(fill=BOTH, expand=1)
mat = findROI(img,height,width)
points = drawEdge(w,mat)
drawLine(w,points)
max_i = points[0]
min_i = points[1]
max_j = points[2]
min_j = points[3]
histFrame = Toplevel(master)
x = setupHistGUI(histFrame)
if(sys.argv[2] == 'n'):
findNormalHist(img,x,points)
elif(sys.argv[2] == 's'):
n = 10
findSpatialHist(img,n,x,points)
mainloop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment