cv_actions.py
Go to the documentation of this file.
00001 from pkg import *
00002 import cv
00003 import laser_interface.blob as blob
00004 
00005 class CombineMasks:
00006     def __init__(self, sample_image, channels=1):
00007         self.combined = cv.CreateImage(cv.GetSize(sample_image), 8 , channels)
00008 
00009     def combine(self, images):
00010         cv.Set(self.combined, 1)
00011         for img in images:
00012             cv.Mul(self.combined, img, self.combined)
00013         return self.combined
00014 
00015 class Mask:
00016     def __init__(self, sample_image):
00017         self.thres_red_img         = cv.CreateImage(cv.GetSize(sample_image), 8 , 1)
00018         self.thres_green_img       = cv.CreateImage(cv.GetSize(sample_image), 8 , 1)
00019         self.thres_blue_img        = cv.CreateImage(cv.GetSize(sample_image), 8 , 1)
00020         self.merged_frame          = cv.CreateImage(cv.GetSize(sample_image), 8 , 3)
00021 
00022     def mask(self, mask, r, g, b):
00023         cv.Mul(r, mask, self.thres_red_img)
00024         cv.Mul(g, mask, self.thres_green_img)
00025         cv.Mul(b, mask, self.thres_blue_img)
00026         cv.Merge(self.thres_blue_img, self.thres_green_img, self.thres_red_img, None, self.merged_frame);
00027         return self.merged_frame
00028 
00029 class SplitColors:
00030     def __init__(self, sample_image):
00031         self.green_img             = cv.CreateImage(cv.GetSize(sample_image), 8 , 1)
00032         self.red_img               = cv.CreateImage(cv.GetSize(sample_image), 8 , 1)
00033         self.blue_img              = cv.CreateImage(cv.GetSize(sample_image), 8 , 1)
00034 
00035     def split(self, image):
00036         cv.Split(image, self.blue_img, self.green_img, self.red_img, None);
00037         return (self.red_img, self.green_img, self.blue_img)
00038 
00039 class BrightnessThreshold:
00040     def __init__(self, sample_image, max_area): #, tune=False):
00041         #self.thres_low  = thres_low
00042         #self.thres_high = thres_high
00043         self.max_area = max_area
00044         #self.set_thresholds([thres_low, thres_high])
00045         #self.csplit = SplitColors(sample_image)
00046         #if should_mask:
00047         #    self.mask   = Mask(sample_image)
00048         self.thresholded_low      = cv.CreateImage(cv.GetSize(sample_image), 8 , 1)
00049         self.thresholded_high     = cv.CreateImage(cv.GetSize(sample_image), 8 , 1)
00050         self.thresholded_combined = cv.CreateImage(cv.GetSize(sample_image), 8 , 1)
00051         #self.should_mask = should_mask
00052         #self.channel = channel
00053         #self.debug = False
00054         #self.tune = tune
00055 
00056         #if tune:
00057         #    cv.NamedWindow('low', 1)
00058         #    cv.NamedWindow('high', 1)
00059     #def set_thresholds(self, thresholds):
00060     #    self.thres_low = thresholds[0]
00061     #    self.thresh_high = thresholds[1]
00062 
00063     def get_thresholded_image(self):
00064         return self.thresholded_combined
00065 
00066     def threshold(self, thres_low, thres_high, thres_chan):
00067         result_val = 1 #Change result_val to 255 if need to view image
00068 
00069         cv.Threshold(thres_chan, self.thresholded_low, thres_low, result_val, cv.CV_THRESH_BINARY)
00070         cv.Dilate(self.thresholded_low, self.thresholded_low) #thresholded_low thresholded image using threshold for dark regions
00071         blob.remove_large_blobs(self.thresholded_low, self.max_area)
00072 
00073         cv.Threshold(thres_chan, self.thresholded_high, thres_high, result_val, cv.CV_THRESH_BINARY)
00074         cv.Dilate(self.thresholded_high, self.thresholded_high) #thresholded_high thresholded image using threshold for bright regions
00075         blob.remove_large_blobs(self.thresholded_high, self.max_area)#, show=True)
00076 
00077         cv.Or(self.thresholded_low, self.thresholded_high, self.thresholded_combined)
00078         return self.thresholded_combined
00079 
00080 class MotionSubtract:
00081     def __init__(self, sample_image, max_area, adaptation_rate=0.8, threshold=10):
00082         self.max_area              = max_area
00083         self.accumulator           = cv.CreateImage(cv.GetSize(sample_image), 32, 1)
00084         cv.SetZero(self.accumulator)
00085         self.thresholded_img       = cv.CreateImage(cv.GetSize(sample_image), 8 , 1)
00086         self.difference_img        = cv.CreateImage(cv.GetSize(sample_image), 32 , 1)
00087         self.green32_img           = cv.CreateImage(cv.GetSize(sample_image), 32 , 1)
00088         self.adaptation_rate       = adaptation_rate
00089         self.threshold             = threshold
00090 
00091     def get_thresholded_image(self):
00092         return self.thresholded_img
00093 
00094     def subtract(self, thres_chan):
00095         cv.RunningAvg(thres_chan, self.accumulator, self.adaptation_rate)
00096         cv.CvtScale(thres_chan, self.green32_img)
00097         cv.Sub(self.green32_img, self.accumulator, self.difference_img)
00098         cv.Threshold(self.difference_img, self.thresholded_img, self.threshold, 1, cv.CV_THRESH_BINARY)
00099         cv.Dilate(self.thresholded_img, self.thresholded_img, iterations=1)
00100         blob.remove_large_blobs(self.thresholded_img, max_area = self.max_area)
00101         return self.thresholded_img
00102 
00103 def make_visible_binary_image(img):
00104     cv.Threshold(img, img, 0, 255, cv.CV_THRESH_BINARY)
00105 


laser_interface
Author(s): Hai Nguyen and Travis Deyle. Advisor: Prof. Charlie Kemp, Lab: Healthcare Robotics Lab at Georgia Tech
autogenerated on Wed Nov 27 2013 11:45:51