|  | '''
 | 
  
    |  | Created on 30.4.2011
 | 
  
    |  | 
 | 
  
    |  | @author: thorag08
 | 
  
    |  | '''
 | 
  
    |  | 
 | 
  
    |  | import sys
 | 
  
    |  | sys.path.append("C:\OpenCV2.2\Python2.7\Lib\site-packages")
 | 
  
    |  | import cv
 | 
  
    |  | import time
 | 
  
    |  | 
 | 
  
    |  | """FLIR class. Captures a Frame from Webcam, converts it to a grayscale and creates a histogram for each frame.
 | 
  
    |  | Displays it in a special window"""
 | 
  
    |  | class Flir:
 | 
  
    |  | 
 | 
  
    |  |     def __init__self(self):
 | 
  
    |  |         self.capture = cv.CaptureFromCAM(0)
 | 
  
    |  | #        cv.NamedWindow("FLIR",1)
 | 
  
    |  |     
 | 
  
    |  |     def keyra(self):
 | 
  
    |  |         """Capture the first frame to get size"""
 | 
  
    |  |         self.capture = cv.CaptureFromCAM(0)
 | 
  
    |  |         frame = cv.QueryFrame(self.capture)
 | 
  
    |  |         frame_size = cv.GetSize(frame)
 | 
  
    |  |         gray_image = cv.CreateImage(cv.GetSize(frame), cv.IPL_DEPTH_8U,1)
 | 
  
    |  |         gray_mat=cv.GetMat(gray_image)
 | 
  
    |  |         
 | 
  
    |  |         
 | 
  
    |  |         """While capturing from camera"""
 | 
  
    |  |         while True:
 | 
  
    |  |             color_image = cv.QueryFrame(self.capture)
 | 
  
    |  |             """Convert image from Image to Array"""
 | 
  
    |  |             mat = cv.GetMat(color_image)
 | 
  
    |  |             """Smooth to get rid of false positives"""
 | 
  
    |  |             cv.Smooth(color_image, color_image, cv.CV_GAUSSIAN, 3, 0)
 | 
  
    |  |       
 | 
  
    |  |             """Convert the image to GrayScale"""
 | 
  
    |  |             cv.CvtColor(mat, gray_mat, cv.CV_RGB2GRAY)
 | 
  
    |  |             cv.CvtColor(mat, gray_image, cv.CV_RGB2GRAY)
 | 
  
    |  |             
 | 
  
    |  |             """Size of the histogram - 1D histogram"""
 | 
  
    |  |             bins = 256;
 | 
  
    |  |             hsize = [bins]
 | 
  
    |  |             
 | 
  
    |  |             heat_th = 200
 | 
  
    |  |             counter = 0
 | 
  
    |  |             
 | 
  
    |  |             for ii in range(0,gray_mat.rows):
 | 
  
    |  |                 for jj in range(0,gray_mat.cols):
 | 
  
    |  |                     if gray_mat[ii,jj]>heat_th:
 | 
  
    |  |                         counter= counter + 1
 | 
  
    |  |             numpix=gray_mat.rows*gray_mat.cols
 | 
  
    |  |             
 | 
  
    |  |             hotratio = float(counter)/float(numpix)
 | 
  
    |  |             print hotratio
 | 
  
    |  |             
 | 
  
    |  |             """Ranges - grayscale 0-256"""
 | 
  
    |  |             xranges = [0,256]
 | 
  
    |  |             ranges = [xranges]
 | 
  
    |  |             
 | 
  
    |  |             """Create three windows to show results"""
 | 
  
    |  |             cv.NamedWindow("original",1)  
 | 
  
    |  |             cv.NamedWindow("gray",1)
 | 
  
    |  |             cv.NamedWindow("histogram",1)
 | 
  
    |  |             
 | 
  
    |  |             """planes to obtain the histogram, in this case - just one"""
 | 
  
    |  |             planes = [gray_image]
 | 
  
    |  |             
 | 
  
    |  |             """get the histogram and some info about it"""
 | 
  
    |  |             hist = cv.CreateHist(hsize, cv.CV_HIST_ARRAY, ranges,1);
 | 
  
    |  |             cv.CalcHist( planes, hist, 0);
 | 
  
    |  |             (min_value, max_value, _, max_idx) = cv.GetMinMaxHistValue(hist)
 | 
  
    |  |             #print "min value: ", min_value, " max value: " , max_value
 | 
  
    |  | 
 | 
  
    |  |             """create an 8 bits single channel image to hold the histogram"""
 | 
  
    |  |             """paint it white"""
 | 
  
    |  |             imgHistogram = cv.CreateImage((bins, 250),8,1);
 | 
  
    |  |             cv.Rectangle(imgHistogram,(0,0),(256,250), cv.CV_RGB(255,255,255),-1);
 | 
  
    |  |             
 | 
  
    |  |             """draw the histogram"""
 | 
  
    |  |             for i in range(0,bins):
 | 
  
    |  |                 value = cv.QueryHistValue_1D(hist, i);
 | 
  
    |  |                 normalized = cv.Round(value*250/max_value);
 | 
  
    |  |                 cv.Line(imgHistogram,(i,250),(i,250-normalized), cv.CV_RGB(0,0,0));
 | 
  
    |  |             
 | 
  
    |  |             """Ef max_value fer yfir akvedid gildi - prenta thad ut"""
 | 
  
    |  |             if max_value > 100000:
 | 
  
    |  |                 print "of hvitt"
 | 
  
    |  |                 print max_value
 | 
  
    |  |                 
 | 
  
    |  |             """Show the image results"""
 | 
  
    |  |             cv.ShowImage( "original", mat );
 | 
  
    |  |             cv.ShowImage( "gray", gray_image);
 | 
  
    |  |             cv.ShowImage( "histogram", imgHistogram );
 | 
  
    |  |             
 | 
  
    |  |             c = cv.WaitKey(7) % 0x100
 | 
  
    |  |             if c == 27 or c==10:
 | 
  
    |  |                 break
 | 
  
    |  | 
 | 
  
    |  | if __name__=="__main__":
 | 
  
    |  |     t = Flir()
 | 
  
    |  |     t.keyra()
 | 
  
    |  | 
 | 
  
    |  | 
 |