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):
00041
00042
00043 self.max_area = max_area
00044
00045
00046
00047
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
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
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
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)
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)
00075 blob.remove_large_blobs(self.thresholded_high, self.max_area)
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