⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 segment.m

📁 利用matlab实现彩色图像的分割。算法主要是利用聚类算法。
💻 M
字号:
function [finalMasks, finalConfs]= segment(filename, distances, K, LAB_DIFF, PERCENTAGE, PERCEPTUAL)CONFIDENCE_THRESHOLD = 0.8;disp('********************');disp('Pre-proceesing Image')disp('********************');[pyr,masks,confidences,stds,means] = preprocess(filename,distances, K, LAB_DIFF, PERCENTAGE, PERCEPTUAL);lastMasks = masks;lastPtheta = confidences;numLevels = size(pyr,4);disp('********************');disp('Begin relaxation')disp('********************');% work our way up the pyramidfor pyrind = numLevels-1:-1:1,    disp(sprintf('Working on level %d',pyrind));    currImage = pyr(:,:,:,pyrind);    [h,w,c] = size(currImage);        % this is the one done with color histograms    currPx = calculatePXTheta(currImage, lastMasks);        % this is the Q function    currQ = calculateQ(currImage, lastMasks, currPx, lastPtheta);        % get the new prob assignments    confs = currPx.*lastPtheta.*currQ;        % now we need to normalize across clusters    sumConfs = sum(confs,3);    disp('normalizing all confidences');    numClusters = size(confs,3);    for ind=1:numClusters,        confs(:,:,ind) = confs(:,:,ind) ./ sumConfs;    end     % create the new masks% form a set of new masks and confidences by taking out any empty regions    newMasks = zeros(h,w,0);    newPtheta = zeros(h,w,0);    %newPtheta = confs;        disp('making new masks');    for j=1:numClusters,        % go thru each plane of confidences and pick out the ones with conf > THRESHOLD        mask = (confs(:,:,j) >= CONFIDENCE_THRESHOLD);        if (sum(mask(:)) ~= 0)            disp(sprintf('cluster %d is not empty --> adding to output',j));            newMasks(:,:,end+1) = mask;            newPtheta(:,:,end+1) = confs(:,:,j);            %else            %disp(sprintf('cluster %d is empty --> adding to output',j));            %newMasks(:,:,end+1) = mask;        end   end        showClusters(newMasks);    title(sprintf('core clusters at level %d',pyrind));        % set stuff up for the next iteration    lastMasks = newMasks;    lastPtheta = newPtheta;end% return the final set of masks and confidences (ON THE LAST LEVEL...MAYBE USE MAX INSTEAD OD 80%)finalConfs = lastPtheta;[h,w,c] = size(finalConfs);% for the last level, we're going to create masks without using the threshold. just take the max.finalMasks = zeros(h,w,c);for j=1:numClusters,    % go thru each plane of confidences and pick out the ones with conf > THRESHOLD    finalMasks(:,:,j) = (finalConfs(:,:,j) == max(finalConfs,[],3));endshowClusters(finalMasks);title('final clusters');

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -