基于LBP和LPQ特征融合的PSO-SVM缺陷分类附matlab代码

✅作者简介:热爱科研的Matlab仿真开发者,修心和技术同步精进,matlab项目合作可私信。

🍎个人主页:Matlab科研工作室

🍊个人信条:格物致知。

⛄ 内容介绍

钢板在生产及使用过程中产生的表面缺陷不仅影响外观还会降低产品的性能,针对目前检测的效率低,误差大提出了一种结合图像处理与粒子群优化支持向量机的缺陷分类检测系统.利用融合空域的局部二值模式和频域局部相位量化两种特征提取方式的优势对工件的图像进行缺陷特征提取,建立支持向量机(SVM)缺陷分类模型.由于SVM算法参数容易陷入局部最优的问题,所以采用粒子群算法优化SVM的惩罚参数和核函数.在MATLAB 2019b平台进行实验,实验结果对比分析显示,所提算法较传统的SVM分类模型相比提高了18.33%的识别准确率.

⛄ 部分代码

function results=CreateAndTrainANN(x,t)

    if ~isempty(x)

        

        % Choose a Training Function

        % For a list of all training functions type: help nntrain

        % 'trainlm' is usually fastest.

        % 'trainbr' takes longer but may be better for challenging problems.

        % 'trainscg' uses less memory. NFTOOL falls back to this in low memory situations.

        trainFcn = 'trainlm';  % Levenberg-Marquardt

        % Create a Fitting Network

        hiddenLayerSize = 10;

        net = fitnet(hiddenLayerSize,trainFcn);

        % Choose Input and Output Pre/Post-Processing Functions

        % For a list of all processing functions type: help nnprocess

        net.input.processFcns = {'removeconstantrows','mapminmax'};

        net.output.processFcns = {'removeconstantrows','mapminmax'};

        % Setup Division of Data for Training, Validation, Testing

        % For a list of all data division functions type: help nndivide

        net.divideFcn = 'dividerand';  % Divide data randomly

        net.divideMode = 'sample';  % Divide up every sample

        net.divideParam.trainRatio = 70/100;

        net.divideParam.valRatio = 15/100;

        net.divideParam.testRatio = 15/100;

        % Choose a Performance Function

        % For a list of all performance functions type: help nnperformance

        net.performFcn = 'mse';  % Mean squared error

        % Choose Plot Functions

        % For a list of all plot functions type: help nnplot

        net.plotFcns = {};

        % net.plotFcns = {'plotperform','plottrainstate','ploterrhist', 'plotregression', 'plotfit'};

        net.trainParam.showWindow=false;

        

        % Train the Network

        [net,tr] = train(net,x,t);

        % Test the Network

        y = net(x);

        e = gsubtract(t,y);

        E = perform(net,t,y);

        

    else        

        

        y=inf(size(t));

        e=inf(size(t));

        E=inf;

        

        tr.trainInd=[];

        tr.valInd=[];

        tr.testInd=[];

        

    end

    % All Data

    Data.x=x;

    Data.t=t;

    Data.y=y;

    Data.e=e;

    Data.E=E;

    

    % Train Data

    TrainData.x=x(:,tr.trainInd);

    TrainData.t=t(:,tr.trainInd);

    TrainData.y=y(:,tr.trainInd);

    TrainData.e=e(:,tr.trainInd);

    if ~isempty(x)

        TrainData.E=perform(net,TrainData.t,TrainData.y);

    else

        TrainData.E=inf;

    end

    

    % Validation and Test Data

    TestData.x=x(:,[tr.testInd tr.valInd]);

    TestData.t=t(:,[tr.testInd tr.valInd]);

    TestData.y=y(:,[tr.testInd tr.valInd]);

    TestData.e=e(:,[tr.testInd tr.valInd]);

    if ~isempty(x)

        TestData.E=perform(net,TestData.t,TestData.y);

    else

        TestData.E=inf;

    end

    

    % Export Results

    if ~isempty(x)

        results.net=net;

    else

        results.net=[];

    end

    results.Data=Data;

    results.TrainData=TrainData;

    % results.ValidationData=ValidationData;

    results.TestData=TestData;

    

end

⛄ 运行结果

⛄ 参考文献

[1]杜绪伟, 陈东. 基于LBP和LPQ特征融合的PSO-SVM缺陷分类[J]. 电子测量技术, 2020(021):043.

⛄ 完整代码

❤️部分理论引用网络文献,若有侵权联系博主删除

❤️ 关注我领取海量matlab电子书和数学建模资料

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
LBP-SVM(Local Binary Pattern-Support Vector Machine)是一种基于局部二值模式和支持向量机的图像分类方法。在Python中,可以使用OpenCV和scikit-learn库来实现LBP-SVM图像分类。 以下是一个简单的LBP-SVM图像分类示例代码: ```python import cv2 import numpy as np from sklearn.svm import SVC from sklearn.model_selection import train_test_split from sklearn.metrics import accuracy_score # 读取图像数据和标签 def read_data(): data = [] labels = [] for i in range(1, 11): for j in range(1, 6): img = cv2.imread(f"data/{i}_{j}.jpg", 0) lbp = local_binary_pattern(img, 8, 1) data.append(lbp.ravel()) labels.append(i) return np.array(data), np.array(labels) # 计算局部二值模式 def local_binary_pattern(img, num_points, radius): lbp = np.zeros_like(img) for i in range(radius, img.shape[0]-radius): for j in range(radius, img.shape[1]-radius): center = img[i, j] bin_str = '' for k in range(num_points): x = int(np.round(i + radius * np.cos(2 * np.pi * k / num_points))) y = int(np.round(j - radius * np.sin(2 * np.pi * k / num_points))) if img[x, y] > center: bin_str += '1' else: bin_str += '0' lbp[i, j] = int(bin_str, 2) return lbp # 划分训练集和测试集 data, labels = read_data() X_train, X_test, y_train, y_test = train_test_split(data, labels, test_size=0.2, random_state=42) # 训练SVM模型 clf = SVC(kernel='linear', C=1, gamma='auto') clf.fit(X_train, y_train) # 预测测试集并计算准确率 y_pred = clf.predict(X_test) acc = accuracy_score(y_test, y_pred) print(f"Accuracy: {acc}") ``` 该代码首先读取图像数据和标签,然后使用`local_binary_pattern`函数计算每张图像的局部二值模式。接着,使用`train_test_split`函数将数据划分为训练集和测试集。最后,使用`SVC`函数训练SVM模型并预测测试集,计算准确率。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

matlab科研助手

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值