def display_lowintesity8u(self, image, maxintesity=None): if not maxintesity: (_, maxintesity, _, _) = cv.MinMaxLoc(image) # (minVal, maxVal, minLoc, maxLoc) display = cv.CloneImage(image) cv.Scale(image, display, 255 / maxintesity) cv.ShowImage("display", display)
def glyphRec(image): storage = cv.CreateMemStorage(0) contrast = cv.CreateImage(cv.GetSize(image), 8, 3) grey = cv.CreateImage(cv.GetSize(image), 8, 1) canny = cv.CreateImage(cv.GetSize(grey), cv.IPL_DEPTH_8U, 1) #increase contrast avg = cv.Avg(image) cv.AddS(image, cv.Scalar(-.5 * avg[0], -.5 * avg[1], -.5 * avg[2]), contrast) cv.Scale(contrast, contrast, 3) #make grayscale cv.CvtColor(contrast, grey, cv.CV_BGR2GRAY) #smooth cv.Smooth(grey, grey, cv.CV_GAUSSIAN, 3, 3) #edge detect cv.Canny(grey, canny, 20, 200, 3) #smooth again cv.Smooth(canny, canny, cv.CV_GAUSSIAN, 3, 3) #find lines lines = cv.HoughLines2(canny, storage, cv.CV_HOUGH_PROBABILISTIC, 3, math.pi / 180, 50, 150, 40) #find corners corners = getCorners(lines) #find quadrilaterals quad = findGlpyh(contrast, corners) if quad == None: return None drawQuad(image, quad) grid = readGlyph(image, quad) printGrid(grid) print '' toCoords(grid) return grid
def update(self, im=None): if im is not None: self.im = im cv.CalcArrHist([self.im], self.hist) (min_value, max_value, _, _) = cv.GetMinMaxHistValue(self.hist) cv.Scale(self.hist.bins, self.hist.bins, float(self.hist_image.height) / max_value, 0) cv.Set(self.hist_image, cv.ScalarAll(255)) bin_w = round(float(self.hist_image.width) / self.hist_size) for i in range(self.hist_size): cv.Rectangle(self.hist_image, (int(i * bin_w), self.hist_image.height), (int((i + 1) * bin_w), self.hist_image.height - cv.Round(self.hist.bins[i])), self.color, -1, 8, 0) cv.ShowImage(self.title, self.hist_image)
def OCVHistogram(frame, ranges=[[0, 256]], hist_size=64): """Create a histogram of given frame""" if frame.nChannels != 1: dest = OCVCopyGrayscale(frame) else: dest = frame hist_image = cv.CreateImage((dest.width, dest.height), 8, 1) hist = cv.CreateHist([hist_size], cv.CV_HIST_ARRAY, ranges, 1) cv.CalcArrHist([dest], hist) (min_value, max_value, _, _) = cv.GetMinMaxHistValue(hist) cv.Scale(hist.bins, hist.bins, float(hist_image.height) / max_value, 0) cv.Set(hist_image, cv.ScalarAll(255)) bin_w = round(float(hist_image.width) / hist_size) for i in range(hist_size): cv.Rectangle(hist_image, (int(i * bin_w), hist_image.height), (int( (i + 1) * bin_w), hist_image.height - cv.Round(hist.bins[i])), cv.ScalarAll(0), -1, 8, 0) return hist_image
def loadFilterEyeLocator(filename, ilog=None): ''' Loads the eye locator from a file.' ''' # open the file f = open(filename, 'rb') # Check the first line line = f.readline().strip() assert line == "CFEL" # read past the comment and copyright. f.readline() f.readline() # get the width and the height r, c = f.readline().split() r, c = int(r), int(c) # read in the left bounding rectangle x, y, w, h = f.readline().split() left_rect = (int(x), int(y), int(w), int(h)) # read in the right bounding rectangle x, y, w, h = f.readline().split() right_rect = (int(x), int(y), int(w), int(h)) # read the magic number magic_number = f.readline().strip() assert len(magic_number) == 4 magic_number = struct.unpack('i', magic_number)[0] # Read in the filter data lf = array.array('f') rf = array.array('f') lf.fromfile(f, r * c) rf.fromfile(f, r * c) # Test the magic number and byteswap if necessary. if magic_number == 0x41424344: pass elif magic_number == 0x44434241: lf.byteswap() rf.byteswap() else: raise ValueError("Bad Magic Number: Unknown byte ordering in file") # Create the left and right filters left_filter = cv.CreateMat(r, c, cv.CV_32F) right_filter = cv.CreateMat(r, c, cv.CV_32F) # Copy data into the left and right filters cv.SetData(left_filter, lf.tostring()) cv.SetData(right_filter, rf.tostring()) tmp = pv.OpenCVToNumpy(left_filter) t1 = tmp.mean() t2 = tmp.std() cv.Scale(left_filter, left_filter, 1.0 / t2, -t1 * 1.0 / t2) tmp = pv.OpenCVToNumpy(right_filter) t1 = tmp.mean() t2 = tmp.std() cv.Scale(right_filter, right_filter, 1.0 / t2, -t1 * 1.0 / t2) #tmp = pv.OpenCVToNumpy(left_filter) #print tmp.mean(),tmp.std() if ilog != None: #lf = cv.cvCreateMat(r,c,cv.CV_8U) #rf = cv.cvCreateMat(r,c,cv.CV_8U) lf = pv.OpenCVToNumpy(left_filter) rf = pv.OpenCVToNumpy(right_filter) lf = np.fft.fftshift(lf).transpose() rf = np.fft.fftshift(rf).transpose() ilog.log(pv.Image(lf), label="LeftEyeFilter") ilog.log(pv.Image(rf), label="RightEyeFilter") # Return the eye locator return OpenCVFilterEyeLocator(left_filter, right_filter, left_rect, right_rect)
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]