def getSpectra(imgList): """ Calculates the fourier transforms (against time) of all pixels in imgList. imgList is a list of tuples (datetime,image). Creates a 2 dimensional array, where one dimension is the pixel values in the image, and the other is time, then calculates the fourier transform. To give the frequency contributions of the values in each pixel. """ (width, height) = cv.GetSize(imgList[0][1]) nPixels = width * height print "Image Size = (%d x %d) - %d pixels. Number of Images = %d" \ % (width,height,nPixels,len(imgList)) # Create a matrix with pixel values in the y direction, and time (frame no) # in the x direction. This means we can do an FFT on each row to get # frequency components of each pixel. dataMat = cv.CreateMat(nPixels, len(imgList), cv.CV_32FC1) for frameNo in range(len(imgList)): for y in range(height - 1): for x in range(width - 1): pixelNo = y * width + x pixelVal = float(imgList[frameNo][1][y, x] / 255.0) dataMat[pixelNo, frameNo] = pixelVal cv.ShowImage(window3, dataMat) fftMat = cv.CreateMat(nPixels, len(imgList), cv.CV_32FC1) (a, fftMax, b, c) = cv.MinMaxLoc(fftMat) #print "fftMax=%f" % (fftMax) fftMat_int = cv.CreateMat(nPixels, len(imgList), cv.CV_8UC1) cv.DFT(dataMat, fftMat, cv.CV_DXT_ROWS) #cv.Split(fftMat,complexParts) #cv.magnitude(complexParts[0],complexParts[1],complexParts[0]) #fftMat = complexParts[0] cv.ConvertScale(fftMat, fftMat_int, 1000) cv.ShowImage(window4, fftMat_int) # Apply frequency filter to FFT data for x in range(0, FFT_CHAN_MIN): for y in range(0, nPixels): fftMat[y, x] = 0.0 for x in range(FFT_CHAN_MAX, len(imgList) - 1): for y in range(0, nPixels): fftMat[y, x] = 0.0 (a, fftMax, b, c) = cv.MinMaxLoc(fftMat) print "fftMax=%f (filtered region)" % (fftMax)
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 normalize(self, image): """ scale image to max of 255 """ minVal, maxVal, minLoc, maxLoc = cv.MinMaxLoc(image) if maxVal > 0: scaler = 255 / maxVal cv.ConvertScale(image, image, scale=scaler, shift=0.0) return image
def extract_bright(grey_img, histogram=False): """ Extracts brightest part of the image. Expected to be the LEDs (provided that there is a dark background) Returns a Thresholded image histgram defines if we use the hist calculation to find the best margin """ ## Searches for image maximum (brightest pixel) # We expect the LEDs to be brighter than the rest of the image [minVal, maxVal, minLoc, maxLoc] = cv.MinMaxLoc(grey_img) print "Brightest pixel val is %d" % (maxVal) #We retrieve only the brightest part of the image # Here is use a fixed margin (80%), but you can use hist to enhance this one if 0: ## Histogram may be used to wisely define the margin # We expect a huge spike corresponding to the mean of the background # and another smaller spike of bright values (the LEDs) hist = grey_histogram(img, nBins=64) [hminValue, hmaxValue, hminIdx, hmaxIdx] = cv.GetMinMaxHistValue(hist) margin = 0 # statistics to be calculated using hist data else: margin = 0.8 thresh = int(maxVal * margin) # in pix value to be extracted print "Threshold is defined as %d" % (thresh) thresh_img = cv.CreateImage(cv.GetSize(img), img.depth, 1) cv.Threshold(grey_img, thresh_img, thresh, 255, cv.CV_THRESH_BINARY) return thresh_img
def detect(templatePath, targetPath, threshold=0.8): """ targetPathで指定される画像からtemplatePathで指定される画像の座標を返す @param templatePath {String} 探索対象画像 @param targetPath {String} 探索範囲画像 @param threshold=0.8 {Number} マッチ度の閾値 @return {Tuple} or False テンプレートの中心座標。threshold以上の座標がなければFalse """ target = cv.LoadImage(targetPath) template = cv.LoadImage(templatePath) dstSize = (target.width - template.width + 1, target.height - template.height + 1) dstImg = cv.CreateImage(dstSize, cv.IPL_DEPTH_32F, 1) cv.MatchTemplate(target, template, dstImg, cv.CV_TM_CCOEFF_NORMED) minMaxLoc = cv.MinMaxLoc(dstImg) logger.debug("%s, %.2f%%" % (templatePath, minMaxLoc[1] * 100)) if minMaxLoc[1] < threshold: return False maxLoc = minMaxLoc[3] x = maxLoc[0] + template.width / 2 y = maxLoc[1] + template.height / 2 return (x, y)
def run(im, headInfo): (cam, head) = headInfo # convert the image im = convertImage(im) #cv.SaveImage('a.jpg', im) # filter the image im = filterImage(im) #cv.SaveImage('b.jpg', im) # blur the image cv.Smooth(im, im, cv.CV_BLUR, 5, 5) #cv.SaveImage('c.jpg', im) # find the max value in the image (minVal, maxValue, minLoc, maxLocation) = cv.MinMaxLoc(im) #print maxValue/256.0 # if the maxValue isn't hight enough return 'None' if maxValue / 256.0 < 0.4: return None # calculate the angles to the ball #(xAngle, yAngle) = calcAngles((xcoord, ycoord ), cam) # calculate the position of the ball position = calcPosition(maxLocation, cam) (xPos, yPos, xAngle, yAngle) = position #print position #track(position) return (xPos, yPos)
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 scale_32f_image(image): ''' Scales the given cv.IPL_DEPTH_32F type image to an 8 bit image so that the smallest value maps to 0 and the largest maps to 255. Used for displaying debugging images. Processes each channel separately, which can produce some useful, but esoteric results. ''' if image.depth != cv.IPL_DEPTH_32F: return image result = cv.CreateImage(cv.GetSize(image), 8, image.channels) channel_image = cv.CreateImage(cv.GetSize(image), cv.IPL_DEPTH_32F, 1) channel_scaled = cv.CreateImage(cv.GetSize(image), 8, 1) for channel_num in xrange(1, image.channels + 1): cv.SetImageCOI(image, channel_num) cv.Copy(image, channel_image) minmaxloc = cv.MinMaxLoc(channel_image) minimum = minmaxloc[0] maximum = minmaxloc[1] if maximum - minimum > 0: cv.ConvertScale(channel_image, channel_scaled, 255 / (maximum - minimum), -255 / (maximum - minimum) * minimum) else: cv.ConvertScale(channel_image, channel_scaled, 0, -255 / minimum) cv.SetImageCOI(result, channel_num) cv.Copy(channel_scaled, result) cv.SetImageCOI(image, 0) cv.SetImageCOI(result, 0) return result
def find(template, image): image_size = cv.GetSize(image) template_size = cv.GetSize(template) result_size = [s[0] - s[1] + 1 for s in zip(image_size, template_size)] result = cv.CreateImage(result_size, cv.IPL_DEPTH_32F, 1) cv.CV_BGR2RGB cv.MatchTemplate(image, template, result, cv.CV_TM_CCOEFF_NORMED) min_val, max_val, min_loc, max_loc = cv.MinMaxLoc(result) x, y = max_loc return {'score': max_val, 'x': x, 'y': y}
def logging(self): u'''ログを取る''' target = self._cvmat digits_sieve = DigitsSieve() for template in self._templates: if not template.result: # マッチング結果保存用領域の準備 template.result = cv.CreateImage( (target.width - template.image.width + 1, target.height - template.image.height + 1), cv.IPL_DEPTH_32F, 1, ) cv.MatchTemplate(target, template.image, template.result, config.logging.match_method) # 数値の読み取り minVal, maxVal, minLoc, maxLoc = cv.MinMaxLoc(template.result) while maxVal > config.logging.match_threshold: # 検出された数値情報の保持 digits_sieve.push( A( number=template.number, x=maxLoc[0], y=maxLoc[1], width=template.image.width, height=template.image.height, score=maxVal, )) # 現在の位置周辺のスコアをクリアし、次にスコアの高い位置を取得する SetReal2DAround(template.result, maxLoc, config.logging.match_exclusion_size, 0.0) minVal, maxVal, minLoc, maxLoc = cv.MinMaxLoc(template.result) value = digits_sieve.getValue() if value is not None: self._log.append(value) self.setValue(value) self._textarea.insert(tk.END, '%d\n' % value) self._textarea.see(tk.END)
def locateEyes(self, image_tile): ''' @param image_tile: is an 32-bit gray scale opencv image tile of a face that is the same size as the filter @type image_tile: 8-bit gray scale opencv image @returns: a tuple consisting of the location of the left and right eyes (opencv 2D points), and the complex correlation plain output @raises AssertionError: is raised if the image is not 8-bit or not the same size as the filter ''' self.correlate(image_tile) leye = cv.MinMaxLoc(self.left_roi)[3] leye = (self.left_rect[0] + leye[0], self.left_rect[1] + leye[1]) reye = cv.MinMaxLoc(self.right_roi)[3] reye = (self.right_rect[0] + reye[0], self.right_rect[1] + reye[1]) return leye, reye, self.left_corr, self.right_corr
def show_depth(): global threshold global current_depth depth, timestamp = freenect.sync_get_depth() viewable = frame_convert.full_depth_cv(depth) cv.Rectangle(viewable, sense_pt1, sense_pt2, (255, 0, 0), 1) cv.ShowImage('Depth', viewable) roi = cv.GetSubRect(frame_convert.raw_depth_cv(depth), sense_rect) pix = cv.Avg(roi)[0] (roimin, roimax, a, b) = cv.MinMaxLoc(roi) if roimax < 1090: dist = 350.0 / (1091 - pix) print "%f %i %i" % (dist, roimin, roimax) else: print "XX"
def bestmatch(A, B): """ Tries to find the image A within the (larger) image B. For instance, A could be a voting target, and B could be a contest. Input: cvMat A: Patch to search for cvMat B: Image to search over Output: ((x,y), s_mat), location on B of the best match for A. """ w_A, h_A = A.cols, A.rows w_B, h_B = B.cols, B.rows s_mat = cv.CreateMat(h_B - h_A + 1, w_B - w_A + 1, cv.CV_32F) cv.MatchTemplate(B, A, s_mat, cv.CV_TM_CCOEFF_NORMED) minResp, maxResp, minLoc, maxLoc = cv.MinMaxLoc(s_mat) return maxLoc, s_mat
def find_col_x1(I, Icol, bb, K=3, AX=0.2, AY=0.2, T=0.9): """ Tries to find the column of marks on I, using ICOL as a ref. image in template matching. """ roi_prev = cv.GetImageROI(I) shift_roi(I, bb[0], bb[1], bb[2] - bb[0], bb[3] - bb[1]) w_A, h_A = cv.GetSize(Icol) w_I, h_I = cv.GetSize(I) M = cv.CreateMat(h_I - h_A + 1, w_I - w_A + 1, cv.CV_32F) cv.MatchTemplate(I, Icol, M, cv.CV_TM_CCOEFF_NORMED) if DEBUG_SAVEIMGS: M_np = np.array(M) import scipy.misc print_dbg("<><><><> Saving '_Mbase.png' <><><><>") cv.SaveImage("_Mbase.png", I) print_dbg("<><><><> Saving '_M.png' <><><><>") scipy.misc.imsave("_M.png", M) pdb.set_trace() cv.SetImageROI(I, roi_prev) i = 0 xs = [] _xamt, _yamt = int(round(AX * w_A)), int(round(AY * h_A)) while i < K: minResp, maxResp, minLoc, maxLoc = cv.MinMaxLoc(M) if maxResp < T: break x, y = maxLoc # Find the /leftmost/ match: don't find a match in the middle # of a column. while M[y, x] >= T: x -= 1 xs.append((x + bb[0])) _x1 = max(1, x - _xamt) _x2 = max(1, x + _xamt) _y1 = max(1, y - _yamt) _y2 = max(1, y + _yamt) M[_y1:_y2, _x1:_x2] = -1.0 i += 1 if not xs: return None elif len(xs) == 1: return xs[0] return np.median(xs)
def hue_histogram_as_image(hist): histimg_hsv = cv.CreateImage((640, 480), 8, 3) mybins = cv.CloneMatND(hist.bins) cv.Log(mybins, mybins) (_, hi, _, _) = cv.MinMaxLoc(mybins) cv.ConvertScale(mybins, mybins, 255. / hi) w, h = cv.GetSize(histimg_hsv) hdims = int(cv.GetDims(mybins)[0]) for x in range(w): xh = (180 * x) / (w - 1) # hue sweeps from 0-180 across the image val = int(mybins[int(hdims * x / w)] * h / 255) cv.Rectangle(histimg_hsv, (x, 0), (x, h - val), (xh, 255, 64), -1) cv.Rectangle(histimg_hsv, (x, h - val), (x, h), (xh, 255, 255), -1) histimg = cv.CreateImage((320, 200), 8, 3) cv.CvtColor(histimg_hsv, histimg, cv.CV_HSV2BGR) return histimg
def scale_32f_image(image): result = cv.CreateImage(cv.GetSize(image), 8, image.channels) channel_image = cv.CreateImage(cv.GetSize(image), cv.IPL_DEPTH_32F, 1) channel_scaled = cv.CreateImage(cv.GetSize(image), 8, 1) print "CHANNELS:", image.channels for channel_num in xrange(1, image.channels + 1): cv.SetImageCOI(image, channel_num) cv.Copy(image, channel_image) maximum = cv.MinMaxLoc(channel_image)[1] print "MAXIMUM:", maximum cv.ConvertScale(channel_image, channel_scaled, 255 / maximum) cv.SetImageCOI(result, channel_num) cv.Copy(channel_scaled, result) cv.SetImageCOI(image, 0) cv.SetImageCOI(result, 0) return result
def mkgray(self, msg): """ Convert a message into a 8-bit 1 channel monochrome OpenCV image """ # as cv_bridge automatically scales, we need to remove that behavior if msg.encoding.endswith('16'): mono16 = self.br.imgmsg_to_cv(msg, "mono16") mono8 = cv.CreateMat(mono16.rows, mono16.cols, cv.CV_8UC1) cv.ConvertScale(mono16, mono8) return mono8 elif 'FC1' in msg.encoding: # floating point image handling img = self.br.imgmsg_to_cv(msg, "passthrough") mono_img = cv.CreateMat(img.rows, img.cols, cv.CV_8UC1) _, max_val, _, _ = cv.MinMaxLoc(img) scale = 255.0 / max_val if max_val > 0 else 1.0 cv.ConvertScale(img, mono_img, scale) return mono_img else: return self.br.imgmsg_to_cv(msg, "mono8")
def points_max_cols(img, color='green', threshold=240): """ Read maximum pixel value in one color channel for each row """ w, h = cv.GetSize(img) xy = list() gray = cv.CreateImage((w, h), cv.IPL_DEPTH_8U, 1) if color == 'red': cv.Split(img, None, None, gray, None) else: cv.Split(img, None, gray, None, None) for i in range(0, h): row = cv.GetRow(gray, i) minv, maxv, minl, maxl = cv.MinMaxLoc(row) if maxv > threshold: xy.append((maxl[0], i)) return xy
def matching(self,template,time): capture=cv.CaptureFromCAM(0) image=cv.QueryFrame(capture) # writer=cv.CreateVideoWriter("output.avi", 0, 15, cv.GetSize(image), 1) count=0 w,h = cv.GetSize(template) W,H = cv.GetSize(image) width = W-w+1 height = H-h+1 while count<time: image=cv.QueryFrame(capture) result = cv.CreateImage((width, height), 32, cv.CV_TM_SQDIFF) cv.MatchTemplate(template, image, result, cv.CV_TM_SQDIFF) print result (min_x,max_y,minloc,maxloc)=cv.MinMaxLoc(result) (x,y)=minloc cv.Rectangle(image,(int(x),int(y)),(int(x)+w,int(y)+h),(255,255,255),1,0) print minloc # cv.WriteFrame(writer, image) cv.WaitKey(1) cv.ShowImage('Image_Window',image) count+=1 cv.DestroyWindow('Image_Window')
def setup_camshift(roi_selection, img): hist = cv.CreateHist([180], cv.CV_HIST_ARRAY, [(0, 180)], 1) # backproject_mode = False histimg_hsv = cv.CreateImage((320, 200), 8, 3) mybins = cv.CloneMatND(hist.bins) cv.Log(mybins, mybins) (_, hi, _, _) = cv.MinMaxLoc(mybins) cv.ConvertScale(mybins, mybins, 255. / hi) w, h = cv.GetSize(histimg_hsv) hdims = cv.GetDims(mybins)[0] for x in range(w): xh = (180 * x) / (w - 1) # hue sweeps from 0-180 across the image val = int(mybins[int(hdims * x / w)] * h / 255) cv.Rectangle(histimg_hsv, (x, 0), (x, h - val), (xh, 255, 64), -1) cv.Rectangle(histimg_hsv, (x, h - val), (x, h), (xh, 255, 255), -1) histimg = cv.CreateImage((320, 200), 8, 3) cv.CvtColor(histimg_hsv, histimg, cv.CV_HSV2BGR) # making_selection # print roi_selection[0] # print roi_selection[1] point1 = roi_selection[0] point2 = roi_selection[1] xmin = point1[0] ymin = point1[1] xmax = point2[0] ymax = point2[1] widthx = xmax - xmin heighty = ymax - ymin selection = (xmin, ymin, widthx, heighty) return selection, hist
def findBestTemplateMatch(tplList, img): """ Compares img against a list of templates. tplList is a list of string filenames of template images Returns a tuple (num, suit) if a template is suitably matched or None if not """ minTpl = 200 # arbitrarily large number tString = None for t in tplList: tpl = cv.LoadImage(t) w = img.width - tpl.width + 1 h = img.height - tpl.height + 1 result = cv.CreateImage((w, h), 32, 1) cv.MatchTemplate(img, tpl, result, cv.CV_TM_SQDIFF_NORMED) (minVal, maxVal, minLoc, maxLoc) = cv.MinMaxLoc(result) #print t #print (minVal, maxVal, minLoc, maxLoc) # 0.2 found by experiment (the non-card images end up being around # 0.25 - 0.28, and all the card images were much around 0.08 and less if minVal < minTpl and minVal < 0.2: minTpl = minVal tString = t #print minTpl, tString #cv.ShowImage("win", img) #cv.ShowImage("win2", result) #cv.WaitKey(0) return tString
y = 0 R = [None] * 11 G = [None] * 11 B = [None] * 11 alpha = [None] * 11 sump = 0 while (1): telly = cv.QueryFrame(capo) #bingo=cv.CloneImage(telly) #red=cv.CreateImage(cv.GetSize(bingo),8,1) #bingo=cv.CloneImage(telly) cv.Zero(red) if (q == 9): break cv.Split(telly, None, None, red, None) (_, _, _, jingo) = cv.MinMaxLoc(red, None) (x, y) = jingo points[q] = cv.Get2D(mas, y, x) (B[q], G[q], R[q], alpha[q]) = points[q] print "Points scored in shot" print q + 1 print "is:" print R[q] print G[q] print B[q] print alpha[q] cv.Circle(rora, jingo, 3, cv.Scalar(0, 0, 255), -1, 8) cv.PutText(rora, "YOUR SCORE IN SHOT " + str(q) + " IS: " + str(int(G[q])), (25, 600 + (35 * q)), font, cv.Scalar(0, 0, 255)) points[q] sump = sump + G[q]
def show(self): """ Process and show the current frame """ source = cv.LoadImage(self.files[self.index]) width, height = cv.GetSize(source) center = (width / 2) + self.offset cv.Line(source, (center, 0), (center, height), (0, 255, 0), 1) if self.roi: x, y, a, b = self.roi print self.roi width, height = ((a - x), (b - y)) mask = cv.CreateImage((width, height), cv.IPL_DEPTH_8U, 1) cv.SetImageROI(source, (x, y, width, height)) cv.Split(source, None, None, mask, None) gray = cv.CloneImage(mask) cv.InRangeS(mask, self.thresholdMin, self.thresholdMax, mask) cv.And(mask, gray, gray) line = [] points = [] for i in range(0, height - 1): row = cv.GetRow(gray, i) minVal, minLoc, maxLoc, maxVal = cv.MinMaxLoc(row) y = i x = maxVal[0] point = (0, 0, height - i) if x > 0: line.append((x, y)) s = x / sin(radians(self.camAngle)) x = s * cos(self.angles[self.index]) z = height - y y = s * sin(self.angles[self.index]) point = (round(x, 2), round(y, 2), z) points.append(point) cv.PolyLine(source, [line], False, (255, 0, 0), 2, 8) cv.ResetImageROI(source) x, y, a, b = self.roi cv.Rectangle(source, (int(x), int(y)), (int(a), int(b)), (255.0, 255, 255, 0)) if self.roi: x, y, a, b = self.roi width, height = ((a - x), (b - y)) mask = cv.CreateImage((width, height), cv.IPL_DEPTH_8U, 1) cv.SetImageROI( source, (x - width, y, width, height)) # moves roi to the left cv.Split(source, None, None, mask, None) gray = cv.CloneImage(mask) cv.InRangeS(mask, self.thresholdMin, self.thresholdMax, mask) cv.And(mask, gray, gray) line = [] points2 = [] for i in range(0, height - 1): row = cv.GetRow(gray, i) minVal, minLoc, maxLoc, maxVal = cv.MinMaxLoc(row) y = i x = maxVal[0] point = (0, 0, height - i) if x > 0: line.append((x, y)) x = width - x # left to the x-axis s = x / sin(radians(self.camAngle)) x = s * cos(self.angles[self.index]) z = height - y # 500 higher then the other. y = s * sin(self.angles[self.index]) a = radians(300) nx = (cos(a) * x) - (sin(a) * y) ny = (sin(a) * x) + (cos(a) * y) point = (nx, ny, z) points2.append(point) cv.PolyLine(source, [line], False, (255, 0, 0), 2, 8) cv.ResetImageROI(source) x, y, a, b = self.roi cv.Rectangle(source, (int(x), int(y)), (int(a), int(b)), (255.0, 255, 255, 0)) if self.mode == 'mask': cv.ShowImage('preview', mask) return if self.mode == 'record' and self.roi: font = cv.InitFont(cv.CV_FONT_HERSHEY_SIMPLEX, 0.5, 0.5, 1) cv.PutText(source, "recording %d" % self.index, (20, 20), font, (0, 0, 255)) self.points.extend(points) self.points2.extend(points2) #self.colors.extend(colors); cv.ShowImage('preview', source)
# 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)) cv.ShowImage("magnitude", image_Re) cv.WaitKey(0)
def locate_object(self, frame): ''' Finds the object in the given frame based on information from previous frames. The object location as a tuple (x,y) within the given image is returned. If the object is not found, False is returned. Tracker.object_center will always contain the last known object location. ''' if not self._template: raise RuntimeError("The Tracker class can not be used after it is " "unpickled.") search_rect = clip_rectangle( ( self.object_center[0] - self.search_size[0] / 2, # x self.object_center[1] - self.search_size[1] / 2, # y self.search_size[0], # width self.search_size[1], # height ), frame.width, frame.height) search_image = self._preprocess(crop(frame, search_rect)) result = cv.CreateImage( (search_image.width - self._template.width + 1, search_image.height - self._template.height + 1), cv.IPL_DEPTH_32F, 1) cv.MatchTemplate(search_image, self._template, result, self.match_method) min_or_max = MATCH_METHOD_MIN_OR_MAX[self.match_method] minmaxloc = cv.MinMaxLoc(result) if abs(minmaxloc[1] - minmaxloc[0]) < 0.001: return False match_in_result = minmaxloc[min_or_max] # Change from result image coordinates to search region coordinates to # image coordinates match_in_search_region = ( match_in_result[0] + self._template.width / 2, match_in_result[1] + self._template.height / 2, ) object_center = ( match_in_search_region[0] + search_rect[0], match_in_search_region[1] + search_rect[1], ) object_center = ( int(in_range(0, object_center[0], frame.width - 1)), int(in_range(0, object_center[1], frame.height - 1)), ) # Determine if the max/min is significant. hist = cv.CreateHist([256], cv.CV_HIST_ARRAY, [[0, 255]], 1) cv.CalcHist([scale_32f_image(result)], hist) # XXX stddevs from mean should be calculated from either 0 or 255 # depending on min or max distance = abs(libvision.hist.num_stddev_from_mean(hist, 255)) if distance < self.min_z_score: object_found = False else: object_found = True self._update_template(search_image, match_in_search_region) self.object_center = object_center if self.debug: result_8bit = scale_32f_image(result) if object_found: cv.Circle(result_8bit, match_in_result, 5, (0, 255, 0)) cv.Circle(search_image, match_in_search_region, 5, (0, 255, 0)) hist_image = libvision.hist.histogram_image(hist) cv.ShowImage("match", result_8bit) cv.ShowImage("template", scale_32f_image(self._template)) cv.ShowImage("search region", scale_32f_image(search_image)) cv.ShowImage("Histogram", hist_image) # Update Template if object_found: return self.object_center else: return False
def atualiza_foto(self): real = cv.CreateImage(cv.GetSize(imagem), cv.IPL_DEPTH_64F, 1) imaginario = cv.CreateImage(cv.GetSize(imagem), cv.IPL_DEPTH_64F, 1) complexo = cv.CreateImage(cv.GetSize(imagem), cv.IPL_DEPTH_64F, 2) cv.Scale(imagem_cinza, real, 1.0, 0.0) cv.Zero(imaginario) cv.Merge(real, imaginario, None, None, complexo) Altura_M = cv.GetOptimalDFTSize(imagem.height - 1) Largura_N = cv.GetOptimalDFTSize(imagem.width - 1) Vetor_dft = cv.CreateMat(Altura_M, Largura_N, cv.CV_64FC2) imagem_Real = cv.CreateImage((Largura_N, Altura_M), cv.IPL_DEPTH_64F, 1) imagem_Imaginaria = cv.CreateImage((Largura_N, Altura_M), cv.IPL_DEPTH_64F, 1) temporario = cv.GetSubRect(Vetor_dft, (0, 0, imagem.width, imagem.height)) cv.Copy(complexo, temporario, None) if (Vetor_dft.width > imagem.width): temporario = cv.GetSubRect( Vetor_dft, (imagem.width, 0, Largura_N - imagem.width, imagem.height)) cv.Zero(temporario) # APLICANDO FOURIER cv.DFT(Vetor_dft, Vetor_dft, cv.CV_DXT_FORWARD, complexo.height) cv.Split(Vetor_dft, imagem_Real, imagem_Imaginaria, None, None) cv.Pow(imagem_Real, imagem_Real, 2.0) cv.Pow(imagem_Imaginaria, imagem_Imaginaria, 2.0) cv.Add(imagem_Real, imagem_Imaginaria, imagem_Real, None) cv.Pow(imagem_Real, imagem_Real, 0.5) cv.AddS(imagem_Real, cv.ScalarAll(1.0), imagem_Real, None) cv.Log(imagem_Real, imagem_Real) cvShiftDFT(imagem_Real, imagem_Real) min, max, pt1, pt2 = cv.MinMaxLoc(imagem_Real) cv.Scale(imagem_Real, imagem_Real, 1.0 / (max - min), 1.0 * (-min) / (max - min)) #APLICANDO FILTRO passa-baixa circular cv.Circle(Vetor_dft, (0, 0), self.raio, [0, 0, 0], -1, 1, 0) cv.Circle(Vetor_dft, (Vetor_dft.cols, 0), self.raio, [0, 0, 0], -1, 1, 0) cv.Circle(Vetor_dft, (0, Vetor_dft.rows), self.raio, [0, 0, 0], -1, 1, 0) cv.Circle(Vetor_dft, (Vetor_dft.cols, Vetor_dft.rows), self.raio, [0, 0, 0], -1, 1, 0) cv.Split(Vetor_dft, imagem_Real, imagem_Imaginaria, None, None) cv.Pow(imagem_Real, imagem_Real, 2.0) cv.Pow(imagem_Imaginaria, imagem_Imaginaria, 2.0) cv.Add(imagem_Real, imagem_Imaginaria, imagem_Real, None) cv.Pow(imagem_Real, imagem_Real, 0.5) cv.AddS(imagem_Real, cv.ScalarAll(1.0), imagem_Real, None) cv.Log(imagem_Real, imagem_Real) cvShiftDFT(imagem_Real, imagem_Real) min, max, pt1, pt2 = cv.MinMaxLoc(imagem_Real) cv.Scale(imagem_Real, imagem_Real, 1.0 / (max - min), 1.0 * (-min) / (max - min)) cv.ShowImage("Transformada de Fourier", imagem_Real) # APLICANDO A INVERSA de Fourier cv.DFT(Vetor_dft, Vetor_dft, cv.CV_DXT_INVERSE_SCALE, Largura_N) cv.Split(Vetor_dft, imagem_Real, imagem_Imaginaria, None, None) min, max, pt1, pt2 = cv.MinMaxLoc(imagem_Real) if ((pt1 < 0) or (pt2 > 255)): cv.Scale(imagem_Real, imagem_Real, 1.0 / (max - min), 1.0 * (-min) / (max - min)) else: cv.Scale(imagem_Real, imagem_Real, 1.0 / 255, 0) cv.ShowImage("Inversa da Fourier", imagem_Real)
mask = cv.CreateMat(height,width,cv.CV_8U) cv.SetZero(mask) # histogram creation #temp = cv.CreateMat(height,width,cv.CV_32FC1) #cv.CV_8UC1) #cv.SetZero(temp) #cv.ConvertScale(themap,temp,40,0) #temp2 = cv.CreateMat(height,width,cv.CV_32FC1) #cv.SetZero(temp2) #cv.Pow(temp,temp2,0.5) #cv.SaveImage("histogram.png",temp2) cv.Smooth(themap, themap, cv.CV_GAUSSIAN, gaussian_blur,gaussian_blur) cv.SaveImage("map.png",themap) (minval,maxval,minloc,maxloc)=cv.MinMaxLoc(themap) print "Min: "+`minval`+" max: "+`maxval` cv.ConvertScale(themap,mask,255.0/maxval,0); cv.SaveImage("mask.png",mask) cv.CmpS(themap,mask_threshold,mask,cv.CV_CMP_GT) cv.SaveImage("thresholded.png",mask) #contour = cv.FindContours(mask,cv.CreateMemStorage(),cv.CV_RETR_CCOMP,cv.CV_CHAIN_APPROX_SIMPLE) chain = cv.FindContours(mask,cv.CreateMemStorage(),cv.CV_RETR_CCOMP,cv.CV_CHAIN_CODE) contour = cv.ApproxChains(chain,cv.CreateMemStorage(),cv.CV_CHAIN_APPROX_NONE,0,100,1) img = cv.CreateMat(height,width,cv.CV_8UC3) cv.SetZero(img) cv.DrawContours(img,contour,(255,255,255),(0,255,0),6,1) cv.SaveImage("contours.png",img)
def templateMatchFace(image, template, result): cv.MatchTemplate(image, template, result, cv.CV_TM_SQDIFF) (min_x, max_y, minloc, maxloc) = cv.MinMaxLoc(result) return minloc
reslist = [] for template in templates: temptype = template[0] tempimg = template[1] tempthre = template[2] tempname = template[3] if template[4] == 'CCORR': tempmethod = cv.CV_TM_CCORR_NORMED elif template[4] == 'CCOEFF': tempmethod = cv.CV_TM_CCOEFF_NORMED else: tempmethod = cv.CV_TM_SQDIFF_NORMED ressize = list(cv.GetSize(cv_image)) ressize[0] -= cv.GetSize(tempimg)[0] - 1 ressize[1] -= cv.GetSize(tempimg)[1] - 1 results = cv.CreateImage(ressize, cv.IPL_DEPTH_32F, 1 ) cv.MatchTemplate(cv_image, tempimg, results, tempmethod) status = cv.MinMaxLoc(results) if (tempmethod == cv.CV_TM_SQDIFF_NORMED): found = ( status[0] < tempthre ) reslist += [(tempname,(status[0],status[2],tempthre,found))] if found : result.data += tempname+' ' else : found = (tempthre < status[1] ) reslist += [(tempname,(status[1],status[3],tempthre,found))] if found : result.data += tempname+' ' print reslist result_pub.publish(result) publish_debug(cv_image, reslist)
# create the wanted images import cv image=cv.LoadImage('picture.png', cv.CV_LOAD_IMAGE_COLOR) grey=cv.CreateImage((100,100),8,1) eig = cv.CreateImage (cv.GetSize (grey), 32, 1) temp = cv.CreateImage (cv.GetSize (grey), 32, 1) # the default parameters quality = 0.01 min_distance = 10 # search the good points features = cv.GoodFeaturesToTrack ( grey, eig, temp, 1000, quality, min_distance, None, 3, 0, 0.04) for (x,y) in features: x) + ',' + str(y) cv.Circle (image, (int(x), int(y)), 3, (0, 255, 0), -1, 8, 0) cv.ResetImageROI(image) W,H=cv.GetSize(image) w,h=cv.GetSize(template) width=W-w+1 height=H-h+1 result=cv.CreateImage((width,height),32,1) cv.MatchTemplate(frame,template, result,cv.CV_TM_SQDIFF) (min_x,max_y,minloc,maxloc)=cv.MinMaxLoc(result) (x,y)=minloc cv.Rectangle(image2,(int(x),int(y)),(int(x)+w,int(y)+h),(255,255,255),1,0)