Guest User

eye corners using harris

a guest
Mar 10th, 2012
2,420
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import cv
  2.  
  3.  
  4. filename = "C:\\Users\\ronnieaka\\Desktop\\PROJ\\openCV\\threshed3.jpg"
  5. image = cv.LoadImage("C:\\Users\\ronnieaka\\Desktop\\PROJ\\openCV\\eyeSnaps\\batch2\\snap3.jpg")
  6.  
  7. ##image = cv.LoadImage("C:\\Users\\ronnieaka\\Desktop\\PROJ\\openCV\\eyeSnaps\\closed0.jpg")
  8.  
  9. gray = cv.CreateImage((image.width, image.height), 8, 1)
  10. threshed2 = cv.CreateImage((image.width, image.height), 8, 1)
  11. canny = cv.CreateImage((image.width, image.height), 8, 1)
  12. corner = cv.CreateImage((image.width, image.height), 32, 1)
  13. sobel = cv.CreateImage((image.width, image.height), 32, 1)
  14. res = cv.CreateImage((canny.width*3,canny.height*3),canny.depth,canny.nChannels)
  15.  
  16. def HarrisCorners():
  17.  
  18.     # Convert color input sobel to grayscale
  19.     cv.CvtColor(image, gray, cv.CV_BGR2GRAY) #========================= 1  gray
  20.  
  21.     #cv.CornerHarris(gray,corner,3)
  22.  
  23.     # Equalize the histogram
  24.     cv.EqualizeHist(gray,gray) #======================================= 2 contrast enhancement
  25.     cv.Threshold(gray,threshed2,33,10,cv.CV_THRESH_BINARY) #=========== 3 threshold
  26.  
  27.     cv.Erode(threshed2,threshed2,None,2)
  28.     cv.EqualizeHist(threshed2,threshed2)
  29.     cv.Dilate(threshed2,threshed2,None,2)
  30.  
  31.     cv.Sobel(threshed2,sobel,1,2,5)
  32.     cv.Canny(threshed2,canny,20,100,3) #================================ 4 create edges
  33.     cv.Resize(canny,res,cv.CV_INTER_LINEAR)
  34.  
  35. ##    cv.Sobel(threshed2,sobel,2,2,3)
  36.  
  37.     cv.CornerHarris(threshed2,corner,3)
  38. ##    cv.CornerHarris(sobel,corner,2)
  39.  
  40.     max_dist = 0
  41.     maxL = 20
  42.     maxR = 0
  43.  
  44.     lc =0
  45.     rc =0
  46.  
  47.     maxLP =[]
  48.     maxRP =[]
  49.    
  50.     for y in range(0, image.height):
  51.          for x in range(0, image.width):
  52.              
  53.               harris = cv.Get2D(corner, y, x) # get the x,y value
  54.               # check the corner detector response
  55.              
  56.               # draw a small circle on the original image
  57.               if harris[0] > 10e-06:
  58.                   if ( x<image.width/5 or x>((image.width/4)*3) ) and y>40:
  59.                       #cv.Circle(image,(x,y),2,cv.RGB(155, 0, 25))
  60.  
  61.                       if maxL > x:
  62.                                maxL = x
  63.                                maxLP = (x,y)
  64.                                print maxLP
  65.                                
  66.                       if maxR < x:
  67.                                maxR = x
  68.                                maxRP = (x,y)
  69.  
  70.                       dist = maxR-maxL
  71.  
  72.                       if max_dist<dist:
  73.                            max_dist = maxR-maxL
  74.                            lc = maxLP
  75.                            rc = maxRP
  76.  
  77.  
  78.     cv.Circle(image,maxLP,3,cv.RGB(0, 255, 0))
  79.     cv.Circle(image,maxRP,3,cv.RGB(0,255,0))
  80.  
  81.  
  82.  
  83. HarrisCorners()
  84.  
  85. while True:
  86.  
  87.    
  88.     cv.ShowImage("image",image)
  89.     cv.ShowImage('sobel',res)
  90.     cv.ShowImage("edge",corner)
  91.     cv.ShowImage("threshed", threshed2)
  92.     #cv.ShowImage("pupil",image)
  93.    
  94.     k = cv.WaitKey(10)
  95.     if k % 0x100 == 27: # if ESC key is pressed....
  96.  
  97.         #cv.SaveImage(filename,threshed2)
  98.         cv.DestroyWindow("threshed") #Destroy the window of detection results
  99.         cv.DestroyWindow("image")
  100.         cv.DestroyWindow("edge")
  101.         #cv.DestroyWindow("pupil")
  102.         cv.DestroyWindow('sobel')
  103.         print (k%0x100)
  104.         break
  105.  
  106.     elif k == ord('e'): #if E is pressed
  107.             eyeSnap = "C:\\Users\\ronnieaka\\Desktop\\PROJ\\openCV\\eyeSnaps\\EC_GTSb223.jpg"
  108.             cv.SaveImage(eyeSnap,image)
RAW Paste Data