# -*- coding: utf-8 -*- import math import numpy from pycvf.core import genericmodel from pycvf.datatypes import image import zopencv def shitomasi(image, quality_level = 0.1, min_distance = 5, eig_block_size = 3, use_harris = 0,MAX_CORNERS = 100,k=0.04): timage=image.mean(axis=2).astype(numpy.uint8) cornersn=numpy.zeros((MAX_CORNERS,2),dtype=numpy.float32) corners=zopencv.zopencv_pclasses.PointerOnCvPoint2D32f(zopencv.zopencv_core.memory_addr_of_numpy_array(cornersn)) corner_count = numpy.array([MAX_CORNERS],dtype=int) mask=zopencv.zopencv_pclasses.PointerOnCvMat(0) eig_image=timage.astype(numpy.float32) temp_image=timage.astype(numpy.float32) zopencv.cvGoodFeaturesToTrack(timage, eig_image, temp_image, corners.get_pointer(), corner_count, quality_level, min_distance, mask.get_pointer(), eig_block_size, use_harris, k) return cornersn Model=genericmodel.pycvf_model_function(image.Datatype,image.Datatype)(shitomasi) __call__=Model
# -*- coding: utf-8 -*- import numpy, sys from pycvf.core import genericmodel from pycvf.datatypes import image from pycvf.datatypes import basics from pycvf.lib.info.obs import make_observation def obs(x, *args, **kwargs): return make_observation(*args, **kwargs)(x) Model = genericmodel.pycvf_model_function(image.Datatype, basics.NumericArray.Datatype)(obs) __call__ = Model
# -*- coding: utf-8 -*- ######################################################################################################################################### # # MyModel By Bertrand NOUVEL # 2009 CNRS Postdoctorate JFLI # # (c) All rights reserved # ############################################### # ################################################################################################################################################################################ # Includes ################################################################################################################################################################################ import os from pycvf.core.genericmodel import pycvf_model_function from pycvf.core import genericmodel from pycvf.datatypes import image import PIL from PIL import Image from pycvf.lib.graphics.imgfmtutils import * def jpegcompress(x,amount=0.94): tf=os.tmpnam()+".jpg" NumPy2PIL(x).save(tf,quality=(1-amount)*100) r=PIL2NumPy(Image.open(tf)) os.remove(tf) return r Model=pycvf_model_function(image.Datatype, image.Datatype)(jpegcompress) __call__=Model
# -*- coding: utf-8 -*- ######################################################################################################################################### # # MyModel By Bertrand NOUVEL # 2009 CNRS Postdoctorate JFLI # # (c) All rights reserved # ############################################### # ################################################################################################################################################################################ # Includes ################################################################################################################################################################################ import numpy, sys from pycvf.core import genericmodel from pycvf.datatypes import basics def histogram_alt(x,*args,**kwargs): return numpy.histogram(x,*args,**kwargs)[0] Model=genericmodel.pycvf_model_function(None,basics.NumericArray.Datatype,numpy.histogram_alt) __call__=Model
import numpy, sys from pycvf.core import genericmodel from pycvf.datatypes import image def ddif(x,y): dpi=(2*numpy.pi) z=((((x-y)%dpi)+dpi)%dpi) z[z>numpy.pi]-=dpi return abs(z) def orient_filter(x,value=0,smooth=0.08,oneside=False): dx=x.shape[0] dy=x.shape[1] dx2=dx//2 dy2=dy//2 g=numpy.mgrid[(-dx2):(dx-dx2),(-dy2):(dy-dy2)] gd=numpy.angle(g[0]+g[1]*1J) if (oneside): g=((numpy.pi/2.)-numpy.arctan(ddif(gd,value)/smooth))/(numpy.pi/2.) else: g=((numpy.pi/2.)-numpy.arctan(ddif(gd,value)/smooth))/(numpy.pi/2.)+((numpy.pi/2.)-numpy.arctan(ddif(gd,(value+numpy.pi))/smooth))/(numpy.pi/2.) if (x.ndim==2): return g*x else: return g.reshape(x.shape[0],x.shape[1],1)*x Model=genericmodel.pycvf_model_function(image.Datatype,image.Datatype)(orient_filter) __call__=Model
######################################################################################################################################### # # MyModel By Bertrand NOUVEL # 2009 CNRS Postdoctorate JFLI # # (c) All rights reserved # ############################################### # ################################################################################################################################################################################ # Includes ################################################################################################################################################################################ ### ### from pycvf.core.genericmodel import pycvf_model_function from pycvf.datatypes import image import numpy def noise(x,amount=0.5): if (x.dtype==numpy.uint8): amount*=255 return (x.astype(float)+(numpy.random.random(x.shape)-.5)*amount).clip(0,255).astype(x.dtype) else: return x+((numpy.random.random(x.shape)-.5)*amount) Model=pycvf_model_function(image.Datatype, image.Datatype)(noise) __call__=Model
# return numpy.sign(y)*(x >= thld)*(x - thld) #def hard_thres(y,thld): # return (numpy.abs(y) > thld)*y def wdenoise(x,tau=-1,mode=0,multiplier=3): if (type(x) in [list,tuple]): return map(lambda y:wdenoise(y,tau,mode),x) else: if (tau=-1): tmp=numpy.hstack(reduce( lambda b,n: b+map(lambda y:y.ravel(),filter(lambda y:type(y)==numpy.ndarray,n)) ,x,[]) ) tau= multiplier*scipy.stats.median(abs(tmp(:)))/.67; elif(tau=-2), tau = multiplier*std(tmp(:)); if (mode==0): x[abs(x)<=tau]=0 return x elif (mode==1): x[abs(x)<=tau]=0 x[x>tau]-=tau x[x<-tau]+=tau return x else: raise ValueError,"Unknown mode" Model=genericmodel.pycvf_model_function(ldt.Datatype(ldt.Datatype(image.Datatype)),ldt.Datatype(ldt.Datatype(image.Datatype)))(wdenoise) __call__=Model
# (c) All rights reserved # ############################################### # ################################################################################################################################################################################ # Includes ################################################################################################################################################################################ ### ### from pycvf.core.genericmodel import pycvf_model_function from pycvf.datatypes import image import numpy, random def blackframe(i,amount=0.2): i=i.copy() h,w=i.shape[0],i.shape[0] amount=amount**.5 dx=int(w*amount) dy=int(h*amount) x=random.randint(0,w-dx-1) y=random.randint(0,h-dy-1) i[y:(y+dy),x:(x+dx)]=0 return i Model=pycvf_model_function(image.Datatype, image.Datatype)(blackframe) __call__=Model
# -*- coding: utf-8 -*- ######################################################################################################################################### # # MyModel By Bertrand NOUVEL # 2009 CNRS Postdoctorate JFLI # # (c) All rights reserved # ############################################### # ################################################################################################################################################################################ # Includes ################################################################################################################################################################################ ### ### from pycvf.core import genericmodel from pycvf.datatypes import basics from pycvf.datatypes import image from pycvf.datatypes import list as ldt from pycvf.lib.graphics.kp_sift import sift from pycvf.structures.list import PointListStructure Model = genericmodel.pycvf_model_function(image.Datatype, ldt.Datatype(basics.NumericArray.Datatype))(sift) __call__ = Model
# -*- coding: utf-8 -*- ######################################################################################################################################### # # MyModel By Bertrand NOUVEL # 2009 CNRS Postdoctorate JFLI # # (c) All rights reserved # ############################################### # ################################################################################################################################################################################ # Includes ################################################################################################################################################################################ ### ### import os from pycvf.core.genericmodel import pycvf_model_function from pycvf.core import genericmodel from pycvf.datatypes import image from pycvf.lib.graphics.watershed import watershed Model=pycvf_model_function(image.Datatype, image.Datatype)(watershed) __call__=Model
# -*- coding: utf-8 -*- import numpy, sys from pycvf.core import genericmodel from pycvf.datatypes import image from pycvf.datatypes import list as ldt def downscale_fast(x): return x[::2,::2] def downscale_bm(x): return x.reshape(x.shape[0]//2,2, x.shape[1]//2,2,-1).swapaxes(1,2).reshape(x.shape[0]//2,x.shape[0]//2,4,-1).mean(axis=2) def pyramidalize(x,ds=downscale_bm,upbound=1): s=min(x.shape[0],x.shape[1]) return reduce(lambda b,n:[ds(b[0])]+b ,range(numpy.log2(s)-(1+upbound)), [x]) Model=genericmodel.pycvf_model_function(image.Datatype,ldt.Datatype(image.Datatype))(pyramidalize) __call__=Model
# ############################################### # ################################################################################################################################################################################ # Includes ################################################################################################################################################################################ import numpy, sys import pywt from pycvf.core import genericmodel from pycvf.datatypes import image from pycvf.datatypes import list as ldt def dwt_one_layer(x,wav): return pywt.dwt2(x,wav) def all_layer(f,i): return ([f(i[:,:,l]) for l in range(i.shape[2])]) def imgdwt(x,wavelet='db5'): if (x.ndim==3): r=all_layer(lambda y:dwt_one_layer(y,wavelet),x) else: r=[dwt_one_layer(x,wavelet)] return r Model=genericmodel.pycvf_model_function(image.Datatype, ldt.Datatype(ldt.Datatype(image.Datatype)))(imgdwt) __call__=Model
# -*- coding: utf-8 -*- ######################################################################################################################################### # # MyModel By Bertrand NOUVEL # 2009 CNRS Postdoctorate JFLI # # (c) All rights reserved # ############################################### # ################################################################################################################################################################################ # Includes ################################################################################################################################################################################ import numpy, scipy,sys from pycvf.core import genericmodel from pycvf.datatypes import basics from pycvf.datatypes import image from pycvf.datatypes import histogram from pycvf.lib.graphics.features import lbph ## ## This is simply a color model that is to apply by filtering ## Model=genericmodel.pycvf_model_function(image.Datatype,histogram.Datatype)(lbph) __call__=Model
def julius(snd): k=random.randint(0,99999999) tmpfile="/tmp/juliusexchg-%08d"%(k,) try: os.mkdir(tmpfile) infile=os.path.join(tmpfile,"in.wav") resfile=os.path.join(tmpfile,"out.txt") juliuscmd=""" export PATH=%s/bin:$PATH export LD_LIBRARY_PATH=%s/lib:$LD_LIBRARY_PATH cd %s julius in.wav > out.txt """%(juliuspath,juliuspath,tmpfile) txt=file(resfile).read() finally: try: os.unlink(resfile) except: pass try: os.unlink(infile) except: pass os.rmdir(tmpfile) return txt Model=genericmodel.pycvf_model_function(audio.datatype, basics.Label.datatype)(julius) __call__=Model
# # MyModel By Bertrand NOUVEL # 2009 CNRS Postdoctorate JFLI # # (c) All rights reserved # ############################################### # ################################################################################################################################################################################ # Includes ################################################################################################################################################################################ import numpy, sys from pycvf.core import genericmodel from pycvf.datatypes import basics def blockifier(gen,nb): i=iter(gen) try: while True: r=[] for x in range(nb): r.append(i.next()) yield r except StopIteration: if r!=[]: yield r Model=genericmodel.pycvf_model_function(generator.Datatype(basics.NumericArray.Datatype),generator.Datatype(basics.NumericArray.Datatype))(blockifier) __call__=Model
# # MyModel By Bertrand NOUVEL # 2009 CNRS Postdoctorate JFLI # # (c) All rights reserved # ############################################### # ################################################################################################################################################################################ # Includes ################################################################################################################################################################################ ### ### from pycvf.core import genericmodel from pycvf.datatypes import image import numpy,scipy,scipy.ndimage from pycvf.core.distribution import * pycvf_dist(PYCVFD_MODULE_STATUS, PYCVFD_STATUS_BETA) def angleofgradient(x,*args, **kwargs): g0=scipy.gradient(x,*args,**kwargs) return numpy.angle(g0[0]+g0[1]*1J) Model=genericmodel.pycvf_model_function(image.Datatype,image.Datatype)(angleofgradient) __call__=Model
import zopencv 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 Model=genericmodel.pycvf_model_function(image.Datatype,image.Datatype)(houghtlines) __call__=Model
from pycvf.core import genericmodel from pycvf.datatypes import image from pycvf.datatypes import basics from pycvf.core.distribution import * pycvf_dist(PYCVFD_MODULE_STATUS, PYCVFD_STATUS_EXPERIMENTAL) def snake(src,alpha=0.45,beta=0.2,gamma=0.45,max_iter=1000,epsilon=0.001,length=int(50),neigborhoodsize=(10,10)): CV_VALUE=1 if (src.ndim==2): src=src.reshape(src.shape+(1,)) if (src.shape[2]!=1): src=src.mean(axis=2) src=src.astype(numpy.uint8) w2,h2=src.shape[1]/2,src.shape[0]/2 points=(numpy.vstack([scipy.cos(numpy.arange(0,scipy.pi*2,scipy.pi*2./length))*w2+w2,scipy.sin(numpy.arange(0,scipy.pi*2,scipy.pi*2./length))*h2+h2])).T.astype(numpy.int32) alpha=numpy.array([alpha], dtype=numpy.float32) beta=numpy.array([beta], dtype=numpy.float32) gamma=numpy.array([gamma], dtype=numpy.float32) size=zopencv.cvSize(neigborhoodsize[0]|1,neigborhoodsize[1]|1) criteria=zopencv.CvTermCriteria() criteria.type=zopencv.CV_TERMCRIT_ITER+zopencv.CV_TERMCRIT_EPS; criteria.max_iter=max_iter; criteria.epsilon=epsilon; zopencv.cvSnakeImage( src, zopencv.memory_addr_of_numpy_array(points),length,alpha,beta, gamma,CV_VALUE,size,criteria,0 ); return points Model=genericmodel.pycvf_model_function(image.Datatype,basics.Label.Datatype)(snake) __call__=Model
# -*- coding: utf-8 -*- from pycvf.core import genericmodel from pycvf.datatypes import image import zopencv 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 Model=genericmodel.pycvf_model_function(image.Datatype,image.Datatype)(opencv_pyrsegmentation) __call__=Model
# -*- coding: utf-8 -*- ######################################################################################################################################### # # MyModel By Bertrand NOUVEL # 2009 CNRS Postdoctorate JFLI # # (c) All rights reserved # ############################################### # ################################################################################################################################################################################ # Includes ################################################################################################################################################################################ ### ### from pycvf.core import genericmodel from pycvf.datatypes import image import scipy, scipy.ndimage from pycvf.core.distribution import * pycvf_dist(PYCVFD_MODULE_STATUS, PYCVFD_STATUS_BETA) Model = genericmodel.pycvf_model_function(image.Datatype, image.Datatype)(scipy.ndimage.laplace) __call__ = Model
import numpy, scipy,sys from pycvf.core import genericmodel from pycvf.datatypes import basics from pycvf.datatypes import image def Pz(P): return (P[:,0]+1j*P[:,1]) def ANSIG_shape_feature(P,K=s128,robust=False): """ # ANSIG — An analytic signature for permutation-invariant two-dimensional shape representation # José J. Rodrigues, Pedro M. Q. Aguiar, and João M. F. Xavier # October 2008 (Thesis pdf) (Executive Summary - pdf) (Presentation - pptx) # Award: Professor Luís Vidigal Award 2008 / 2009. """ P-=P.mean(axis=0) #translation invariance P/=numpy.linalg.norm(P.std(axis=0)) # scale invariance N=P.shape[0] y=((Pz(P)[numpy.newaxis,:]).repeat(K,axis=0)*(numpy.exp(numpy.arange(K)*2*numpy.pi/K))[:,numpy.newaxis].repeat(N,axis=1)).mean(axis=1) if robust: P2=P[(numpy.asmatrix(P)*numpy.asmatrix(P.T))<1,:] y2=((Pz(P2)[numpy.newaxis,:]).repeat(K,axis=0)*(numpy.exp(numpy.arange(K)*2*numpy.pi/K))[:,numpy.newaxis].repeat(N,axis=1)).mean(axis=1) return y,y2,N return y,N Model=genericmodel.pycvf_model_function(shape.Datatype, basics.NumericalArray.Datatype)(ANSIG_shape_feature) __call__=Model
# -*- coding: utf-8 -*- ######################################################################################################################################### # # MyModel By Bertrand NOUVEL # 2009 CNRS Postdoctorate JFLI # # (c) All rights reserved # ############################################### # ################################################################################################################################################################################ # Includes ################################################################################################################################################################################ ### ### import os from pycvf.core.genericmodel import pycvf_model_function from pycvf.core import genericmodel from pycvf.datatypes import image import scipy import scipy.ndimage distance_transform_edt=scipy.ndimage.distance_transform_edt Model=pycvf_model_function(image.Datatype, image.Datatype)(distance_transform_edt) __call__=Model
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))) Model=genericmodel.pycvf_model_function(image.Datatype,basics.Label.Datatype)(largest_contour) __call__=Model
# # MyModel By Bertrand NOUVEL # 2009 CNRS Postdoctorate JFLI # # (c) All rights reserved # ############################################### # ################################################################################################################################################################################ # Includes ################################################################################################################################################################################ ### ### from pycvf.core import genericmodel from pycvf.datatypes import image import numpy,scipy,scipy.ndimage from pycvf.core.distribution import * pycvf_dist(PYCVFD_MODULE_STATUS, PYCVFD_STATUS_BETA) def DoG(x,value1=3,value2=1,*args,**kwargs): return numpy.abs(scipy.ndimage.gaussian_filter(x,value1,*args,**kwargs)-scipy.ndimage.gaussian_filter(x,value2,*args,**kwargs)) Model=genericmodel.pycvf_model_function(image.Datatype,image.Datatype)(DoG) __call__=Model
# -*- coding: utf-8 -*- ######################################################################################################################################### # # MyModel By Bertrand NOUVEL # 2009 CNRS Postdoctorate JFLI # # (c) All rights reserved # ############################################### # ################################################################################################################################################################################ # Includes ################################################################################################################################################################################ import numpy, sys from pycvf.core import genericmodel from pycvf.datatypes import image from pycvf.lib.graphics import colortransforms def colorconvert(x,fromto="rgb2hsv"): return (eval("colortransforms."+fromto))(x) Model=genericmodel.pycvf_model_function(image.Datatype,image.Datatype)(colorconvert) __call__=Model