zoukankan      html  css  js  c++  java
  • MATLAB图像分析程序

    1.迭代法
    I=imread('rice.png');
    ZMax=max(max(I));
    ZMin=min(min(I));
    TK=(ZMax+ZMin)/2;
    bCal=1;
    iSize=size(I);
    while(bCal)
    iForeground=0;
    iBackground=0;
    ForegroundSum=0;
    BackgroundSum=0;
    for i=1:iSize(1)
    for j=1:iSize(2)
    tmp=I(i,j);
    if(tmp>=TK)
    iForeground=iForeground+1;
    ForegroundSum=ForegroundSum+double(tmp);
    else
    iBackground=iBackground+1;
    BackgroundSum=BackgroundSum+double(tmp);
    end
    end
    end
    ZO=ForegroundSum/iForeground;
    ZB=BackgroundSum/iBackground;
    TKTmp=uint8((ZO+ZB)/2);
    if(TKTmp==TK)
    bCal=0;
    else
    TK=TKTmp;
    end
    end
    disp(strcat('迭代后的域值:',num2str(TK)));
    newI=im2bw(I,double(TK)/255);
    subplot(121),imshow(I)
    subplot(122),imshow(newI)
    运行结果:迭代后的域值:131
    2.大津法
    %大津法
    I=imread('coins.png');
    subplot(131),imshow(I);
    title('原始图像')
    level=graythresh(I);
    BW=im2bw(I,level);
    subplot(132),imshow(BW)
    title('graythresh计算阈值')
    disp(strcat('graythresh计算灰度阈值:',num2str(uint8(level*255))))
    iMax=max(max(I));
    iMin=min(min(I));
    T=double(iMin:iMax);
    iSize=size(I);
    muxSize=iSize(1)*iSize(2);
    for i=1:length(T)
    TK=T(1,i);
    iForeground=0;
    iBzckground=0;
    ForegroundSum=0;
    BzckgroundSum=0;
    for j=1:iSize(1)
    for k=1:iSize(2)
    tmpData=I(j,k);
    if(tmpData>=TK)
    iForeground=iForeground+1;
    ForegroundSum=ForegroundSum+double(tmpData);
    else
    iBackground=iBackground+1;
    BackgroundSum=BackgroundSum+double(tmpData);
    end
    end
    end
    w0=iForeground/muxSize;
    w1=iBackground/muxSize;
    u0=ForegroundSum/iForeground;
    u1=BackgroundSum/iBackground;
    T(2,i)=w0*w1*(u0-u1)*(u0-u1);
    end
    oMax=max(T(2,:);
    idx=find(T(2,:)>=oMax);
    T=uint8(T(1,idx));
    disp(strcat('简化大津法计算灰度阈值:',num2str(T)))
    BW=im2bw(I,double(T)/255);
    subplot(133),imshow(BW)
    title('简化大津法计算灰度阈值')运行结果:
    graythresh计算灰度阈值:126
  • 相关阅读:
    深入理解 ProtoBuf 原理与工程实践(概述)
    高性能缓存 Caffeine 原理及实战
    Java 多线程上下文传递在复杂场景下的实践
    SpringBoot 2.0 中 HikariCP 数据库连接池原理解析
    MySQL 5.6.35 索引优化导致的死锁案例解析
    gitlab安装升级(大版本跨度9.4.5----13.2.1)
    mysql 查看表的索引
    python安装mysql库 ,MySQL-python
    Alpine包管理工具apk使用介绍
    docker容器添加hosts
  • 原文地址:https://www.cnblogs.com/nktblog/p/2489604.html
Copyright © 2011-2022 走看看