def avgstd_image_list(images): mean = None std = None if len(images) > 0: scale = 1. / len(images) mean = cv.CreateImage(cv.GetSize(images[0]), cv.IPL_DEPTH_32F, images[0].channels) std = cv.CreateImage(cv.GetSize(images[0]), cv.IPL_DEPTH_32F, images[0].channels) buf = cv.CreateImage(cv.GetSize(images[0]), cv.IPL_DEPTH_32F, images[0].channels) for image in images: cv.Add(image, mean, mean) cv.Mul(image, image, buf) cv.Add(buf, std, std) cv.ConvertScale(mean, mean, scale) cv.ConvertScale(std, std, scale) cv.Mul(mean, mean, buf) cv.Sub(std, buf, std) cv.Pow(std, std, 0.5) meanresult = cv.CreateImage(cv.GetSize(images[0]), images[0].depth, images[0].channels) stdresult = cv.CreateImage(cv.GetSize(images[0]), images[0].depth, images[0].channels) cv.ConvertScale(mean, meanresult) cv.ConvertScale(std, stdresult) del buf del std del mean return (meanresult, stdresult)
def mask(self, mask, r, g, b): cv.Mul(r, mask, self.thres_red_img) cv.Mul(g, mask, self.thres_green_img) cv.Mul(b, mask, self.thres_blue_img) cv.Merge(self.thres_blue_img, self.thres_green_img, self.thres_red_img, None, self.merged_frame) return self.merged_frame
def locateMarker(self, frame): self.frameReal = cv.CloneImage(frame) self.frameImag = cv.CloneImage(frame) self.frameRealThirdHarmonics = cv.CloneImage(frame) self.frameImagThirdHarmonics = cv.CloneImage(frame) # Calculate convolution and determine response strength. cv.Filter2D(self.frameReal, self.frameReal, self.matReal) cv.Filter2D(self.frameImag, self.frameImag, self.matImag) cv.Mul(self.frameReal, self.frameReal, self.frameRealSq) cv.Mul(self.frameImag, self.frameImag, self.frameImagSq) cv.Add(self.frameRealSq, self.frameImagSq, self.frameSumSq) # Calculate convolution of third harmonics for quality estimation. cv.Filter2D(self.frameRealThirdHarmonics, self.frameRealThirdHarmonics, self.matRealThirdHarmonics) cv.Filter2D(self.frameImagThirdHarmonics, self.frameImagThirdHarmonics, self.matImagThirdHarmonics) min_val, max_val, min_loc, max_loc = cv.MinMaxLoc(self.frameSumSq) self.lastMarkerLocation = max_loc (xm, ym) = max_loc self.determineMarkerOrientation(frame) self.determineMarkerQuality() return max_loc
def threshold_image(): """ runs the image processing in order to create a black and white thresholded image out of D.image into D.threshed_image. """ # get D so that we can change values in it global D # Use OpenCV to split the image up into channels, saving them in gray images cv.Split(D.image, D.blue, D.green, D.red, None) # This line creates a hue-saturation-value image cv.CvtColor(D.image, D.hsv, cv.CV_RGB2HSV) cv.Split(D.hsv, D.hue, D.sat, D.val, None) # Here is how OpenCV thresholds the images based on the slider values: cv.InRangeS(D.red, D.thresholds["low_red"], D.thresholds["high_red"], D.red_threshed) cv.InRangeS(D.blue, D.thresholds["low_blue"], D.thresholds["high_blue"], D.blue_threshed) cv.InRangeS(D.green, D.thresholds["low_green"], D.thresholds["high_green"], D.green_threshed) cv.InRangeS(D.hue, D.thresholds["low_hue"], D.thresholds["high_hue"], D.hue_threshed) cv.InRangeS(D.sat, D.thresholds["low_sat"], D.thresholds["high_sat"], D.sat_threshed) cv.InRangeS(D.val, D.thresholds["low_val"], D.thresholds["high_val"], D.val_threshed) # Multiply all the thresholded images into one "output" image, D.threshed_image cv.Mul(D.red_threshed, D.green_threshed, D.threshed_image) cv.Mul(D.threshed_image, D.blue_threshed, D.threshed_image) cv.Mul(D.threshed_image, D.hue_threshed, D.threshed_image) cv.Mul(D.threshed_image, D.sat_threshed, D.threshed_image) cv.Mul(D.threshed_image, D.val_threshed, D.threshed_image)
def create_image(self): #Find the size of the image #Create images for each channel blue = cv.CreateImage(self.size, 8, 1) red = cv.CreateImage(self.size, 8, 1) green = cv.CreateImage(self.size, 8, 1) hue = cv.CreateImage(self.size, 8, 1) sat = cv.CreateImage(self.size, 8, 1) val = cv.CreateImage(self.size, 8, 1) #Create an image to be returned and eventually displayed thresholds = cv.CreateImage(self.size, 8, 1) #Create images to save the thresholded images to red_threshed = cv.CreateImage(self.size, 8, 1) green_threshed = cv.CreateImage(self.size, 8, 1) blue_threshed = cv.CreateImage(self.size, 8, 1) hue_threshed = cv.CreateImage(self.size, 8, 1) sat_threshed = cv.CreateImage(self.size, 8, 1) val_threshed = cv.CreateImage(self.size, 8, 1) #Split the image up into channels, saving them in their respective image cv.Split(self.image, blue, green, red, None) cv.CvtColor(self.image, self.hsv, cv.CV_RGB2HSV) cv.Split(self.hsv, hue, sat, val, None) #Threshold the images based on the slider values cv.InRangeS(red, self.thresholds['low_red'],\ self.thresholds['high_red'], red_threshed) cv.InRangeS(green, self.thresholds['low_green'],\ self.thresholds['high_green'], green_threshed) cv.InRangeS(blue, self.thresholds['low_blue'],\ self.thresholds['high_blue'], blue_threshed) cv.InRangeS(hue, self.thresholds['low_hue'],\ self.thresholds['high_hue'], hue_threshed) cv.InRangeS(sat, self.thresholds['low_sat'],\ self.thresholds['high_sat'], sat_threshed) cv.InRangeS(val, self.thresholds['low_val'],\ self.thresholds['high_val'], val_threshed) #Recombine all of the thresholded images into one image cv.Mul(red_threshed, green_threshed, thresholds) cv.Mul(thresholds, blue_threshed, thresholds) cv.Mul(thresholds, hue_threshed, thresholds) cv.Mul(thresholds, sat_threshed, thresholds) cv.Mul(thresholds, val_threshed, thresholds) #Erode and Dilate shave off and add edge pixels respectively cv.Erode(thresholds, thresholds, iterations=1) cv.Dilate(thresholds, thresholds, iterations=1) return thresholds
def element_wise_dot_product(a, b): a_as_row_vectors = cv.Reshape(a, 1, a.width * a.height) b_as_row_vectors = cv.Reshape(b, 1, b.width * b.height) a_as_rows_mul_b_as_rows = cv.CreateMat(a.width * a.height, 3, cv.CV_32FC1) cv.Mul(a_as_row_vectors, b_as_row_vectors, a_as_rows_mul_b_as_rows) dot_product = cv.CreateMat(a.width * a.height, 1, cv.CV_32FC1) cv.Reduce(a_as_rows_mul_b_as_rows, dot_product, dim=1, op=cv.CV_REDUCE_SUM) return cv.Reshape(dot_product, 1, a.height)
def process_Image(self): """ here is where the image should be processed to get the bounding box """ # check if we've created the supporting images yet if self.threshed_image == None: if self.image != None: self.create_all_images() # from the old method call def threshold_image(self): cv.Split(self.image, self.blue, self.green, self.red, None) cv.CvtColor(self.image, self.hsv, cv.CV_RGB2HSV) cv.Split(self.hsv, self.hue, self.sat, self.val, None) # replace each channel with its thresholded version cv.InRangeS(self.red, self.thresholds['low_red'],\ self.thresholds['high_red'], self.red) cv.InRangeS(self.green, self.thresholds['low_green'],\ self.thresholds['high_green'], self.green) cv.InRangeS(self.blue, self.thresholds['low_blue'],\ self.thresholds['high_blue'], self.blue) cv.InRangeS(self.hue, self.thresholds['low_hue'],\ self.thresholds['high_hue'], self.hue) cv.InRangeS(self.sat, self.thresholds['low_sat'],\ self.thresholds['high_sat'], self.sat) cv.InRangeS(self.val, self.thresholds['low_val'],\ self.thresholds['high_val'], self.val) # AND (multiply) all the thresholded images into one "output" image, # named self.copy cv.Mul(self.red, self.green, self.copy) cv.Mul(self.copy, self.blue, self.copy) cv.Mul(self.copy, self.hue, self.copy) cv.Mul(self.copy, self.sat, self.copy) cv.Mul(self.copy, self.val, self.copy) # erode and dilate shave off and add edge pixels respectively cv.Erode(self.copy, self.copy, iterations = 1) cv.Dilate(self.copy, self.copy, iterations = 1) # Make self.threshed_image be self.copy cv.Copy(self.copy,self.threshed_image) self.find_biggest_region()
def threshold_image(D): """ runs the image processing in order to create a black and white thresholded image out of D.image into D.threshed_image """ # Use OpenCV to split the image up into channels, # saving them in their respective bw images cv.Split(D.image, D.blue, D.green, D.red, None) # This line creates a hue-saturation-value image cv.CvtColor(D.image, D.hsv, cv.CV_RGB2HSV) cv.Split(D.hsv, D.hue, D.sat, D.val, None) # Here is how OpenCV thresholds the images based on the slider values: cv.InRangeS(D.red, D.thresholds["low_red"], \ D.thresholds["high_red"], D.red_threshed) cv.InRangeS(D.green, D.thresholds["low_green"], \ D.thresholds["high_green"], D.green_threshed) cv.InRangeS(D.blue, D.thresholds["low_blue"], \ D.thresholds["high_blue"], D.blue_threshed) cv.InRangeS(D.hue, D.thresholds["low_hue"], \ D.thresholds["high_hue"], D.hue_threshed) cv.InRangeS(D.sat, D.thresholds["low_sat"], \ D.thresholds["high_sat"], D.sat_threshed) cv.InRangeS(D.val, D.thresholds["low_val"], \ D.thresholds["high_val"], D.val_threshed) # Multiply all the thresholded images into one "output" image, # named D.threshed_image"] cv.Mul(D.red_threshed, D.green_threshed, D.threshed_image) cv.Mul(D.threshed_image, D.blue_threshed, D.threshed_image) cv.Mul(D.threshed_image, D.hue_threshed, D.threshed_image) cv.Mul(D.threshed_image, D.sat_threshed, D.threshed_image) cv.Mul(D.threshed_image, D.val_threshed, D.threshed_image) # Erode and Dilate shave off and add edge pixels respectively cv.Erode(D.threshed_image, D.threshed_image, iterations=1) cv.Dilate(D.threshed_image, D.threshed_image, iterations=1)
def project_pixels_to_3d_rays(pixels, model): x = cv.CreateMat(pixels.height, pixels.width, cv.CV_32FC1) y = cv.CreateMat(pixels.height, pixels.width, cv.CV_32FC1) cv.Split(pixels, x, y, None, None) x_squared = cv.CreateMat(pixels.height, pixels.width, cv.CV_32FC1) cv.Pow(x, x_squared, 2) y_squared = cv.CreateMat(pixels.height, pixels.width, cv.CV_32FC1) cv.Pow(y, y_squared, 2) inverse_norm = cv.CreateMat(pixels.height, pixels.width, cv.CV_32FC1) cv.Add(x_squared, y_squared, inverse_norm) cv.AddS(inverse_norm, 1, inverse_norm) cv.Pow(inverse_norm, inverse_norm, -0.5) cv.Mul(x, inverse_norm, x) cv.Mul(y, inverse_norm, y) result = cv.CreateMat(pixels.height, pixels.width, cv.CV_32FC3) cv.Merge(x, y, inverse_norm, None, result) return result
def ray_plane_intersections(rays, planes): rows = rays.height cols = rays.width rays_split = [None] * 3 for i in range(3): rays_split[i] = cv.CreateMat(rows, cols, cv.CV_32FC1) cv.Split(rays, rays_split[0], rays_split[1], rays_split[2], None) planes_split = [None] * 4 for i in range(4): planes_split[i] = cv.CreateMat(rows, cols, cv.CV_32FC1) cv.Split(planes, planes_split[0], planes_split[1], planes_split[2], planes_split[3]) n_dot_v = cv.CreateMat(rows, cols, cv.CV_32FC1) cv.SetZero(n_dot_v) for i in range(3): temp = cv.CreateMat(rows, cols, cv.CV_32FC1) cv.Mul(planes_split[i], rays_split[i], temp) cv.Add(temp, n_dot_v, n_dot_v) depth = cv.CreateMat(rows, cols, cv.CV_32FC1) cv.Div(planes_split[3], n_dot_v, depth) intersection_points_split = [None] * 3 for i in range(3): intersection_points_split[i] = cv.CreateMat(rows, cols, cv.CV_32FC1) for i in range(3): cv.Mul(depth, rays_split[i], intersection_points_split[i]) intersection_points = cv.CreateMat(rows, cols, cv.CV_32FC3) cv.Merge(intersection_points_split[0], intersection_points_split[1], intersection_points_split[2], None, intersection_points) return intersection_points
def get_hands(image): """ Returns the hand as white on black. Uses value in HSV to determine hands.""" size = cv.GetSize(image) hsv = cv.CreateImage(size, 8, 3) hue = cv.CreateImage(size, 8, 1) sat = cv.CreateImage(size, 8, 1) val = cv.CreateImage(size, 8, 1) hands = cv.CreateImage(size, 8, 1) cv.CvtColor(image, hsv, cv.CV_BGR2HSV) cv.Split(hsv, hue, sat, val, None) cv.ShowImage('Live', image) cv.ShowImage('Hue', hue) cv.ShowImage('Saturation', sat) cv.Threshold( hue, hue, 10, 255, cv.CV_THRESH_TOZERO) #set to 0 if <= 10, otherwise leave as is cv.Threshold( hue, hue, 244, 255, cv.CV_THRESH_TOZERO_INV) #set to 0 if > 244, otherwise leave as is cv.Threshold(hue, hue, 0, 255, cv.CV_THRESH_BINARY_INV) #set to 255 if = 0, otherwise 0 cv.Threshold( sat, sat, 64, 255, cv.CV_THRESH_TOZERO) #set to 0 if <= 64, otherwise leave as is cv.EqualizeHist(sat, sat) cv.Threshold(sat, sat, 64, 255, cv.CV_THRESH_BINARY) #set to 0 if <= 64, otherwise 255 cv.ShowImage('Saturation threshold', sat) cv.ShowImage('Hue threshold', hue) cv.Mul(hue, sat, hands) #smooth + threshold to filter noise # cv.Smooth(hands, hands, smoothtype=cv.CV_GAUSSIAN, param1=13, param2=13) # cv.Threshold(hands, hands, 200, 255, cv.CV_THRESH_BINARY) cv.ShowImage('Hands', hands) return hands
def noisy(image, noise_typ, filename): if noise_typ == "gauss": row, col = image.shape mean = 0 deviation = 30 #var = 0.1 #sigma = var**0.5 gauss = np.random.normal(mean, deviation, (row, col)) gauss = gauss.reshape(row, col) noisy = image + gauss misc.imsave(filename, noisy) elif noise_typ == "s&p": row, col = image.shape s_vs_p = 0.5 amount = 0.01 out = image # Salt mode num_salt = np.ceil(amount * image.size * s_vs_p) coords = [ np.random.randint(0, i - 1, int(num_salt)) for i in image.shape ] out[coords] = 255 # Pepper mode num_pepper = np.ceil(amount * image.size * (1. - s_vs_p)) coords = [ np.random.randint(0, i - 1, int(num_pepper)) for i in image.shape ] out[coords] = 0 misc.imsave(filename, out) elif noise_typ == "speckle": mult_noise = cv.CreateImage((image.width, image.height), cv.IPL_DEPTH_32F, 1) cv.RandArr(cv.RNG(6), mult_noise, cv.CV_RAND_NORMAL, 1, 0.1) cv.Mul(image, mult_noise, image) cv.SaveImage(filename, image) elif noise_typ == "blur": blur = ndimage.gaussian_filter(image, sigma=3) misc.imsave(filename, blur)
def find_car(image): size = cv.GetSize(image) #prepare memory car = cv.CreateImage(size, 8, 1) red = cv.CreateImage(size, 8, 1) hsv = cv.CreateImage(size, 8, 3) sat = cv.CreateImage(size, 8, 1) #split image into hsv, grab the sat cv.CvtColor(image, hsv, cv.CV_BGR2HSV) cv.Split(hsv, None, sat, None, None) #split image into rgb cv.Split(image, None, None, red, None) #find the car by looking for red, with high saturation cv.Threshold(red, red, 128, 255, cv.CV_THRESH_BINARY) cv.Threshold(sat, sat, 128, 255, cv.CV_THRESH_BINARY) #AND the two thresholds, finding the car cv.Mul(red, sat, car) #remove noise, highlighting the car cv.Erode(car, car, iterations=5) cv.Dilate(car, car, iterations=5) storage = cv.CreateMemStorage(0) obj = cv.FindContours(car, storage, cv.CV_RETR_CCOMP, cv.CV_CHAIN_APPROX_SIMPLE) cv.ShowImage('A', car) if not obj: return (0, 0, 0, 0) else: return cv.BoundingRect(obj)
def get_hands( image ): #Image Filtering. Filtering was from outside source in sources.txt file. """ Returns the hand as white on black. Uses value in HSV to determine hands.""" live = image size = cv.GetSize(image) hsv = cv.CreateImage(size, 8, 3) hue = cv.CreateImage(size, 8, 1) sat = cv.CreateImage(size, 8, 1) val = cv.CreateImage(size, 8, 1) hands = cv.CreateImage(size, 8, 1) cv.CvtColor(image, hsv, cv.CV_BGR2HSV) cv.Split(hsv, hue, sat, val, None) red = cv.CV_RGB(255, 0, 0) green = cv.CV_RGB(0, 255, 0) #print cornerPoints(4) if canvas.data.liveVideo.get() == "True": if canvas.data.showActiveCorners.get( ) == "True": #Drawing Video Overlay for i in range(len(canvas.data.activePoints)): points = cornerPoints(i) point1 = points[0] point2 = points[1] point3 = points[2] point4 = points[3] if canvas.data.activePoints[i] == True: cv.Rectangle(live, (point1,point2), (point3,point4), \ green,2) else: cv.Rectangle(live, (point1, point2), (point3, point4), \ red,2) cv.ShowImage('Live', live) #cv.ShowImage('Hue', hue) #cv.ShowImage('Saturation', sat) cv.Threshold( hue, hue, 10, 255, cv.CV_THRESH_TOZERO) #set to 0 if <= 10, otherwise leave as is cv.Threshold( hue, hue, 244, 255, cv.CV_THRESH_TOZERO_INV) #set to 0 if > 244, otherwise leave as is cv.Threshold(hue, hue, 0, 255, cv.CV_THRESH_BINARY_INV) #set to 255 if = 0, otherwise 0 cv.Threshold( sat, sat, 64, 255, cv.CV_THRESH_TOZERO) #set to 0 if <= 64, otherwise leave as is cv.EqualizeHist(sat, sat) cv.Threshold(sat, sat, 64, 255, cv.CV_THRESH_BINARY) #set to 0 if <= 64, otherwise 255 #cv.ShowImage('Saturation threshold', sat) #cv.ShowImage('Hue threshold', hue) cv.Mul(hue, sat, hands) #smooth + threshold to filter noise #cv.Smooth(hands, hands, smoothtype=cv.CV_GAUSSIAN, param1=13, param2=13) #cv.Threshold(hands, hands, 200, 255, cv.CV_THRESH_BINARY) if canvas.data.testVideo.get() == "True": cv.ShowImage('Hands', hands) cv.SaveImage("hands.jpg", hands) #openCVtoPIL(hands) cornerCrop() colorslist = getColors() #print colorslist sortedlist = getColorsSort(colorslist) percentcolorslist = getColorsPercentage(sortedlist) canvas.data.activePoints = getColorsAnalysis(percentcolorslist) checkAction() doAction() #histogramAnalysis() return hands
def doSSIM(frame1, frame2): ''' The equivalent of Zhou Wang's SSIM matlab code using OpenCV. from http://www.cns.nyu.edu/~zwang/files/research/ssim/index.html The measure is described in : "Image quality assessment: From error measurement to structural similarity" C++ code by Rabah Mehdi. http://mehdi.rabah.free.fr/SSIM C++ to Python translation and adaptation by Iñaki Úcar ''' def array2cv(a): dtype2depth = { 'uint8': cv.IPL_DEPTH_8U, 'int8': cv.IPL_DEPTH_8S, 'uint16': cv.IPL_DEPTH_16U, 'int16': cv.IPL_DEPTH_16S, 'int32': cv.IPL_DEPTH_32S, 'float32': cv.IPL_DEPTH_32F, 'float64': cv.IPL_DEPTH_64F, } try: nChannels = a.shape[2] except: nChannels = 1 cv_im = cv.CreateImageHeader((a.shape[1], a.shape[0]), dtype2depth[str(a.dtype)], nChannels) cv.SetData(cv_im, a.tostring(), a.dtype.itemsize * nChannels * a.shape[1]) return cv_im C1 = 6.5025 C2 = 58.5225 img1_temp = array2cv(frame1) img2_temp = array2cv(frame2) nChan = img1_temp.nChannels d = cv.IPL_DEPTH_32F size = img1_temp.width, img1_temp.height img1 = cv.CreateImage(size, d, nChan) img2 = cv.CreateImage(size, d, nChan) cv.Convert(img1_temp, img1) cv.Convert(img2_temp, img2) img1_sq = cv.CreateImage(size, d, nChan) img2_sq = cv.CreateImage(size, d, nChan) img1_img2 = cv.CreateImage(size, d, nChan) cv.Pow(img1, img1_sq, 2) cv.Pow(img2, img2_sq, 2) cv.Mul(img1, img2, img1_img2, 1) mu1 = cv.CreateImage(size, d, nChan) mu2 = cv.CreateImage(size, d, nChan) mu1_sq = cv.CreateImage(size, d, nChan) mu2_sq = cv.CreateImage(size, d, nChan) mu1_mu2 = cv.CreateImage(size, d, nChan) sigma1_sq = cv.CreateImage(size, d, nChan) sigma2_sq = cv.CreateImage(size, d, nChan) sigma12 = cv.CreateImage(size, d, nChan) temp1 = cv.CreateImage(size, d, nChan) temp2 = cv.CreateImage(size, d, nChan) temp3 = cv.CreateImage(size, d, nChan) ssim_map = cv.CreateImage(size, d, nChan) #/*************************** END INITS **********************************/ #// PRELIMINARY COMPUTING cv.Smooth(img1, mu1, cv.CV_GAUSSIAN, 11, 11, 1.5) cv.Smooth(img2, mu2, cv.CV_GAUSSIAN, 11, 11, 1.5) cv.Pow(mu1, mu1_sq, 2) cv.Pow(mu2, mu2_sq, 2) cv.Mul(mu1, mu2, mu1_mu2, 1) cv.Smooth(img1_sq, sigma1_sq, cv.CV_GAUSSIAN, 11, 11, 1.5) cv.AddWeighted(sigma1_sq, 1, mu1_sq, -1, 0, sigma1_sq) cv.Smooth(img2_sq, sigma2_sq, cv.CV_GAUSSIAN, 11, 11, 1.5) cv.AddWeighted(sigma2_sq, 1, mu2_sq, -1, 0, sigma2_sq) cv.Smooth(img1_img2, sigma12, cv.CV_GAUSSIAN, 11, 11, 1.5) cv.AddWeighted(sigma12, 1, mu1_mu2, -1, 0, sigma12) #////////////////////////////////////////////////////////////////////////// #// FORMULA #// (2*mu1_mu2 + C1) cv.Scale(mu1_mu2, temp1, 2) cv.AddS(temp1, C1, temp1) #// (2*sigma12 + C2) cv.Scale(sigma12, temp2, 2) cv.AddS(temp2, C2, temp2) #// ((2*mu1_mu2 + C1).*(2*sigma12 + C2)) cv.Mul(temp1, temp2, temp3, 1) #// (mu1_sq + mu2_sq + C1) cv.Add(mu1_sq, mu2_sq, temp1) cv.AddS(temp1, C1, temp1) #// (sigma1_sq + sigma2_sq + C2) cv.Add(sigma1_sq, sigma2_sq, temp2) cv.AddS(temp2, C2, temp2) #// ((mu1_sq + mu2_sq + C1).*(sigma1_sq + sigma2_sq + C2)) cv.Mul(temp1, temp2, temp1, 1) #// ((2*mu1_mu2 + C1).*(2*sigma12 + C2))./((mu1_sq + mu2_sq + C1).*(sigma1_sq + sigma2_sq + C2)) cv.Div(temp3, temp1, ssim_map, 1) index_scalar = cv.Avg(ssim_map) #// through observation, there is approximately #// 1% error max with the original matlab program return index_scalar[0]
def combine(self, images): cv.Set(self.combined, 1) for img in images: cv.Mul(self.combined, img, self.combined) return self.combined
def line_line_intersections(P_0, u, Q_0, v): rows = P_0.height cols = P_0.width w_0 = cv.CreateMat(rows, cols, cv.CV_32FC3) cv.Sub(P_0, Q_0, w_0) a = element_wise_dot_product(u, u) b = element_wise_dot_product(u, v) c = element_wise_dot_product(v, v) d = element_wise_dot_product(u, w_0) e = element_wise_dot_product(v, w_0) a_mul_c = cv.CreateMat(rows, cols, cv.CV_32FC1) cv.Mul(a, c, a_mul_c) b_squared = cv.CreateMat(rows, cols, cv.CV_32FC1) cv.Pow(b, b_squared, 2) denominator = cv.CreateMat(rows, cols, cv.CV_32FC1) cv.Sub(a_mul_c, b_squared, denominator) b_mul_e = cv.CreateMat(rows, cols, cv.CV_32FC1) cv.Mul(b, e, b_mul_e) c_mul_d = cv.CreateMat(rows, cols, cv.CV_32FC1) cv.Mul(c, d, c_mul_d) b_mul_d = cv.CreateMat(rows, cols, cv.CV_32FC1) cv.Mul(b, d, b_mul_d) a_mul_e = cv.CreateMat(rows, cols, cv.CV_32FC1) cv.Mul(a, e, a_mul_e) s_c = cv.CreateMat(rows, cols, cv.CV_32FC1) cv.Sub(b_mul_e, c_mul_d, s_c) cv.Div(s_c, denominator, s_c) t_c = cv.CreateMat(rows, cols, cv.CV_32FC1) cv.Sub(a_mul_e, b_mul_d, t_c) cv.Div(t_c, denominator, t_c) u_x = cv.CreateMat(rows, cols, cv.CV_32FC1) u_y = cv.CreateMat(rows, cols, cv.CV_32FC1) u_z = cv.CreateMat(rows, cols, cv.CV_32FC1) cv.Split(u, u_x, u_y, u_z, None) su_x = cv.CreateMat(rows, cols, cv.CV_32FC1) su_y = cv.CreateMat(rows, cols, cv.CV_32FC1) su_z = cv.CreateMat(rows, cols, cv.CV_32FC1) cv.Mul(s_c, u_x, su_x) cv.Mul(s_c, u_y, su_y) cv.Mul(s_c, u_z, su_z) su = cv.CreateMat(rows, cols, cv.CV_32FC3) cv.Merge(su_x, su_y, su_z, None, su) tu_x = cv.CreateMat(rows, cols, cv.CV_32FC1) tu_y = cv.CreateMat(rows, cols, cv.CV_32FC1) tu_z = cv.CreateMat(rows, cols, cv.CV_32FC1) cv.Mul(t_c, u_x, tu_x) cv.Mul(t_c, u_y, tu_y) cv.Mul(t_c, u_z, tu_z) tu = cv.CreateMat(rows, cols, cv.CV_32FC3) cv.Merge(tu_x, tu_y, tu_z, None, tu) closest_point = cv.CreateMat(rows, cols, cv.CV_32FC3) cv.Add(P_0, su, closest_point) return closest_point
import cv im = cv.LoadImage('melon.jpg', cv.CV_LOAD_IMAGE_GRAYSCALE) mult_noise = cv.CreateImage((im.width, im.height), cv.IPL_DEPTH_32F, 1) cv.RandArr(cv.RNG(6), mult_noise, cv.CV_RAND_NORMAL, 1, 0.1) cv.Mul(im, mult_noise, im) cv.ShowImage("tree with speckle noise", im) cv.WaitKey(0)
def threshold_spock(image): cv.AdaptiveThreshold(image,spock_adaptive,255,cv.CV_ADAPTIVE_THRESH_MEAN_C,cv.CV_THRESH_BINARY_INV,103,25) cv.Mul(spock_adaptive,hls_h,spock) cv.Erode(spock,spock_eroded,None,2) cv.Dilate(spock_eroded,spock_dilated,None,6)
def threshold_paper(image): cv.AdaptiveThreshold(image,paper_adaptive,255,cv.CV_ADAPTIVE_THRESH_MEAN_C,cv.CV_THRESH_BINARY_INV,203,15) cv.Mul(paper_adaptive,hls_s,paper) cv.Erode(paper_adaptive,paper_eroded,None,3) cv.Dilate(paper_eroded,paper_dilated,None,4)
def mapper(self, _, line): photo_label = "photo:" if line[0:len(photo_label)] == photo_label: (label, id, secret, server) = line.split(' ') unique = "candidate_flickr_" + server + "_" + id + "_" + secret + "_m.jpg" url = 'http://static.flickr.com/' + server + "/" + id + "_" + secret + '_m.jpg' (filename, headers) = urllib.urlretrieve(url) width = self.options.processing_size height = width flickrImage = Image.open(filename) candidateImage = ImageOps.fit(flickrImage, (width, height), Image.ANTIALIAS, 0, (0.5, 0.5)) os.remove(filename) candidateImage.save(filename, "JPEG") #imageName=os.path.basename(sys.argv[1]) #dirName=`dirname $1` #echo Finding superpixels ... berkeleySegFile = '/tmp/' + unique + '.bse' #subprocess.call(['segment', '-image', filename, '-segfile', berkeleySegFile, '-numsuperpixels', str(number_of_superpixels)]) subprocess.call([ '/home/stolrsky/Desktop/LTPM/Libraries/BSE-1.2/segment', '-image', filename, '-segfile', berkeleySegFile, '-numsuperpixels', str(number_of_superpixels) ]) #echo Merging superpixels ... segmentationString = subprocess.check_output([ '/home/stolrsky/Desktop/LTPM/SuperPixelsToSegmentation/build/SuperPixelsToSegmentation', filename, berkeleySegFile, '0' ]) #print segmentationString segmentationFilePath = '/tmp/' + unique + '.realseg' segmentationFile = open(segmentationFilePath, 'w') segmentationFile.write(segmentationString) segmentationFile.close() cvImage = cv.CreateImageHeader(candidateImage.size, cv.IPL_DEPTH_8U, 3) cv.SetData(cvImage, candidateImage.tostring()) #cv.ShowImage(cvImage) #cv.WaitKey() polys = parseRealseg(segmentationString) #sys.stderr.write("polysString: " + segmentationString) sys.stderr.write("polysCount: " + str(len(polys))) p = 0 for poly in polys: segmentMask = cv.CreateImage(candidateImage.size, cv.IPL_DEPTH_8U, 3) cv.SetZero(segmentMask) cv.FillPoly(segmentMask, [poly], cv.RGB(1, 1, 1)) segment = cv.CreateImage(candidateImage.size, cv.IPL_DEPTH_8U, 3) cv.Mul(segmentMask, cvImage, segment) cv.SaveImage('/tmp/' + unique + '_' + str(p) + '.jpg', segment) p = p + 1 cv.SaveImage('/tmp/' + unique + '.jpg', cvImage) os.remove(berkeleySegFile) os.remove(filename) """ db = MySQLdb.connect(user="******", db="LTPM") c = db.cursor() LTPM_name = self.options.target_image_name candidate_name = unique rows = self.options.rows cols = self.options.cols for target_row in range(rows): for target_col in range(cols): target_realseg_path = self.get_target_seg_path(target_row, target_col) call = '/home/stolrsky/Desktop/LTPM/PlaceImage/build/PlaceImage ' + target_realseg_path + ' ' + segmentationFilePath #print call #score = subprocess.check_output(['/home/stolrsky/Desktop/LTPM/PlaceImage/build/PlaceImage', target_realseg_path, segmentationFilePath]) score = subprocess.check_output(shlex.split(call)) #print "scored " + unique + ' vs. ' + LTPM_name + ':' + str(target_row) + ':' + str(target_col) #print score #print " " # insert in db c.execute("insert into scores(LTPM_name, target_row, target_col, candidate_name, score) VALUES('"+LTPM_name+"', '"+str(target_row)+"', '"+str(target_col)+"', '"+url+"', "+str(score)+")") c.close() """ os.remove(segmentationFilePath) #(filename, headers) = urllib.urlretrieve(url, '/tmp/' + unique) #s3conn = S3Connection() #LTPM = Bucket(s3conn, 'ltpm') #image = Key(LTPM) #image.key = unique #image.set_contents_from_filename(filename) yield (unique, url)
def image_callback(data): global running, color if (running): #print "running" image = bridge.imgmsg_to_cv(data, "bgr8") #normalize image cv.Split(image, rgb_r, rgb_g, rgb_b, None) red_mean = cv2.mean(numpy.asarray(rgb_r[:, :])) cv.Div(src2=cv.fromarray(numpy.ones((480, 640))), src1=rgb_r, dst=scaled_r, scale=128 / red_mean[0]) green_mean = cv2.mean(numpy.asarray(rgb_g[:, :])) cv.Div(src2=cv.fromarray(numpy.ones((480, 640))), src1=rgb_g, dst=scaled_g, scale=128 / green_mean[0]) blue_mean = cv2.mean(numpy.asarray(rgb_b[:, :])) cv.Div(src2=cv.fromarray(numpy.ones((480, 640))), src1=rgb_b, dst=scaled_b, scale=128 / blue_mean[0]) cv.Merge(scaled_r, scaled_g, scaled_b, None, cv_image) cv.CvtColor(cv_image, hsv, cv.CV_BGR2HSV) cv.CvtColor(cv_image, lab, cv.CV_BGR2Lab) cv.CvtColor(cv_image, luv, cv.CV_BGR2Luv) cv.CvtColor(cv_image, hls, cv.CV_BGR2HLS) cv.CvtColor(cv_image, xyz, cv.CV_BGR2XYZ) cv.CvtColor(cv_image, ycrcb, cv.CV_BGR2YCrCb) cv.Split(hsv, hsv_h, hsv_s, hsv_v, None) cv.Split(cv_image, rgb_r, rgb_g, rgb_b, None) cv.Split(lab, lab_l, lab_a, lab_b, None) cv.Split(luv, luv_l, luv_u, luv_v, None) cv.Split(hls, hls_h, hls_l, hls_s, None) cv.Split(xyz, xyz_x, xyz_y, xyz_x, None) cv.Split(ycrcb, ycrcb_y, ycrcb_cr, ycrcb_cb, None) cv.Not(lab_a, a_not) cv.Sub(hsv_s, a_not, sa) cv.Sub(luv_u, hls_h, test) ''' cv.CvtColor(cv_image,gray,cv.CV_BGR2GRAY) cv.Smooth(gray,blurred_gray,cv.CV_GAUSSIAN,3,3) small = cv2.resize(numpy.asarray(ycrcb_cr[:,:]),(320,240) circles=cv2.HoughCircles(image=numpy.asarray(small[:,:]),method=cv.CV_HOUGH_GRADIENT,dp=1,minDist=1,param1=100,param2=60, minRadius=1,maxRadius=600) if not(circles is None): for i in circles: if (i[0][1] < 200): print "found circles",i,len(i) center = (i[0][0],i[0][1]) radius = i[0][2] cv.Circle(cv_image,center,radius,(1,1,0),2) ''' cv.Mul(test, red_adaptive, final) if (color == 'red'): threshold_red(sa) #threshold_red(ycrcb_cr) red_contours, _ = cv2.findContours( image=numpy.asarray(red_dilated_image[:, :]), mode=cv.CV_RETR_EXTERNAL, method=cv.CV_CHAIN_APPROX_SIMPLE) circles = extract_circles(red_contours, [1, 0, 0]) for x, y, radius in circles: cv.Circle(cv_image, (x, y), radius, [0, 0, 255], 3) elif (color == 'purple'): threshold_purple(lab_a) purple_contours, _ = cv2.findContours( image=numpy.asarray(purple_dilated_image[:, :]), mode=cv.CV_RETR_EXTERNAL, method=cv.CV_CHAIN_APPROX_SIMPLE) circles = extract_circles(purple_contours, [1, 0, 1]) for x, y, radius in circles: cv.Circle(cv_image, (x, y), radius, [255, 0, 255], 3) cv.ShowImage("red", red_adaptive) cv.ShowImage("purple", purple_adaptive) cv.ShowImage("camera feed", cv_image) cv.WaitKey(3)
def __SSIM(self, frame1, frame2): """ The equivalent of Zhou Wang's SSIM matlab code using OpenCV. from http://www.cns.nyu.edu/~zwang/files/research/ssim/index.html The measure is described in : "Image quality assessment: From error measurement to structural similarity" C++ code by Rabah Mehdi. http://mehdi.rabah.free.fr/SSIM C++ to Python translation and adaptation by Iñaki Úcar """ C1 = 6.5025 C2 = 58.5225 img1_temp = self.__array2cv(frame1) img2_temp = self.__array2cv(frame2) nChan = img1_temp.nChannels d = cv.IPL_DEPTH_32F size = img1_temp.width, img1_temp.height img1 = cv.CreateImage(size, d, nChan) img2 = cv.CreateImage(size, d, nChan) cv.Convert(img1_temp, img1) cv.Convert(img2_temp, img2) img1_sq = cv.CreateImage(size, d, nChan) img2_sq = cv.CreateImage(size, d, nChan) img1_img2 = cv.CreateImage(size, d, nChan) cv.Pow(img1, img1_sq, 2) cv.Pow(img2, img2_sq, 2) cv.Mul(img1, img2, img1_img2, 1) mu1 = cv.CreateImage(size, d, nChan) mu2 = cv.CreateImage(size, d, nChan) mu1_sq = cv.CreateImage(size, d, nChan) mu2_sq = cv.CreateImage(size, d, nChan) mu1_mu2 = cv.CreateImage(size, d, nChan) sigma1_sq = cv.CreateImage(size, d, nChan) sigma2_sq = cv.CreateImage(size, d, nChan) sigma12 = cv.CreateImage(size, d, nChan) temp1 = cv.CreateImage(size, d, nChan) temp2 = cv.CreateImage(size, d, nChan) temp3 = cv.CreateImage(size, d, nChan) ssim_map = cv.CreateImage(size, d, nChan) #/*************************** END INITS **********************************/ #// PRELIMINARY COMPUTING cv.Smooth(img1, mu1, cv.CV_GAUSSIAN, 11, 11, 1.5) cv.Smooth(img2, mu2, cv.CV_GAUSSIAN, 11, 11, 1.5) cv.Pow(mu1, mu1_sq, 2) cv.Pow(mu2, mu2_sq, 2) cv.Mul(mu1, mu2, mu1_mu2, 1) cv.Smooth(img1_sq, sigma1_sq, cv.CV_GAUSSIAN, 11, 11, 1.5) cv.AddWeighted(sigma1_sq, 1, mu1_sq, -1, 0, sigma1_sq) cv.Smooth(img2_sq, sigma2_sq, cv.CV_GAUSSIAN, 11, 11, 1.5) cv.AddWeighted(sigma2_sq, 1, mu2_sq, -1, 0, sigma2_sq) cv.Smooth(img1_img2, sigma12, cv.CV_GAUSSIAN, 11, 11, 1.5) cv.AddWeighted(sigma12, 1, mu1_mu2, -1, 0, sigma12) #////////////////////////////////////////////////////////////////////////// #// FORMULA #// (2*mu1_mu2 + C1) cv.Scale(mu1_mu2, temp1, 2) cv.AddS(temp1, C1, temp1) #// (2*sigma12 + C2) cv.Scale(sigma12, temp2, 2) cv.AddS(temp2, C2, temp2) #// ((2*mu1_mu2 + C1).*(2*sigma12 + C2)) cv.Mul(temp1, temp2, temp3, 1) #// (mu1_sq + mu2_sq + C1) cv.Add(mu1_sq, mu2_sq, temp1) cv.AddS(temp1, C1, temp1) #// (sigma1_sq + sigma2_sq + C2) cv.Add(sigma1_sq, sigma2_sq, temp2) cv.AddS(temp2, C2, temp2) #// ((mu1_sq + mu2_sq + C1).*(sigma1_sq + sigma2_sq + C2)) cv.Mul(temp1, temp2, temp1, 1) #// ((2*mu1_mu2 + C1).*(2*sigma12 + C2))./((mu1_sq + mu2_sq + C1).*(sigma1_sq + sigma2_sq + C2)) cv.Div(temp3, temp1, ssim_map, 1) index_scalar = cv.Avg(ssim_map) #// through observation, there is approximately #// 1% error max with the original matlab program return index_scalar[0]