def normCrossCorrelation(img1, img2, pt0, pt1, status, winsize, method=cv2.cv.CV_TM_CCOEFF_NORMED): """ **SUMMARY** (Dev Zone) Calculates normalized cross correlation for every point. **PARAMETERS** img1 - Image 1. img2 - Image 2. pt0 - vector of points of img1 pt1 - vector of points of img2 status - Switch which point pairs should be calculated. if status[i] == 1 => match[i] is calculated. else match[i] = 0.0 winsize- Size of quadratic area around the point which is compared. method - Specifies the way how image regions are compared. see cv2.matchTemplate **RETURNS** match - Output: Array will contain ncc values. 0.0 if not calculated. """ nPts = len(pt0) match = np.zeros(nPts) for i in np.argwhere(status): i = i[0] patch1 = cv2.getRectSubPix(img1,(winsize,winsize),tuple(pt0[i])) patch2 = cv2.getRectSubPix(img2,(winsize,winsize),tuple(pt1[i])) match[i] = cv2.matchTemplate(patch1,patch2,method) return match
def _initData(self): """ Initialize the cluster centers and initial values of the pixel-wise cluster assignment and distance values. """ self.clusters = -1 * np.ones(self.img.shape[:2]) self.distances = self.FLT_MAX * np.ones(self.img.shape[:2]) centers = [] for i in xrange(self.step, self.width - self.step/2, self.step): for j in xrange(self.step, self.height - self.step/2, self.step): nc = self._findLocalMinimum(center=(i, j)) color = self.labimg[nc[1], nc[0]] center = [color[0], color[1], color[2], nc[0], nc[1]] centers.append(center) self.center_counts = np.zeros(len(centers)) self.centers = np.array(centers)
def normCrossCorrelation(img1, img2, pt0, pt1, status, winsize, method=cv2.cv.CV_TM_CCOEFF_NORMED): """ **SUMMARY** (Dev Zone) Calculates normalized cross correlation for every point. **PARAMETERS** img1 - Image 1. img2 - Image 2. pt0 - vector of points of img1 pt1 - vector of points of img2 status - Switch which point pairs should be calculated. if status[i] == 1 => match[i] is calculated. else match[i] = 0.0 winsize- Size of quadratic area around the point which is compared. method - Specifies the way how image regions are compared. see cv2.matchTemplate **RETURNS** match - Output: Array will contain ncc values. 0.0 if not calculated. """ nPts = len(pt0) match = np.zeros(nPts) for i in np.argwhere(status): i = i[0] patch1 = cv2.getRectSubPix(img1, (winsize, winsize), tuple(pt0[i])) patch2 = cv2.getRectSubPix(img2, (winsize, winsize), tuple(pt1[i])) match[i] = cv2.matchTemplate(patch1, patch2, method) return match
def createLowpassFilter(self, xCutoff, yCutoff=None, size=(64, 64)): """ **SUMMARY** Creates a lowpass filter of given size and order. **PARAMETERS** * *xCutoff* - int - horizontal cut off frequency - list - provide a list of three cut off frequencies to create a 3 channel filter * *yCutoff* - int - vertical cut off frequency - list - provide a list of three cut off frequencies to create a 3 channel filter * *size* - size of the filter (width, height) **RETURNS** DFT filter. **EXAMPLE** >>> flt = DFT.createLowpassFilter(xCutoff=75, size=(320, 280)) >>> flt = DFT.createLowpassFilter(xCutoff=[75], size=(320, 280)) >>> flt = DFT.createLowpassFilter(xCutoff=[75, 100, 120], size=(320, 280)) >>> flt = DFT.createLowpassFilter(xCutoff=75, yCutoff=35, size=(320, 280)) >>> flt = DFT.createLowpassFilter(xCutoff=[75], yCutoff=[35], size=(320, 280)) >>> flt = DFT.createLowpassFilter(xCutoff=[75, 100, 125], yCutoff=35, size=(320, 280)) >>> # yCutoff will be [35, 35, 35] >>> flt = DFT.createLowpassFilter(xCutoff=[75, 113, 124], yCutoff=[35, 45, 90], size=(320, 280)) >>> img = Image('lenna') >>> flt.applyFilter(img).show() """ if isinstance(xCutoff, list): if len(xCutoff) != 3 and len(xCutoff) != 1: warnings.warn("xCutoff list must be of size 3 or 1") return None if isinstance(yCutoff, list): if len(yCutoff) != 3 and len(yCutoff) != 1: warnings.warn("yCutoff list must be of size 3 or 1") return None if len(yCutoff) == 1: yCutoff = [yCutoff[0]] * len(xCutoff) else: yCutoff = [yCutoff] * len(xCutoff) stackedfilter = DFT() for xfreq, yfreq in zip(xCutoff, yCutoff): stackedfilter = stackedfilter._stackFilters( self.createLowpassFilter(xfreq, yfreq, size)) image = Image(stackedfilter._numpy) retVal = DFT(numpyarray=stackedfilter._numpy, image=image, xCutoffLow=xCutoff, yCutoffLow=yCutoff, channels=len(xCutoff), size=size, type=stackedfilter._type, order=self._order, frequency=stackedfilter._freqpass) return retVal w, h = size xCutoff = np.clip(int(xCutoff), 0, w / 2) if yCutoff is None: yCutoff = xCutoff yCutoff = np.clip(int(yCutoff), 0, h / 2) flt = np.zeros((w, h)) flt[0:xCutoff, 0:yCutoff] = 255 flt[0:xCutoff, h - yCutoff:h] = 255 flt[w - xCutoff:w, 0:yCutoff] = 255 flt[w - xCutoff:w, h - yCutoff:h] = 255 img = Image(flt) lowpassFilter = DFT(size=size, numpyarray=flt, image=img, type="Lowpass", xCutoffLow=xCutoff, yCutoffLow=yCutoff, frequency="lowpass") return lowpassFilter
def createLowpassFilter(self, xCutoff, yCutoff=None, size=(64, 64)): """ **SUMMARY** Creates a lowpass filter of given size and order. **PARAMETERS** * *xCutoff* - int - horizontal cut off frequency - list - provide a list of three cut off frequencies to create a 3 channel filter * *yCutoff* - int - vertical cut off frequency - list - provide a list of three cut off frequencies to create a 3 channel filter * *size* - size of the filter (width, height) **RETURNS** DFT filter. **EXAMPLE** >>> flt = DFT.createLowpassFilter(xCutoff=75, size=(320, 280)) >>> flt = DFT.createLowpassFilter(xCutoff=[75], size=(320, 280)) >>> flt = DFT.createLowpassFilter(xCutoff=[75, 100, 120], size=(320, 280)) >>> flt = DFT.createLowpassFilter(xCutoff=75, yCutoff=35, size=(320, 280)) >>> flt = DFT.createLowpassFilter(xCutoff=[75], yCutoff=[35], size=(320, 280)) >>> flt = DFT.createLowpassFilter(xCutoff=[75, 100, 125], yCutoff=35, size=(320, 280)) >>> # yCutoff will be [35, 35, 35] >>> flt = DFT.createLowpassFilter(xCutoff=[75, 113, 124], yCutoff=[35, 45, 90], size=(320, 280)) >>> img = Image('lenna') >>> flt.applyFilter(img).show() """ if isinstance(xCutoff, list): if len(xCutoff) != 3 and len(xCutoff) != 1: warnings.warn("xCutoff list must be of size 3 or 1") return None if isinstance(yCutoff, list): if len(yCutoff) != 3 and len(yCutoff) != 1: warnings.warn("yCutoff list must be of size 3 or 1") return None if len(yCutoff) == 1: yCutoff = [yCutoff[0]]*len(xCutoff) else: yCutoff = [yCutoff]*len(xCutoff) stackedfilter = DFT() for xfreq, yfreq in zip(xCutoff, yCutoff): stackedfilter = stackedfilter._stackFilters(self.createLowpassFilter(xfreq, yfreq, size)) image = Image(stackedfilter._numpy) retVal = DFT(numpyarray=stackedfilter._numpy, image=image, xCutoffLow=xCutoff, yCutoffLow=yCutoff, channels=len(xCutoff), size=size, type=stackedfilter._type, order=self._order, frequency=stackedfilter._freqpass) return retVal w, h = size xCutoff = np.clip(int(xCutoff), 0, w/2) if yCutoff is None: yCutoff = xCutoff yCutoff = np.clip(int(yCutoff), 0, h/2) flt = np.zeros((w, h)) flt[0:xCutoff, 0:yCutoff] = 255 flt[0:xCutoff, h-yCutoff:h] = 255 flt[w-xCutoff:w, 0:yCutoff] = 255 flt[w-xCutoff:w, h-yCutoff:h] = 255 img = Image(flt) lowpassFilter = DFT(size=size, numpyarray=flt, image=img, type="Lowpass", xCutoffLow=xCutoff, yCutoffLow=yCutoff, frequency="lowpass") return lowpassFilter