def largest_contour(src,maxLevel = 2): if (src.ndim==2): src=src.reshape(src.shape+(1,)) if (src.shape[2]!=1): src=src.mean(axis=2) if (src.dtype!=numpy.uint8): src=src.astype(numpy.uint8) storage=zopencv.cvCreateMemStorage(0) contour=zopencv.zopencv_pclasses.PointerOnCvSeq(0) ctrobj=zopencv.CvContour() szx=ctrobj.get_sizeof(ctrobj) offset=zopencv.CvPoint() offset.x=offset.y=0 zopencv.cvFindContours( src, storage.get_pointer(), contour.get_pointer_on_pointer(), szx, zopencv.CV_RETR_LIST, zopencv.CV_CHAIN_APPROX_SIMPLE ,offset); if( not contour.get_pointer() ): return [] # find largest contour iterator=zopencv.CvTreeNodeIterator() zopencv.cvInitTreeNodeIterator( iterator.get_pointer(), contour.get_pointer(), maxLevel ); largest = zopencv.zopencv_pclasses.PointerOnCvSeq(0) largest_total = 0 i=0 contour=zopencv.zopencv_pclasses.PointerOnCvSeq((zopencv.cvNextTreeNode( iterator ))) while( contour.get_pointer() != 0 ) : if( not (contour.flags & zopencv.CV_SEQ_FLAG_HOLE ) and contour.total > largest_total ): largest = contour; largest_total = contour.total; i+=1 contour=zopencv.zopencv_pclasses.PointerOnCvSeq((zopencv.cvNextTreeNode( iterator ))) contour = largest # convert it to python return map(lambda x:(x.x,x.y),map(lambda x:zopencv.zopencv_pclasses.PointerOnCvPoint(contour.getSeqElem(x)) ,range(contour.total)))
def houghtlines(image): block_size = 0 timage=image.mean(axis=2).astype(numpy.uint8) storage = zopencv.cvCreateMemStorage ( block_size ) pt1=zopencv.CvPoint() pt2=zopencv.CvPoint() lines = zopencv.cvHoughLines2(timage, storage.get_pointer(), zopencv.CV_HOUGH_STANDARD, 1, zopencv.CV_PI/180, 100, 0, 0 ) #cvFlip(image) BIG=image.shape[0]+image.shape[1] resimage=image.copy('C') for i in range(min(lines.total,100)): line = zopencv.zopencv_pclasses.PointerOnCvPoint2D32f(zopencv.cvGetSeqElem(lines,i)); rho = line.x; theta = line.y; a = math.cos(theta) b = math.sin(theta) x0 = a*rho y0 = b*rho pt1.x = zopencv.cvRound(x0 + BIG*(-b)) pt1.y = zopencv.cvRound(y0 + BIG*(a)) pt2.x = zopencv.cvRound(x0 - BIG*(-b)) pt2.y = zopencv.cvRound(y0 - BIG*(a)) color=zopencv.cvScalar(255,0,0,0) zopencv.cvLine(resimage, pt1, pt2, color, 1, 1,0 )#zopencv.CV_RGB(255,0,0 zopencv.cvReleaseMemStorage(storage.get_pointer_on_pointer()) return resimage
def surf(x, as_couple=False): xnb = x if xnb.ndim == 3: xnb = xnb.mean(axis=2) if xnb.dtype != numpy.uint8: xnb = xnb.astype(numpy.uint8) # from opencv.cv import cvExtractSURF,CvSeq,cvCreateMemStorage, cvSURFParams # keypoints,descriptors=cvExtractSURF(xnb,0,cvSURFParams( 500, 1 ) ,0) import zopencv from zopencv import cvExtractSURF, cvSURFParams params = cvSURFParams(500, 1) print dir(params) # cvSURFParams( 500, 1 ) # CvArr * img,CvArr * mask,CvSeq * * keypoints,CvSeq * * descriptors,CvMemStorage * storage,CvSURFParams params,int useProvidedKeyPts) keypoints = zopencv.zopencv_pclasses.PointerOnCvSeq(0) descriptors = zopencv.zopencv_pclasses.PointerOnCvSeq(0) storage = zopencv.cvCreateMemStorage(0) cvExtractSURF(xnb, 0, keypoints.get_pointer_on_pointer(), descriptors.get_pointer_on_pointer(), storage, params, 0) print "SURF OUTPUT=", keypoints.total, descriptors.total keypoints = map( lambda x: zopencv.zopencv_pclasses.PointerOnCvPoint(keypoints.getSeqElem(x)), range(keypoints.total) ) descriptors = map( lambda x: zopencv.zopencv_pclasses.PointerOnCvPoint(descriptors.getSeqElem(x)), range(descriptors.total) ) zopencv.cvReleaseMemStorage(storage.get_pointer_on_pointer()) if as_couple: return (numpy.array(keypoints), numpy.array(descriptors)) else: return numpy.hstack([numpy.array(keypoints), numpy.array(descriptors)])
def opencv_pyrsegmentation( image0, threshold1 =255,threshold2 =30, level=4,output=0): block_size = 0 storage = zopencv.cvCreateMemStorage ( block_size ) height,width=image0.shape[:2] width =width & -(1<<level) height =height & -(1<<level) image0 = image0[:height,:width,:].copy('C') image1 = image0.copy('C') comp=zopencv.zopencv_pclasses.PointerOnCvSeq(0) zopencv.cvPyrSegmentation(image0, image1, storage, comp.get_pointer_on_pointer(), level, min(255,max(5, threshold1+1)), min(255,max(5, threshold2+1))) #del comp #comp=None zopencv.cvReleaseMemStorage(storage.get_pointer_on_pointer()) if (output==0): return image1.copy('C') elif (output==1): return comp else: return image1,comp