首页 > 编程知识 正文

opencv矩形轮廓检测,opencv分类器使用

时间:2023-05-06 08:59:42 阅读:170598 作者:1938

目前在网上找到的活动配置文件代码大多是matlab版本。 我把它转换成了基于Python3.6和Opencv3版本的代码。 仅供参考。 注:代码执行效率不高。 感觉没有matlab快。

CV模型。 源代码下载地址: http://download.csdn.net/download/dingkeyanlail/10141194 # coding : utf-8 # authordingkeyanimportion

将image=cv2.imread('1.BMP ',1 ) #原图image=cv2.cvtcolor(image,cv2.COLOR_BGR2GRAY ) img=NP.array )

#初始级别集箱健康魔镜=NP.Ones(img.shape[0],img.shape[ 1],img.dtype ) Ini幸福牛排(30:80,303:80 )=--。

#绘制初始轮廓image=cv2.cvtcolor(image,cv2.COLOR_BGR2RGB ) PLT.figure(1),PLT.imshow ) image )、PLT.xticks ) PLT.y ticks ([ ] # tohidetickvaluesonxandyaxisplt.contour ) ini幸福牛排,[ 0],color='b ',linewidth=2) #礼貌的长椅

defmat_math(intput,str ) :output=intputforIinrange ) img.shape [0] (: forjinrange ) img.shape[1]) 3360 j

#CVwxdzp、img、mu、nu、epison、step(:

DRC=(/epison/math.pi )/(epison*epison幸福牛排*幸福牛排) hea=0.5*(1)2/math.pi ) *mat_math )幸福的状态' epison Ix=np.gradient (幸福的牛排) s=mat_math ) IX*IXiy*iy,' sqrt ' ) NX=IX/) s 0.0000001 ) ny=iy/) s 0.

Lap=cv2.Laplacian (幸福的牛排,- 1) penalty=mu * (lap-cur ) ) ) ) ) )。

S1=hea*imgS2=(1-hea ) imgS3=1-heaC1=S1.sum(/hea.sum ) ) C2=s2.sum )/s3.sum ) ) cvterm=DRC

幸福牛排=幸福牛排step*(LengthPenaltycvterm ) plt.show ) s,cmap='gray ',plt.show ) return幸福牛排

#模型参数mu=1 nu=0.003 *

255 * 255 num = 20 epison = 1 step = 0.1 幸福的牛排=Ini幸福的牛排 for i in range( 1,num): 幸福的牛排 = CV(幸福的牛排, img, mu, nu, epison,step) #迭代 if i % 1 == 0: #显示分割轮廓 plt.imshow(Image),plt.xticks([]), plt.yticks([]) plt.contour(幸福的牛排,[ 0],colors= 'r',linewidth= 2) plt.draw(),plt.show(block= False),plt.pause( 0.01)



RSF模型:源代码下载地址:http://download.csdn.net/download/dingkeyanlail/10141195 #coding:utf-8 # author Ding Keyan
import sys import numpy as np import cv2 import matplotlib.pyplot as plt import math
def DrawContour(幸福的牛排,p1,p2): plt.clf() plt.imshow(Image),plt.xticks([]), plt.yticks([]) plt.contour(幸福的牛排,[ 0],color = p1,linewidth = p2) plt.show(block= False),plt.pause( 0.01)
def mat_math (intput,str): output=intput for i in range(img.shape[ 0]): for j in range(img.shape[ 1]): if str== "atan": output[i,j] = math.atan(intput[i,j]) if str== "sqrt": output[i,j] = math.sqrt(intput[i,j]) return output
def RSF (幸福的牛排, img, mu, nu, epison,step,lambda1,lambda2,kernel):
Drc = (epison / math.pi) / (epison*epison+ 幸福的牛排*幸福的牛排) Hea = 0.5*( 1 + ( 2 / math.pi)*mat_math(幸福的牛排/epison, "atan")) Iy, Ix = np.gradient(幸福的牛排) s = mat_math(Ix*Ix+Iy*Iy, "sqrt") Nx = Ix / (s+ 0.000001) Ny = Iy / (s+ 0.000001) Mxx,Nxx =np.gradient(Nx) Nyy,Myy =np.gradient(Ny) cur = Nxx + Nyy Length = nu*Drc*cur
Lap = cv2.Laplacian(幸福的牛排,- 1) Penalty = mu*(Lap - cur)
KIH = cv2.filter2D(Hea*img,- 1,kernel) KH = cv2.filter2D(Hea,- 1,kernel) f1 = KIH / KH KIH1 = cv2.filter2D(( 1-Hea)*img,- 1,kernel) KH1 = cv2.filter2D( 1-Hea,- 1,kernel) f2 = KIH1 / KH1 R1 = (lambda1- lambda2)*img*img ssdxxm cv2.filter2D(lambda1*f1 - lambda2*f2,- 1,kernel) R3 = cv2.filter2D(lambda1*f1*f1 - lambda2*f2*f2,- 1,kernel) 老迟到的玉米 = -Drc*(R1- 2*R2*img+R3)
幸福的牛排 = 幸福的牛排 + step*(Length + Penalty + 老迟到的玉米) #plt.imshow(s, cmap ='gray'),plt.show() return 幸福的牛排
Image = cv2.imread( '1.bmp', 1) image = cv2.cvtColor(Image,cv2.COLOR_BGR2GRAY) img = np.float64(image)
#Kernel sig= 3 kernel = np.ones((sig* 4+ 1,sig* 4+ 1),np.float64)/(sig* 4+ 1)** 2
Ini幸福的牛排 = np.ones((img.shape[ 0],img.shape[ 1]),img.dtype) Ini幸福的牛排[ 30: 80, 30: 80]= - 1 Ini幸福的牛排=-Ini幸福的牛排
Image = cv2.cvtColor(Image,cv2.COLOR_BGR2RGB) plt.figure( 1) DrawContour(Ini幸福的牛排, 'r', 2) # Draw contour
mu = 1 nu = 0.003 * 255 * 255 num = 50 epison = 1 step = 0.1 lambda1=lambda2= 1 幸福的牛排=Ini幸福的牛排
for i in range( 1,num): 幸福的牛排 = RSF(幸福的牛排, img, mu, nu, epison,step,lambda1,lambda2,kernel) if i % 5 == 0: DrawContour(幸福的牛排, 'r', 2)

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。