ostu进行遥感图像的分割

简介:



城市地区道路网的简单的阈值分割。采用的是单ostu(最佳阈值分割)算法,废话少说,如果不太清楚该算法,请参考文献[1]中的图像分割这一章的介绍。程序直接运行的效果如下。

 

直接附加代码,希望对大家有一些益处,节约你的时间:

复制代码
% 最大类方差法实现自适应图像阈值分割
% This implements is implemented by WENG, All rights reserved
% Copy Rights (c) WENG
% 2016-3-30 1639


clear;clc;close all;
warning off; %#ok<WNOFF>
IMG = imread('steger_perfect_1_rg.tif');% fdoa1_rs1_rg.tif
IMG_gray=double(rgb2gray(IMG));
[M,N]=size(IMG_gray);

gray_level=256;


% Histgram
Histgram_normal=zeros(256,1);% value span, 1~256
for i_level=1:256,
    Histgram_normal(i_level)=sum(sum(IMG_gray==(i_level+1)));
end
Histgram_normal=Histgram_normal/(M*N); % histgram normalize

m_overall=0;
for m=1:gray_level,m_overall=m_overall+(m-1)*Histgram_normal(m);end

sigma_max=0;
sigma_B=zeros(gray_level,1);
for i=1:gray_level,
    
    threshold=i-1;
    
    % calculate the number of the two class, the number is 0~1
    num1=0; 
    for m=1:threshold-1
        num1=num1+Histgram_normal(m);
    end
    num2=1-num1;
    
    % cal m1, m2
    m1=0;
    m2=0;
    for m=1:gray_level
        if m<threshold
            m1=m1+(m-1)*Histgram_normal(m);
        else
            m2=m2+(m-1)*Histgram_normal(m);
        end
    end
    
    % calculate the std betweent the two classes
    m1=m1/num1;
    m2=m2/num2;
    sigma1=num1*(m1-m_overall)^2+num2*(m2-m_overall)^2;
    
    % save and update
    sigma_B(i)=sigma1;
    if sigma1>sigma_max
        optical_T=threshold;
        sigma_max=sigma1;
    end
end

Th_optical=optical_T;
Th_matalb=graythresh(IMG_gray)*255;%matlab函数求阈值
fprintf(1,'The otsu threshold and Threshold calculate by graythresh are:\n %.1f [best], %.1f\n\n',Th_optical,Th_matalb);

% visual the result
Threshold_Optimal_IMG=zeros(M,N);
Threshold_graythresh_IMG=zeros(M,N);
Threshold_Optimal_IMG(IMG_gray>Th_optical)=1;
Threshold_graythresh_IMG(IMG_gray>Th_matalb)=1;
figure,subplot(2,2,1);imshow(IMG,[]);title('Ori color image');
subplot(2,2,2);imshow(IMG_gray,[]);title('Ori gray image');
subplot(2,2,3);imshow(Threshold_graythresh_IMG,[]);title('Threshold calculated by graythresh');
subplot(2,2,4);imshow(Threshold_Optimal_IMG,[]);title('Threshold calculated by ostu');


figure,subplot(1,2,1);
hold on; 
plot(1:gray_level,sigma_B);title('The two TH marked on \sigma array');
xlabel('Th'); ylabel('\sigma_B');
plot(Th_optical+1,sigma_B(Th_optical+1),'xg','MarkerSize',8,'LineWidth',2); 
text(Th_optical+1,sigma_B(Th_optical+1)-50,'otsu threshold');
plot(Th_matalb+1,sigma_B(Th_matalb+1),'^r','MarkerSize',8,'LineWidth',2); 
text(Th_matalb+1,sigma_B(Th_matalb+1)-50,'graythresh calculated threshold');
hold off;

subplot(1,2,2);plot(1:gray_level,Histgram_normal);title('Gray image Histgram');
hold on;
plot(Th_optical+1,Histgram_normal(Th_optical+1),'xg','MarkerSize',8,'LineWidth',2);
text(Th_optical+1,Histgram_normal(Th_optical+1)-50,'otsu threshold');
plot(Th_matalb+1,Histgram_normal(Th_matalb+1),'^r','MarkerSize',8,'LineWidth',2);
text(Th_matalb+1,Histgram_normal(Th_matalb+1)-50,'graythresh calculated threshold');
hold off;

