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 sobel(): cv.Smooth(src_image, dst_image, cv.CV_GAUSSIAN, 3, 3) src_gray = cv.CreateImage((src_image.width, src_image.height), 8, 1) dst_gray1 = cv.CreateImage((src_image.width, src_image.height), 8, 1) dst_gray = cv.CreateImage((src_image.width, src_image.height), 8, 1) cv.CvtColor(src_image, src_gray, cv.CV_BGR2GRAY) cv.Sobel(src_gray, dst_gray1, 0, 1, 3) cv.ConvertScaleAbs(dst_gray1, dst_gray1, 1, 0) cv.Sobel(src_gray, dst_gray, 1, 0, 3) cv.ConvertScaleAbs(dst_gray, dst_gray, 1, 0) cv.AddWeighted(dst_gray, 0.5, dst_gray1, 0.5, 0, dst_gray) cv.NamedWindow("Destination Image") cv.ShowImage("Destination Image", dst_gray) cv.WaitKey(0)
def sobelGradient(self, image): '''Calculates the gradient in x and y direction using Sobel mask using default 3x3 filter''' if self.image_check(image) < 0: return -1 gsimage = cv.CreateImage(cv.GetSize(image), cv.IPL_DEPTH_8U, 1) if image.channels > 1: temp = cv.CreateImage(cv.GetSize(image), cv.IPL_DEPTH_8U, 1) cv.CvtColor(image, temp, cv.CV_BGR2GRAY) gsimage = temp else: gsimage = image #smoothing image #Creating empty images for dx,dy and temporary 16 bit dx16 and dy16 dy16 = cv.CreateImage(cv.GetSize(gsimage), cv.IPL_DEPTH_16S, 1) dx16 = cv.CreateImage(cv.GetSize(gsimage), cv.IPL_DEPTH_16S, 1) dx = cv.CreateImage(cv.GetSize(gsimage), cv.IPL_DEPTH_8U, 1) dy = cv.CreateImage(cv.GetSize(gsimage), cv.IPL_DEPTH_8U, 1) #Convolving sobel mask to get gradient of dx and dy cv.Sobel(gsimage, dy16, 0, 1, apertureSize=3) cv.Sobel(gsimage, dx16, 1, 0, apertureSize=3) cv.ConvertScaleAbs(dx16, dx) cv.ConvertScaleAbs(dy16, dy) if self.visualize: while True: cv.NamedWindow("Original") cv.ShowImage("Original", gsimage) cv.NamedWindow("Sobelx") cv.ShowImage("Sobelx", dx) cv.NamedWindow("Sobely") cv.ShowImage("Sobely", dy) c = cv.WaitKey(5) if c > 0: break cv.DestroyAllWindows() return (dx16, dy16)
def _mixImageReplace(self, wipeSettings, level, image1, image2, mixMat): if(level < 0.99): wipeMode, wipePostMix, wipeConfig = wipeSettings if((wipeMode == WipeMode.Fade) or (wipeMode == WipeMode.Default)): if(image1 != None): cv.ConvertScaleAbs(image2, image2, level, 0.0) cv.ConvertScaleAbs(image1, image1, 1.0 - level, 0.0) cv.Add(image1, image2, mixMat) return mixMat else: cv.ConvertScaleAbs(image2, mixMat, level, 0.0) return mixMat else: if(wipePostMix == False): image2, mixMask = self._wipeImage(wipeMode, wipeConfig, level, image2, None, mixMat, False) if(image1 == None): return image2 cv.Copy(image2, image1, mixMask) return image1 else: return self._wipeMix(wipeMode, wipeConfig, level, image1, image2, mixMat) return image2
def _mixImageMultiply(self, wipeSettings, level, image1, image2, mixMat): if(level < 0.99): wipeMode, wipePostMix, wipeConfig = wipeSettings if((wipeMode == WipeMode.Fade) or (wipeMode == WipeMode.Default)): cv.ConvertScaleAbs(image2, mixMat, 1.0, 256*(1.0-level)) cv.Mul(image1, mixMat, mixMat, 0.004) return mixMat else: if(wipePostMix == False): image2, _ = self._wipeImage(wipeMode, wipeConfig, level, image2, None, mixMat, True) cv.Mul(image1, image2, mixMat, 0.004) return mixMat else: cv.Mul(image1, image2, mixMat, 0.004) return self._wipeMix(wipeMode, wipeConfig, level, image1, mixMat, image2) cv.Mul(image1, image2, mixMat, 0.004) return mixMat
def _mixImageAdd(self, wipeSettings, level, image1, image2, mixMat): if(level < 0.99): wipeMode, wipePostMix, wipeConfig = wipeSettings if((wipeMode == WipeMode.Fade) or (wipeMode == WipeMode.Default)): cv.ConvertScaleAbs(image2, image2, level, 0.0) cv.Add(image1, image2, mixMat) return mixMat else: if(wipePostMix == False): image2, _ = self._wipeImage(wipeMode, wipeConfig, level, image2, None, mixMat, False) cv.Add(image1, image2, mixMat) return mixMat else: cv.Add(image1, image2, mixMat) return self._wipeMix(wipeMode, wipeConfig, level, image1, mixMat, image2) cv.Add(image1, image2, mixMat) return mixMat
def _mixImageSubtract(self, wipeSettings, level, image1, image2, mixMat): if(level < 0.99): wipeMode, wipePostMix, wipeConfig = wipeSettings # print "DEBUG pcn: mixImageSubtract: wipeSettings: " + str(wipeSettings) if((wipeMode == WipeMode.Fade) or (wipeMode == WipeMode.Default)): cv.ConvertScaleAbs(image2, image2, level, 0.0) cv.Sub(image1, image2, mixMat) return mixMat else: if(wipePostMix == False): image2, _ = self._wipeImage(wipeMode, wipeConfig, level, image2, None, mixMat, False) cv.Sub(image1, image2, mixMat) return mixMat else: cv.Sub(image1, image2, mixMat) return self._wipeMix(wipeMode, wipeConfig, level, image1, mixMat, image2) cv.Sub(image1, image2, mixMat) return mixMat
elif len(sys.argv) == 2: capture = cv.CreateFileCapture(sys.argv[1]) if not capture: print "Could not initialize capturing..." sys.exit(-1) cv.NamedWindow("Laplacian", 1) while True: frame = cv.QueryFrame(capture) if frame: if not laplace: planes = [cv.CreateImage((frame.width, frame.height), 8, 1) for i in range(3)] laplace = cv.CreateImage((frame.width, frame.height), cv.IPL_DEPTH_16S, 1) colorlaplace = cv.CreateImage((frame.width, frame.height), 8, 3) cv.Split(frame, planes[0], planes[1], planes[2], None) for plane in planes: cv.Laplace(plane, laplace, 3) cv.ConvertScaleAbs(laplace, plane, 1, 0) cv.Merge(planes[0], planes[1], planes[2], None, colorlaplace) cv.ShowImage("Laplacian", colorlaplace) if cv.WaitKey(10) != -1: break cv.DestroyWindow("Laplacian")
def absnorm8(im, im8): """ im may be any single-channel image type. Return an 8-bit version, absolute value, normalized so that max is 255 """ (minVal, maxVal, _, _) = cv.MinMaxLoc(im) cv.ConvertScaleAbs(im, im8, 255 / max(abs(minVal), abs(maxVal)), 0) return im8
def tangent(self, dx, dy, Mask=None, method="cv"): '''This function calculates the gradient orientation of each pixel that is in Mask''' tangent = cv.CreateImage(cv.GetSize(dx), cv.IPL_DEPTH_8U, 1) divSize = cv.GetSize(dx) tangent16U = cv.CreateImage(divSize, cv.IPL_DEPTH_32F, 1) cv.SetZero(tangent16U) if method == "slow": for x in range(divSize[0]): for y in range(divSize[1]): if Mask == None: tang = math.atan2(dy[y, x], dx[y, x]) * self.constant tangent16U[y, x] = int(tang) elif Mask[y, x] > 0: tang = math.atan2(dy[y, x], dx[y, x]) * self.constant tangent16U[y, x] = int(tang) elif Mask[y, x] == 0: tangent16U[y, x] = 0 elif method == "cv": #Calculated the arctan2 which give -pi to pi range #I create numpy arrays then reshape them in to 1 Dimesnion. #Next, I calculated arctan2 and change the range to 0-2pi and make it in degrees #I reshape back to picture format and return the picture #Numpy formatting (width, height) = cv.GetSize(dx) matdx = cv.CreateMat(height, width, cv.CV_16SC1) matdy = cv.CreateMat(height, width, cv.CV_16SC1) cv.SetZero(matdx) cv.SetZero(matdy) cv.Copy(dx, matdx, Mask) cv.Copy(dy, matdy, Mask) a = numpy.asarray(matdx) b = numpy.asarray(matdy) #Reshaping to one dimension ar = numpy.reshape(a, a.size) br = numpy.reshape(b, b.size) #Calculating Arc Tangent with quadrant information c = numpy.arctan2(br, ar) #Turning it to -180 to 180 range z = numpy.multiply(c, self.constant) result = z.astype(numpy.int32) result[result < 0] += 360 tang = numpy.reshape(result, (height, width)) tang = tang.astype(numpy.float32) mat = cv.fromarray(tang) cv.Copy(mat, tangent16U) else: dxTemp = cv.CreateImage(cv.GetSize(dx), cv.IPL_DEPTH_16S, 1) dyTemp = cv.CreateImage(cv.GetSize(dx), cv.IPL_DEPTH_16S, 1) zero = cv.CreateImage(cv.GetSize(dx), cv.IPL_DEPTH_16S, 1) cv.Add(zero, dx, dxTemp, Mask) cv.Add(zero, dy, dyTemp, Mask) dx = dxTemp dy = dyTemp for x in range(divSize[0]): for y in range(divSize[1]): if Mask[y, x] == 0: tangent16U[y, x] = 0 continue tang = math.atan2(dy[y, x], dx[y, x]) * self.constant tangent16U[y, x] = int(tang) if self.visualize: #tangent2 = cv.CreateImage(cv.GetSize(dy), cv.CV_16SC1, 1) cv.ConvertScaleAbs(tangent16U, tangent) cv.EqualizeHist(tangent, tangent) while True: cv.NamedWindow("Tangent") cv.ShowImage("Tangent", tangent) c = cv.WaitKey(5) if c > 0: break cv.DestroyAllWindows() return tangent16U