def __link(self): myfont = cv.cvInitFont(cv.CV_FONT_HERSHEY_SIMPLEX, 0.5, 0.5) kkk = 0 self.edges = [] for curve in self.allcurve: showpt = [] state = 0 currentPoint = None currentPointIdx = -1 cumulate = 0 dcurve = curve + curve curlen = len(curve) ptcount = 0 pointseq = [] for c in dcurve: if (ptcount > curlen): break cumulate += 1 for kk in range(len(self.points)): k = self.points[kk] if (abs(c.x - k.x) + abs(c.y - k.y) < 15): if (currentPoint != k or cumulate > 40): state += 1 currentPoint = k currentPointIdx = kk cumulate = 0 pointseq.append(kk) if (state > 0): showpt.append([c, state, currentPointIdx]) ptcount += 1 if (state > 1): kkk += 1 cnt = 0 pret = -1 e = None for s, t, cp in showpt: if (cp != pret): if e != None: e.end = cp e = Edge() self.edges.append(e) e.start = cp pret = cp cnt += 1 if (t < state): e.addPoint(s) #print "%d\t%3.2f\t%3.2f\t%d\t%d\t%d" % (kkk, s.x, s.y, cnt, pointseq[t - 1], pointseq[t]) e.end = showpt[-1][2] print >> OUT, "seq\tptn\tx\ty\t" # self.__edgededup() self.__evenSample(self.npoints) for ie, e in enumerate(self.edges): print "P(%d) <-> P(%d) length %d, selected %d" % ( e.start, e.end, len(e.points), len(e.selected)) for d in e.points: cv.cvSet2D(self.drawimg, int(d.y), int(d.x), color[3]) for ip, p in enumerate(e.selected): cv.cvDrawCircle(self.drawimg, p, 2, cv.cvScalar(255, 255, 0, 0)) print >> OUT, "%d\t%d\t%d\t%d" % (ie, ip, p.x, p.y)
def __link(self): myfont = cv.cvInitFont(cv.CV_FONT_HERSHEY_SIMPLEX, 0.5, 0.5) kkk = 0 self.edges = [] for curve in self.allcurve: showpt = [] state = 0 currentPoint = None currentPointIdx = -1 cumulate = 0 dcurve = curve + curve curlen = len(curve) ptcount = 0 pointseq = [] for c in dcurve: if (ptcount > curlen): break cumulate += 1 for kk in range(len(self.points)): k = self.points[kk] if (abs(c.x - k.x) + abs(c.y - k.y) < 15): if (currentPoint != k or cumulate > 40): state += 1 currentPoint = k currentPointIdx = kk cumulate = 0 pointseq.append(kk) if (state > 0): showpt.append([c, state, currentPointIdx]) ptcount += 1 if (state > 1): kkk += 1 cnt = 0 pret = -1 e = None for s,t, cp in showpt: if (cp != pret): if e != None: e.end = cp e = Edge() self.edges.append(e) e.start = cp pret = cp cnt += 1 if (t < state): e.addPoint(s) #print "%d\t%3.2f\t%3.2f\t%d\t%d\t%d" % (kkk, s.x, s.y, cnt, pointseq[t - 1], pointseq[t]) e.end = showpt[-1][2] print >> OUT, "seq\tptn\tx\ty\t" # self.__edgededup() self.__evenSample(self.npoints) for ie, e in enumerate(self.edges): print "P(%d) <-> P(%d) length %d, selected %d" % (e.start, e.end, len(e.points), len(e.selected)) for d in e.points: cv.cvSet2D(self.drawimg, int(d.y), int(d.x), color[3]) for ip, p in enumerate(e.selected): cv.cvDrawCircle(self.drawimg, p, 2, cv.cvScalar(255,255,0,0)) print >> OUT, "%d\t%d\t%d\t%d" % (ie, ip, p.x, p.y)
def DrawKeyPoints(self): if (not self.drawimg): self.drawimg = cv.cvCloneImage(self.img) myfont = cv.cvInitFont(cv.CV_FONT_HERSHEY_SIMPLEX, 0.5, 0.5) ic = 0 for c in self.points: cv.cvPutText(self.drawimg, str(ic), cv.cvPoint(int(c.x), int(c.y)), myfont, cv.cvScalar(255, 255, 0,0)) ic += 1 cv.cvDrawCircle(self.drawimg, c, 4, cv.cvScalar(255,255,0,0))
def DrawKeyPoints(self): ic = 0 myfont = cv.cvInitFont(cv.CV_FONT_HERSHEY_SIMPLEX, 0.5, 0.5) for ic, c in enumerate(self.mss.seqs): for k in c.points: if self.bDrawNumber: cv.cvPutText( self.drawimg, str(ic), cv.cvPoint(int(k.x), int(k.y)), myfont, cv.cvScalar(255, 255, 0, 0) ) cv.cvDrawCircle(self.drawimg, cv.cvPoint(int(k.x), int(k.y)), 4, cv.cvScalar(255, 0, 255, 0))
def DrawKeyPoints(self): if (not self.drawimg): self.drawimg = cv.cvCloneImage(self.img) myfont = cv.cvInitFont(cv.CV_FONT_HERSHEY_SIMPLEX, 0.5, 0.5) ic = 0 for c in self.points: cv.cvPutText(self.drawimg, str(ic), cv.cvPoint(int(c.x), int(c.y)), myfont, cv.cvScalar(255, 255, 0, 0)) ic += 1 cv.cvDrawCircle(self.drawimg, c, 4, cv.cvScalar(255, 255, 0, 0))
def get_nearest_feature( image, this_point, n=2000 ): """ Get the n-nearest features to a specified image coordinate. Features are determined using cvGoodFeaturesToTrack. """ _red = cv.cvScalar (0, 0, 255, 0); _green = cv.cvScalar (0, 255, 0, 0); _blue = cv.cvScalar (255,0,0,0); _white = cv.cvRealScalar (255) _black = cv.cvRealScalar (0) quality = 0.01 min_distance = 4 N_best = n win_size = 11 grey = cv.cvCreateImage (cv.cvGetSize (image), 8, 1) eig = cv.cvCreateImage (cv.cvGetSize (image), 32, 1) temp = cv.cvCreateImage (cv.cvGetSize (image), 32, 1) # create a grey version of the image cv.cvCvtColor ( image, grey, cv.CV_BGR2GRAY) points = cv.cvGoodFeaturesToTrack ( grey, eig, temp, N_best, quality, min_distance, None, 3, 0, 0.04) # refine the corner locations better_points = cv.cvFindCornerSubPix ( grey, points, cv.cvSize (win_size, win_size), cv.cvSize (-1, -1), cv.cvTermCriteria (cv.CV_TERMCRIT_ITER | cv.CV_TERMCRIT_EPS, 20, 0.03)) eigs = [] for i in range(len(points)): eigs.append(cv.cvGetMat(eig)[int(points[i].y)][int(points[i].x)]) mypoints = np.matrix(np.zeros((len(points)*2),dtype=float)).reshape(len(points),2) dists = [] for i,point in enumerate(points): mypoints[i,0]=point.x mypoints[i,1]=point.y dists.append( np.linalg.norm(mypoints[i,:]-this_point) ) dists = np.array(dists) sorteddists = dists.argsort() cv.cvDrawCircle ( image, points[ sorteddists[0] ], 5, _green, 2, 8, 0 ) return better_points[ sorteddists[0] ]
def DrawKeyPoints(self): ic = 0 myfont = cv.cvInitFont(cv.CV_FONT_HERSHEY_SIMPLEX, 0.5, 0.5) for ic, c in enumerate(self.mss.seqs): cnt = 0 for k in c.points: cnt += 1 if (int(cnt/2) * 2 != cnt): continue cv.cvDrawCircle(self.drawimg, cv.cvPoint(int(k.x), int(k.y)), 4, cv.cvScalar(255,255,255,0)) if (self.bDrawNumber and (cnt > self.start) and cnt < self.start + 8*4 and len(c.points) > 30): #cv.cvPutText(self.drawimg, str(cnt), cv.cvPoint(int(k.x) + 5, int(k.y)), myfont, cv.cvScalar(255, 255, 0,0)) cv.cvDrawCircle(self.drawimg, cv.cvPoint(int(k.x), int(k.y)), 4, cv.cvScalar(255,0, 255,0))
def get_nearest_feature(image, this_point, n=2000): """ Get the n-nearest features to a specified image coordinate. Features are determined using cvGoodFeaturesToTrack. """ _red = cv.cvScalar(0, 0, 255, 0) _green = cv.cvScalar(0, 255, 0, 0) _blue = cv.cvScalar(255, 0, 0, 0) _white = cv.cvRealScalar(255) _black = cv.cvRealScalar(0) quality = 0.01 min_distance = 4 N_best = n win_size = 11 grey = cv.cvCreateImage(cv.cvGetSize(image), 8, 1) eig = cv.cvCreateImage(cv.cvGetSize(image), 32, 1) temp = cv.cvCreateImage(cv.cvGetSize(image), 32, 1) # create a grey version of the image cv.cvCvtColor(image, grey, cv.CV_BGR2GRAY) points = cv.cvGoodFeaturesToTrack(grey, eig, temp, N_best, quality, min_distance, None, 3, 0, 0.04) # refine the corner locations better_points = cv.cvFindCornerSubPix( grey, points, cv.cvSize(win_size, win_size), cv.cvSize(-1, -1), cv.cvTermCriteria(cv.CV_TERMCRIT_ITER | cv.CV_TERMCRIT_EPS, 20, 0.03)) eigs = [] for i in range(len(points)): eigs.append(cv.cvGetMat(eig)[int(points[i].y)][int(points[i].x)]) mypoints = np.matrix(np.zeros((len(points) * 2), dtype=float)).reshape(len(points), 2) dists = [] for i, point in enumerate(points): mypoints[i, 0] = point.x mypoints[i, 1] = point.y dists.append(np.linalg.norm(mypoints[i, :] - this_point)) dists = np.array(dists) sorteddists = dists.argsort() cv.cvDrawCircle(image, points[sorteddists[0]], 5, _green, 2, 8, 0) return better_points[sorteddists[0]]
def paint(self, img): for p in self.points: cv.cvDrawCircle(img, p.getCvPoint(), 2, cv.cvScalar(0, 0, 255,0)) for i in range(len(self.points) - 1): cv.cvLine(img, self.points[i].getCvPoint(), self.points[i + 1].getCvPoint(), cv.cvScalar(255,255,255,0), 1)
def main(): ct1 = CurvePoint() ct2 = CurvePoint() sc = CntSC() ang = CntAngle() usage = "%prog [options] <imgfile1> <imgfile2>" version = "%prog 0.2\nLongbin Chen, [email protected]" oparser = optparse.OptionParser(usage=usage, version=version) oparser.add_option('-d', '--display', action="store_true", dest = 'display', default = False, help = 'display the image') oparser.add_option('-n', '--number', dest = 'num', type="int", default = 200 , help = 'the number of feature points') oparser.add_option('-s', '--save', dest = 'save', default = None, help = 'save the img file') oparser.add_option('-o', '--output', dest = 'output', default = None, help = 'output file') (options, args) = oparser.parse_args(sys.argv) if len(args) != 3: oparser.parse_args([sys.argv[0], "--help"]) sys.exit(1) ct1.GetContour(args[1], options.num) allkeys = [] for c in ct1.allselected: allkeys = allkeys + c sc.ExtractFeature(allkeys) ang.ExtractFeature(allkeys,0); allkeys = [] ct2.GetContour(args[2], options.num) for c in ct2.allselected: allkeys = allkeys + c sc.ExtractFeature(allkeys) ang.ExtractFeature(allkeys,0); sumscore = [] matcher = SmithWaterman() ct1.bDrawNumber = 0 ct2.bDrawNumber = 0 if (options.display): ct1.DrawKeyPoints() ct2.DrawKeyPoints() myfont = cv.cvInitFont(cv.CV_FONT_HERSHEY_SIMPLEX, 0.5, 0.5) idx = -1 putoriginal(args[1], ct1.drawimg) putoriginal(args[2], ct2.drawimg) cv.cvNot(ct1.drawimg, ct1.drawimg) cv.cvNot(ct2.drawimg, ct2.drawimg) for c1 in ct1.allselected: idx += 1 cscore = -100000000 cpt1 = getdata(c1) bX = [] bY = [] bestcurve = None for c2 in ct2.allselected: cpt2 = getdata(c2) cost,align,X,Y = matcher.Align(cpt1, cpt2) normalized_score = cost - log10(len(c2) + 1) * 1000 print len(c1), len(c2),cost, normalized_score, cscore if (normalized_score > cscore): cscore = normalized_score bX = X[:] bY = Y[:] bestcurve = c2 if (options.display): ptcount = 0 for i in range(len(bX)): xi = bX[i] yi = bY[i] #if (xi == -1): #cv.cvDrawCircle(ct2.drawimg, cv.cvPoint(int(bestcurve[yi].x), int(bestcurve[yi].y)),4, cv.cvScalar(255,0,0,0)) #cv.cvPutText(ct2.drawimg, 'O', cv.cvPoint(int(c2[yi].x), int(c2[yi].y)), myfont, cv.cvScalar(255, 0, 0,0)) #if (yi == -1): #cv.cvDrawCircle(ct1.drawimg, cv.cvPoint(int(c1[xi].x), int(c1[xi].y)),4, cv.cvScalar(255,0,0,0)) #cv.cvPutText(ct1.drawimg, 'O', cv.cvPoint(int(c1[xi].x), int(c1[xi].y)), myfont, cv.cvScalar(255, 0, 0,0)) if (xi != -1 and yi != -1): ptcount += 1 cv.cvDrawCircle(ct1.drawimg, cv.cvPoint(int(c1[xi].x), int(c1[xi].y)),2, clrs[idx]) cv.cvPutText(ct1.drawimg, str(ptcount), cv.cvPoint(int(c1[xi].x), int(c1[xi].y)), myfont, clrs[idx]) cv.cvDrawCircle(ct2.drawimg, cv.cvPoint(int(bestcurve[yi].x), int(bestcurve[yi].y)),2, clrs[idx]) cv.cvPutText(ct2.drawimg, str(ptcount), cv.cvPoint(int(bestcurve[yi].x), int(bestcurve[yi].y)), myfont, clrs[idx]) sumscore.append(cscore) print sumscore if (options.display): highgui.cvNamedWindow ("contour1", 1) highgui.cvNamedWindow ("contour2", 1) highgui.cvShowImage ("contour1", ct1.drawimg) highgui.cvShowImage ("contour2", ct2.drawimg) highgui.cvWaitKey (0) if (options.save): mergeimg = mergeimage_83(ct1.drawimg, ct2.drawimg) highgui.cvSaveImage("_sw_result.bmp", mergeimg)
def main(): ct1 = CurvePoint() ct2 = CurvePoint() agl = CntAngle() sc = CntSC() try: opts, args = getopt.getopt(sys.argv[1:], "ho:dn:es", ["help", "output=", "draw", "num=", "even", "save"]) except getopt.GetoptError: usage() sys.exit(2) output = None bDraw = 0 bSave = 0 bOriginal = 0 npoint = 100 for o, a in opts: if o == "-v": ct1.verbose = 1 ct2.verbose = 1 if o in ("-h", "--help"): usage() sys.exit() if o in ("-o", "--output"): output = a if o in ("-d", "--draw"): bDraw = 1 if o in ("-s", "--save"): bSave = 1 if o in ("-r", "--original"): bOriginal = 1 if o in ("-n", "--num"): npoint = string.atoi(a) if o in ("-e", "--even"): ct1.bEven = 1 ct2.bEven = 1 if (len(args)) != 2: usage() sys.exit(2) ct1.GetContour(args[0], npoint) allkeys = [] for c in ct1.allselected: # agl.ExtractFeature(c, ct1.drawimg) allkeys = allkeys + c sc.ExtractFeature(allkeys) allkeys = [] ct2.GetContour(args[1], npoint) for c in ct2.allselected: # agl.ExtractFeature(c, ct2.drawimg) allkeys = allkeys + c sc.ExtractFeature(allkeys) sumscore = [] matcher = SmithWaterman() ct1.bDrawNumber = 0 ct2.bDrawNumber = 0 if bDraw: ct1.DrawKeyPoints() ct2.DrawKeyPoints() myfont = cv.cvInitFont(cv.CV_FONT_HERSHEY_SIMPLEX, 0.5, 0.5) idx = -1 putoriginal(args[0], ct1.drawimg) putoriginal(args[1], ct2.drawimg) cv.cvNot(ct1.drawimg, ct1.drawimg) cv.cvNot(ct2.drawimg, ct2.drawimg) for c1 in ct1.allselected: idx += 1 cscore = -100000000 cpt1 = getdata(c1) bX = [] bY = [] bestcurve = None for c2 in ct2.allselected: cpt2 = getdata(c2) cost, align, X, Y = matcher.Align(cpt1, cpt2) normalized_score = cost - log10(len(c2) + 1) * 1000 print len(c1), len(c2), cost, normalized_score, cscore if normalized_score > cscore: cscore = normalized_score bX = X[:] bY = Y[:] bestcurve = c2 if bDraw: ptcount = 0 for i in range(len(bX)): xi = bX[i] yi = bY[i] # if (xi == -1): # cv.cvDrawCircle(ct2.drawimg, cv.cvPoint(int(bestcurve[yi].x), int(bestcurve[yi].y)),4, cv.cvScalar(255,0,0,0)) # cv.cvPutText(ct2.drawimg, 'O', cv.cvPoint(int(c2[yi].x), int(c2[yi].y)), myfont, cv.cvScalar(255, 0, 0,0)) # if (yi == -1): # cv.cvDrawCircle(ct1.drawimg, cv.cvPoint(int(c1[xi].x), int(c1[xi].y)),4, cv.cvScalar(255,0,0,0)) # cv.cvPutText(ct1.drawimg, 'O', cv.cvPoint(int(c1[xi].x), int(c1[xi].y)), myfont, cv.cvScalar(255, 0, 0,0)) if xi != -1 and yi != -1: ptcount += 1 cv.cvDrawCircle(ct1.drawimg, cv.cvPoint(int(c1[xi].x), int(c1[xi].y)), 2, clrs[idx]) cv.cvPutText(ct1.drawimg, str(ptcount), cv.cvPoint(int(c1[xi].x), int(c1[xi].y)), myfont, clrs[idx]) cv.cvDrawCircle(ct2.drawimg, cv.cvPoint(int(bestcurve[yi].x), int(bestcurve[yi].y)), 2, clrs[idx]) cv.cvPutText( ct2.drawimg, str(ptcount), cv.cvPoint(int(bestcurve[yi].x), int(bestcurve[yi].y)), myfont, clrs[idx], ) sumscore.append(cscore) print sumscore if bDraw: highgui.cvNamedWindow("contour1", 1) highgui.cvNamedWindow("contour2", 1) highgui.cvShowImage("contour1", ct1.drawimg) highgui.cvShowImage("contour2", ct2.drawimg) highgui.cvWaitKey(0) if bSave: mergeimg = mergeimage_83(ct1.drawimg, ct2.drawimg) highgui.cvSaveImage("_sw_result.bmp", mergeimg)
def main(): ct1 = CurvePoint() ct2 = CurvePoint() sc = CntSC() ang = CntAngle() usage = "%prog [options] <imgfile1> <imgfile2>" version = "%prog 0.2\nLongbin Chen, [email protected]" oparser = optparse.OptionParser(usage=usage, version=version) oparser.add_option('-d', '--display', action="store_true", dest='display', default=False, help='display the image') oparser.add_option('-n', '--number', dest='num', type="int", default=200, help='the number of feature points') oparser.add_option('-s', '--save', dest='save', default=None, help='save the img file') oparser.add_option('-o', '--output', dest='output', default=None, help='output file') (options, args) = oparser.parse_args(sys.argv) if len(args) != 3: oparser.parse_args([sys.argv[0], "--help"]) sys.exit(1) ct1.GetContour(args[1], options.num) allkeys = [] for c in ct1.allselected: allkeys = allkeys + c sc.ExtractFeature(allkeys) ang.ExtractFeature(allkeys, 0) allkeys = [] ct2.GetContour(args[2], options.num) for c in ct2.allselected: allkeys = allkeys + c sc.ExtractFeature(allkeys) ang.ExtractFeature(allkeys, 0) sumscore = [] matcher = SmithWaterman() ct1.bDrawNumber = 0 ct2.bDrawNumber = 0 if (options.display): ct1.DrawKeyPoints() ct2.DrawKeyPoints() myfont = cv.cvInitFont(cv.CV_FONT_HERSHEY_SIMPLEX, 0.5, 0.5) idx = -1 putoriginal(args[1], ct1.drawimg) putoriginal(args[2], ct2.drawimg) cv.cvNot(ct1.drawimg, ct1.drawimg) cv.cvNot(ct2.drawimg, ct2.drawimg) for c1 in ct1.allselected: idx += 1 cscore = -100000000 cpt1 = getdata(c1) bX = [] bY = [] bestcurve = None for c2 in ct2.allselected: cpt2 = getdata(c2) cost, align, X, Y = matcher.Align(cpt1, cpt2) normalized_score = cost - log10(len(c2) + 1) * 1000 print len(c1), len(c2), cost, normalized_score, cscore if (normalized_score > cscore): cscore = normalized_score bX = X[:] bY = Y[:] bestcurve = c2 if (options.display): ptcount = 0 for i in range(len(bX)): xi = bX[i] yi = bY[i] #if (xi == -1): #cv.cvDrawCircle(ct2.drawimg, cv.cvPoint(int(bestcurve[yi].x), int(bestcurve[yi].y)),4, cv.cvScalar(255,0,0,0)) #cv.cvPutText(ct2.drawimg, 'O', cv.cvPoint(int(c2[yi].x), int(c2[yi].y)), myfont, cv.cvScalar(255, 0, 0,0)) #if (yi == -1): #cv.cvDrawCircle(ct1.drawimg, cv.cvPoint(int(c1[xi].x), int(c1[xi].y)),4, cv.cvScalar(255,0,0,0)) #cv.cvPutText(ct1.drawimg, 'O', cv.cvPoint(int(c1[xi].x), int(c1[xi].y)), myfont, cv.cvScalar(255, 0, 0,0)) if (xi != -1 and yi != -1): ptcount += 1 cv.cvDrawCircle(ct1.drawimg, cv.cvPoint(int(c1[xi].x), int(c1[xi].y)), 2, clrs[idx]) cv.cvPutText(ct1.drawimg, str(ptcount), cv.cvPoint(int(c1[xi].x), int(c1[xi].y)), myfont, clrs[idx]) cv.cvDrawCircle( ct2.drawimg, cv.cvPoint(int(bestcurve[yi].x), int(bestcurve[yi].y)), 2, clrs[idx]) cv.cvPutText( ct2.drawimg, str(ptcount), cv.cvPoint(int(bestcurve[yi].x), int(bestcurve[yi].y)), myfont, clrs[idx]) sumscore.append(cscore) print sumscore if (options.display): highgui.cvNamedWindow("contour1", 1) highgui.cvNamedWindow("contour2", 1) highgui.cvShowImage("contour1", ct1.drawimg) highgui.cvShowImage("contour2", ct2.drawimg) highgui.cvWaitKey(0) if (options.save): mergeimg = mergeimage_83(ct1.drawimg, ct2.drawimg) highgui.cvSaveImage("_sw_result.bmp", mergeimg)