越封 2024-02-21 08:08 采纳率: 66.7%
浏览 20
已结题

matlab svm二分类代码问题


imageDir = 'C:\Users\86150\Desktop\zyc_image\lesionimages\images\';
maskDir = 'C:\Users\86150\Desktop\zyc_image\masks\masks\';
imageFiles = dir(fullfile(imageDir, '*.jpg')); % 或者是你的图像格式
maskFiles = dir(fullfile(maskDir, '*.png')); % 假设mask是PNG格式



loadedData = load('C:\Users\86150\Desktop\zyc_image/groundtruth_labels.mat');
%groundtruth = loadedData.groundtruth_labels;
%groundtruth = cellfun(@(label) double(strcmp(label, 'malignant')), loadedData.groundtruth_labels(:,2));
labelsCell = loadedData.groundtruth_labels(:, 2);

% 转换标签为数值型(0为benign,1为malignant)
% labels = cellfun(@(x) double(strcmp(x, 'malignant')), labelsCell);

numImages = length(imageFiles);
indices = randperm(numImages);
%indices = randperm(numImages);

% 分配比例,例如70%训练,30%测试
numTrain = round(numImages * 0.7);
trainIndices = indices(1:numTrain);
testIndices = indices(numTrain+1:end);


% 创建训练集和测试集
trainImages = imageFiles(trainIndices);
testImages = imageFiles(testIndices);
trainMasks = maskFiles(trainIndices);
testMasks = maskFiles(testIndices);
groundtruth = labelsCell(trainIndices);
testlables = labelsCell(testIndices);
% 
% 
% % 假设extractEdgeFeatures函数如上所定义
% 
% 为训练图像初始化特征矩阵
imfeatures = zeros(numTrain, 4); % 根据需要调整特征数量
% 
% 为训练集中的每个图像提取特征
for i = 1:numTrain
    maskPath = fullfile(maskDir, trainMasks(i).name); % 获取当前图像的掩膜路径
    imfeatures(i, :) = extractEdgeFeatures(maskPath); % 提取特征并存储
end

% 归一化特征矩阵
% 最简单的归一化形式是将每个特征缩放到[0,1]区间
minFeatures = min(imfeatures, [], 1); % 计算每个特征的最小值
maxFeatures = max(imfeatures, [], 1); % 计算每个特征的最大值

% 防止除以零,如果某个特征的最大值和最小值相同,则将其设为1
rangeFeatures = maxFeatures - minFeatures;
rangeFeatures(rangeFeatures == 0) = 1;

% 对特征进行归一化
normFeatures = (imfeatures - minFeatures) ./ rangeFeatures;


rng(1); % 确保结果可复现
svm = fitcsvm(imfeatures, groundtruth);
cvsvm = crossval(svm, 'KFold', 10); % 10倍交叉验证
pred = kfoldPredict(cvsvm);

% 生成混淆矩阵并可视化
[cm, order] = confusionmat(groundtruth, pred);
confusionchart(cm, order);



function [features] = extractEdgeFeatures(maskPath)
    mask = imread(maskPath); % 读取mask图片
    mask = imbinarize(mask); % 将mask二值化
    perimeter = bwperim(mask, 8); % 获取边缘
    
    % 计算形状特征
    stats = regionprops(mask, 'Area', 'Perimeter', 'Eccentricity','solidity');
    
    % 特征向量
    features = zeros(1, 4);
    if ~isempty(stats)
        area = stats.Area;
        perimeter = stats.Perimeter;
        eccentricity = stats.Eccentricity;
        
        % 边缘平滑度,周长平方除以面积
        smoothness = perimeter^2 / area;
        solidity = stats.Solidity;
        features = [area, smoothness, eccentricity,solidity];
    end
end



我想设计一个基于计算机视觉的系统,该系统能够区分恶性黑色素瘤和良性皮肤病变的图像,原始图像已经处理好成为mask

img


下图是groundtruth_labels的数据

img


输出结果如下图

img

请问怎么才能提高准确率!谢谢

  • 写回答

17条回答 默认 最新

  • 专家-赤兔[在线] 优质创作者: 编程框架技术领域 2024-02-21 08:09
    关注
    获得0.45元问题酬金

    引自免费微信小程序:皆我百晓生

    提高基于计算机视觉的黑色素瘤和良性皮肤病变图像分类系统的准确率,可以从以下几个方面入手:

    1. 数据增强

      • 通过旋转、翻转、缩放、裁剪等方法增加训练集的多样性,减少过拟合。
      % 假设dataAugmentation是一个自定义的数据增强函数
      for i = 1:numTrain
          augmentedImgs = dataAugmentation(imread(fullfile(imageDir, trainImages(i).name)));
          augmentedMasks = dataAugmentation(imread(fullfile(maskDir, trainMasks(i).name)), 'mask');
      
          for j = 1:size(augmentedImgs, 4)
              imgFeature = extractEdgeFeatures(fullfile('.', augmentedMasks{j}));
              normFeatures = [normFeatures; (imgFeature - minFeatures) ./ rangeFeatures];
              groundtruth = [groundtruth; labelsCell(trainIndices(i))];
          end
      end
      
    2. 特征工程

      • 除了上述代码中的边缘和平滑度特征外,可以尝试提取更多有助于区分恶性与良性病变的特征,例如纹理特征、颜色直方图、局部二值模式(LBP)等。
    3. 选择合适的SVM核函数

      • 尝试不同的核函数,如线性核、高斯核(RBF)、多项式核等,并通过交叉验证找到最优核函数参数。
      svmModel = fitcsvm(normFeatures, groundtruth, 'KernelFunction', 'rbf', 'KernelScale', 'auto', ...
                         'BoxConstraint', C, 'Standardize', true); % C是正则化参数,需通过网格搜索或交叉验证优化
      
    4. 模型优化

      • 使用网格搜索调整SVM参数,比如C值(正则化强度)和gamma(核函数相关参数)。
      [gridOpt, bestAccuracy] = fitcsvmTuning(normFeatures, groundtruth, 'KernelFunction', 'rbf');
      svmModel = fitcsvm(normFeatures, groundtruth, 'KernelFunction', 'rbf', 'KernelScale', gridOpt.KernelScale, ...
                         'BoxConstraint', gridOpt.C, 'Standardize', true);
      
    5. 集成学习

      • 可以尝试使用集成方法,如bagging、boosting或随机森林,结合多个SVM模型来提高整体性能。
    6. 深度学习

      • 考虑使用卷积神经网络(CNN),如VGG、ResNet等,直接从原始图像中学习高级特征,而不是手动设计特征。
    7. 样本平衡

      • 如果两类样本数量不平衡,可以通过欠采样、过采样或SMOTE技术进行处理。
    8. 评估与反馈

      • 在测试集上不断迭代优化模型,并观察混淆矩阵变化,针对性地解决模型在特定类别上的误判问题。

    请注意以上部分代码仅为示例,实际应用时需要根据具体情况进行编写。对于第4点提到的fitcsvmTuning函数,MATLAB并未自带该功能,你需要自行实现一个网格搜索函数或者利用现成的工具箱(如GridSearchCV)。同时,在实际项目中,务必确保所有步骤都进行了适当的验证,并遵循良好的机器学习实践原则。

    评论 编辑记录

报告相同问题?

问题事件

  • 系统已结题 2月29日
  • 创建了问题 2月21日