% Image that can be classified by the threshold evaluate
Sigma_G=std(IMG_gray(:));
eta_ostu=sigma_B(Th_optical+1)/Sigma_G;
eta_matlab=sigma_B(Th_matalb+1)/Sigma_G;
fprintf(1,'The separability of ostu and matlab graythresh func respectively are:\n%.3f, %.3f\n',eta_ostu,eta_matlab);
复制代码

 

 

 

 

 

参考文献

[1] (美)冈萨雷斯(Gonzalez, R.C.), (美)伍兹(Woods,等. 数字图像处理[M]. 电子工业出版社, 2013.

没有整理与归纳的知识,一文不值!高度概括与梳理的知识,才是自己真正的知识与技能。 永远不要让自己的自由、好奇、充满创造力的想法被现实的框架所束缚,让创造力自由成长吧! 多花时间,关心他(她)人,正如别人所关心你的。理想的腾飞与实现,没有别人的支持与帮助,是万万不能的。




    本文转自wenglabs博客园博客,原文链接:http://www.cnblogs.com/arxive/p/5338000.html ,如需转载请自行联系原作者
相关文章
|
8天前
|
数据采集 人工智能 安全
|
4天前
|
机器学习/深度学习 人工智能 前端开发
构建AI智能体:七十、小树成林,聚沙成塔:随机森林与大模型的协同进化
随机森林是一种基于决策树的集成学习算法,通过构建多棵决策树并结合它们的预测结果来提高准确性和稳定性。其核心思想包括两个随机性:Bootstrap采样(每棵树使用不同的训练子集)和特征随机选择(每棵树分裂时只考虑部分特征)。这种方法能有效处理大规模高维数据,避免过拟合,并评估特征重要性。随机森林的超参数如树的数量、最大深度等可通过网格搜索优化。该算法兼具强大预测能力和工程化优势,是机器学习中的常用基础模型。
298 164
|
3天前
|
机器学习/深度学习 自然语言处理 机器人
阿里云百炼大模型赋能|打造企业级电话智能体与智能呼叫中心完整方案
畅信达基于阿里云百炼大模型推出MVB2000V5智能呼叫中心方案,融合LLM与MRCP+WebSocket技术,实现语音识别率超95%、低延迟交互。通过电话智能体与座席助手协同,自动化处理80%咨询,降本增效显著,适配金融、电商、医疗等多行业场景。
307 155
|
11天前
|
SQL 自然语言处理 调度
Agent Skills 的一次工程实践
**本文采用 Agent Skills 实现整体智能体**,开发框架采用 AgentScope,模型使用 **qwen3-max**。Agent Skills 是 Anthropic 新推出的一种有别于mcp server的一种开发方式,用于为 AI **引入可共享的专业技能**。经验封装到**可发现、可复用的能力单元**中,每个技能以文件夹形式存在,包含特定任务的指导性说明(SKILL.md 文件)、脚本代码和资源等 。大模型可以根据需要动态加载这些技能,从而扩展自身的功能。目前不少国内外的一些框架也开始支持此种的开发方式,详细介绍如下。
846 6
|
5天前
|
机器学习/深度学习 人工智能 前端开发
构建AI智能体:六十九、Bootstrap采样在大模型评估中的应用:从置信区间到模型稳定性
Bootstrap采样是一种通过有放回重抽样来评估模型性能的统计方法。它通过从原始数据集中随机抽取样本形成多个Bootstrap数据集,计算统计量(如均值、标准差)的分布,适用于小样本和非参数场景。该方法能估计标准误、构建置信区间,并量化模型不确定性,但对计算资源要求较高。Bootstrap特别适合评估大模型的泛化能力和稳定性,在集成学习、假设检验等领域也有广泛应用。与传统方法相比,Bootstrap不依赖分布假设,在非正态数据中表现更稳健。
239 113