【图像分割】基于遗传算法优化阈值实现道路分割matlab代码

131 阅读2分钟

1 简介

图像阈值分割技术在图像分析和图像识别中有着非常重要的意义。目前,现有的阈值分割算法在实际的应用领域中都存在各种各样的问题,诸如耗时性,目标的轮廓模糊,或者重要细节被忽视掉等等。本文针对这些问题展开一系列研究,选取了阈值分割法中经典的最大类间方差(Otsu)算法作为中心点展开研究。为了进一步克服Otsu算法的用时长,本文结合遗传算法及其特点,将遗传算法用于Otsu的图像分割方法中对阈值进行寻优,加快了算法的收敛速度。实验结果表明,本算法具有良好的实时性、分割效果好的特点

2 部分代码

%%%%%%%%%%%%%%%%遗传算法在道路图像阈值分割中的应用%%%%%%%%%%%%

function main()

clear all

close all

clc

global chrom oldpop fitness lchrom  popsize cross_rate mutation_rate yuzhisum

global maxgen  m n fit gen yuzhi A B C oldpop1 popsize1 b b1 fitness1 yuzhi1

A=imread('1.jpg');     %读入道路图像

A=imresize(A,0.4);

B=rgb2gray(A);         %灰度化

C=imresize(B,0.1);     %将读入的图像缩小

lchrom=8;              %染色体长度

popsize=10;            %种群大小

cross_rate=0.7;        %交叉概率

mutation_rate=0.4;     %变异概率

%%%%%%%%%%%%%%%%%%%%%%%%%变异%%%%%%%%%%%%%%%%%%%%%

function mutation()

global popsize lchrom mutation_rate temp newpop oldpop

sum=lchrom*popsize;    %总基因个数

mutnum=round(mutation_rate*sum);    %发生变异的基因数目

for i=1:mutnum

    s=rem((round(rand*(sum-1))),lchrom)+1; %确定所在基因的位数

    t=ceil((round(rand*(sum-1)))/lchrom); %确定变异的是哪个基因

    if t<1

        t=1;

    end

    if t>popsize

        t=popsize;

    end

    if s>lchrom

        s=lchrom;

    end

    if temp(t,s)==1

        temp(t,s)=0;

    else

        temp(t,s)=1;

    end

end

for i=1:popsize

    oldpop(i,:)=temp(i,:);

end

%%%%%%%%%%%%%%%%%%%%%%查看结果%%%%%%%%%%%%%%%%%%%%

function findresult()

global maxgen yuzhi m n C B A 

result=floor(yuzhi(1,maxgen)) %result为最佳阈值

C=imresize(B,0.3);

imshow(A);

title('原始道路图像')

figure;

subplot(1,2,1)

imshow(C);

title('原始道路的灰度图')

[m,n]=size(C);

%用所找到的阈值分割图象

for i=1:m

    for j=1:n

        if C(i,j)<=result

            C(i,j)=0;

        else

            C(i,j)=255;

        end

    end

end

subplot(1,2,2)

imshow(C);

title('阈值分割后的道路图');

3 仿真结果

4 参考文献

[1]周爱霞, 李春贵, 陶佳伟. 基于遗传算法的图像阈值分割的研究[C]// 广西计算机学会2015年学术年会论文集. 2015.

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

5 MATLAB代码与数据下载地址

见博客主页头条