def Magnitude(self, dx, dy, Mask=None, precise=True, method="cv"): '''Calculates the magnitude of the gradient using precise and fast approach''' dxconv = cv.CreateImage(cv.GetSize(dx), cv.IPL_DEPTH_32F, dx.channels) dyconv = cv.CreateImage(cv.GetSize(dx), cv.IPL_DEPTH_32F, dx.channels) dxdest = cv.CreateImage(cv.GetSize(dx), cv.IPL_DEPTH_32F, dx.channels) dydest = cv.CreateImage(cv.GetSize(dx), cv.IPL_DEPTH_32F, dx.channels) magdest = cv.CreateImage(cv.GetSize(dx), cv.IPL_DEPTH_32F, dx.channels) magnitude = cv.CreateImage(cv.GetSize(dx), cv.IPL_DEPTH_32F, dx.channels) magnitudetemp = cv.CreateImage(cv.GetSize(dx), cv.IPL_DEPTH_32F, dx.channels) zero = cv.CreateImage(cv.GetSize(dx), cv.IPL_DEPTH_32F, dx.channels) cv.Convert(dx, dxconv) cv.Convert(dy, dyconv) if precise: cv.Pow(dxconv, dxdest, 2) cv.Pow(dyconv, dydest, 2) cv.Add(dxdest, dydest, magdest) cv.Pow(magdest, magnitude, 1. / 2) else: #Add the |dx| + |dy| return None if method == "slow": size = cv.GetSize(magnitude) for x in range(size[0]): for y in range(size[1]): if Mask == None: pass elif Mask[y, x] > 0: pass else: magnitude[y, x] = 0 final = cv.CreateImage(cv.GetSize(dx), cv.IPL_DEPTH_8U, dx.channels) cv.ConvertScaleAbs(magnitude, final) else: cv.Add(zero, magnitude, magnitudetemp, Mask) final = cv.CreateImage(cv.GetSize(dx), cv.IPL_DEPTH_8U, dx.channels) cv.ConvertScaleAbs(magnitudetemp, final) if self.visualize: magnitude2 = cv.CreateImage(cv.GetSize(dy), cv.IPL_DEPTH_8U, 1) cv.EqualizeHist(final, magnitude2) while True: cv.NamedWindow("Magnitude") cv.ShowImage("Magnitude", magnitude2) c = cv.WaitKey(5) if c > 0: break cv.DestroyAllWindows() return final
def compute_distance_image(self, input_map): #Create necessary matrices dist_matrix=cv.CreateMat(input_map.height, input_map.width, cv.CV_32FC1) dist_img_gray=cv.CreateMat(input_map.height, input_map.width, cv.CV_8UC1) #Get the Euclidean distance from every free point in the map to the closest obstacle cv.DistTransform(input_map, dist_matrix) #To make the pikes of the distance function we use the square of the resulted value square_mat=cv.CreateMat(input_map.height, input_map.width, cv.CV_32FC1) cv.Pow(dist_matrix,square_mat, 3) #We normalize the values of all pixels to print the image. matrix = dist_matrix max_val = np.max(matrix) m=255/max_val for r in range(0, matrix.height): for c in range(0, matrix.width): dist_img_gray[r,c]=np.int(m*matrix[r,c]) return(dist_img_gray, dist_matrix)
def Process(image, pos_var, pos_w, pos_phase, pos_psi): global kernel_size if kernel_size % 2 == 0: kernel_size += 1 kernel = cv.CreateMat(kernel_size, kernel_size, cv.CV_32FC1) # kernelimg = cv.CreateImage((kernel_size,kernel_size),cv.IPL_DEPTH_32F,1) # big_kernelimg = cv.CreateImage((kernel_size*20,kernel_size*20),cv.IPL_DEPTH_32F,1) src = cv.CreateImage((image.width, image.height), cv.IPL_DEPTH_8U, 1) src_f = cv.CreateImage((image.width, image.height), cv.IPL_DEPTH_32F, 1) # src = image #cv.CvtColor(image,src,cv.CV_BGR2GRAY) #no conversion is needed if cv.GetElemType(image) == cv.CV_8UC3: cv.CvtColor(image, src, cv.CV_BGR2GRAY) else: src = image cv.ConvertScale(src, src_f, 1.0 / 255, 0) dest = cv.CloneImage(src_f) dest_mag = cv.CloneImage(src_f) var = pos_var / 10.0 w = pos_w / 10.0 phase = pos_phase * cv.CV_PI / 180.0 psi = cv.CV_PI * pos_psi / 180.0 cv.Zero(kernel) for x in range(-kernel_size / 2 + 1, kernel_size / 2 + 1): for y in range(-kernel_size / 2 + 1, kernel_size / 2 + 1): kernel_val = math.exp(-( (x * x) + (y * y)) / (2 * var)) * math.cos(w * x * math.cos(phase) + w * y * math.sin(phase) + psi) cv.Set2D(kernel, y + kernel_size / 2, x + kernel_size / 2, cv.Scalar(kernel_val)) # cv.Set2D(kernelimg,y+kernel_size/2,x+kernel_size/2,cv.Scalar(kernel_val/2+0.5)) cv.Filter2D(src_f, dest, kernel, (-1, -1)) # cv.Resize(kernelimg,big_kernelimg) cv.Pow(dest, dest_mag, 2) # return (dest_mag, big_kernelimg, dest) return (dest_mag, dest) # cv.ShowImage("Mag",dest_mag) # cv.ShowImage("Kernel",big_kernelimg) # cv.ShowImage("Process window",dest)
def on_trackbar(edge_thresh): cv.Threshold(gray, edge, float(edge_thresh), float(edge_thresh), cv.CV_THRESH_BINARY) #Distance transform cv.DistTransform(edge, dist, cv.CV_DIST_L2, cv.CV_DIST_MASK_5) cv.ConvertScale(dist, dist, 5000.0, 0) cv.Pow(dist, dist, 0.5) cv.ConvertScale(dist, dist32s, 1.0, 0.5) cv.AndS(dist32s, cv.ScalarAll(255), dist32s, None) cv.ConvertScale(dist32s, dist8u1, 1, 0) cv.ConvertScale(dist32s, dist32s, -1, 0) cv.AddS(dist32s, cv.ScalarAll(255), dist32s, None) cv.ConvertScale(dist32s, dist8u2, 1, 0) cv.Merge(dist8u1, dist8u2, dist8u2, None, dist8u) cv.ShowImage(wndname, dist8u)
cv.AbsDiff(im, im2, res) # Like minus for each pixel im(i) - im2(i) cv.ShowImage("AbsDiff", res) cv.Mul(im, im2, res) #Multiplie each pixels (almost white) cv.ShowImage("Mult", res) cv.Div(im, im2, res) #Values will be low so the image will likely to be almost black cv.ShowImage("Div", res) cv.And(im, im2, res) #Bit and for every pixels cv.ShowImage("And", res) cv.Or(im, im2, res) # Bit or for every pixels cv.ShowImage("Or", res) cv.Not(im, res) # Bit not of an image cv.ShowImage("Not", res) cv.Xor(im, im2, res) #Bit Xor cv.ShowImage("Xor", res) cv.Pow(im, res, 2) #Pow the each pixel with the given value cv.ShowImage("Pow", res) cv.Max(im, im2, res) #Maximum between two pixels #Same form Min MinS cv.ShowImage("Max", res) cv.WaitKey(0)
cv.Zero( tmp ) # no need to pad bottom part of dft_A with zeros because of # use nonzero_rows parameter in cv.FT() call below cv.DFT( dft_A, dft_A, cv.CV_DXT_FORWARD, complexInput.height ) cv.NamedWindow("win", 0) cv.NamedWindow("magnitude", 0) cv.ShowImage("win", im) # Split Fourier in real and imaginary parts cv.Split( dft_A, image_Re, image_Im, None, None ) # Compute the magnitude of the spectrum Mag = sqrt(Re^2 + Im^2) cv.Pow( image_Re, image_Re, 2.0) cv.Pow( image_Im, image_Im, 2.0) cv.Add( image_Re, image_Im, image_Re, None) cv.Pow( image_Re, image_Re, 0.5 ) # Compute log(1 + Mag) cv.AddS( image_Re, cv.ScalarAll(1.0), image_Re, None ) # 1 + Mag cv.Log( image_Re, image_Re ) # log(1 + Mag) # Rearrange the quadrants of Fourier image so that the origin is at # the image center cvShiftDFT( image_Re, image_Re ) min, max, pt1, pt2 = cv.MinMaxLoc(image_Re) cv.Scale(image_Re, image_Re, 1.0/(max-min), 1.0*(-min)/(max-min))