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 ,如需转载请自行联系原作者
相关文章
|
2天前
|
人工智能 运维 安全
|
5天前
|
SpringCloudAlibaba 负载均衡 Dubbo
微服务架构下Feign和Dubbo的性能大比拼,到底鹿死谁手?
本文对比分析了SpringCloudAlibaba框架下Feign与Dubbo的服务调用性能及差异。Feign基于HTTP协议,使用简单,适合轻量级微服务架构;Dubbo采用RPC通信,性能更优,支持丰富的服务治理功能。通过实际测试,Dubbo在调用性能、负载均衡和服务发现方面表现更出色。两者各有适用场景,可根据项目需求灵活选择。
386 124
微服务架构下Feign和Dubbo的性能大比拼,到底鹿死谁手?
|
7天前
|
人工智能 JavaScript 测试技术
Qwen3-Coder入门教程|10分钟搞定安装配置
Qwen3-Coder 挑战赛简介:无论你是编程小白还是办公达人,都能通过本教程快速上手 Qwen-Code CLI,利用 AI 轻松实现代码编写、文档处理等任务。内容涵盖 API 配置、CLI 安装及多种实用案例,助你提升效率,体验智能编码的乐趣。
702 107
|
2天前
|
算法 Python
【轴承故障诊断】一种用于轴承故障诊断的稀疏贝叶斯学习(SBL),两种群稀疏学习算法来提取故障脉冲,第一种仅利用故障脉冲的群稀疏性,第二种则利用故障脉冲的额外周期性行为(Matlab代码实现)
【轴承故障诊断】一种用于轴承故障诊断的稀疏贝叶斯学习(SBL),两种群稀疏学习算法来提取故障脉冲,第一种仅利用故障脉冲的群稀疏性,第二种则利用故障脉冲的额外周期性行为(Matlab代码实现)
223 152
|
4天前
|
Java 数据库 数据安全/隐私保护
Spring 微服务和多租户:处理多个客户端
本文介绍了如何在 Spring Boot 微服务架构中实现多租户。多租户允许单个应用实例为多个客户提供独立服务,尤其适用于 SaaS 应用。文章探讨了多租户的类型、优势与挑战,并详细说明了如何通过 Spring Boot 的灵活配置实现租户隔离、动态租户管理及数据源路由,同时确保数据安全与系统可扩展性。结合微服务的优势,开发者可以构建高效、可维护的多租户系统。
203 127
|
4天前
|
Web App开发 前端开发 API
在折叠屏应用中,如何处理不同屏幕尺寸和设备类型的样式兼容性?
在折叠屏应用中,如何处理不同屏幕尺寸和设备类型的样式兼容性?
230 124
|
2天前
|
编解码 算法 自动驾驶
【雷达通信】用于集成传感和通信的OFDM雷达传感算法(Matlab代码实现)
【雷达通信】用于集成传感和通信的OFDM雷达传感算法(Matlab代码实现)
172 125
|
2天前
|
JavaScript 关系型数据库 MySQL
基于python的网上外卖订餐系统
本系统基于Python与Flask框架,结合MySQL数据库及Vue前端技术,实现了一个功能完善的网上订餐平台。系统涵盖餐品、订单、用户及评价管理模块,并深入研究订餐系统的商业模式、用户行为与服务质量。技术上采用HTML、PyCharm开发工具,支持移动端访问,助力餐饮业数字化转型。