基本全局阈值法(basic global thresholding)MATLAB实现

基本全局阈值分割步骤如下:

   (1)设定参数,并选择一个初始的估计阈值

   (2)用阈值分割图像。将图像分成两部分:是由灰度值大于的像素组成,是由灰度值小于或等于的像素组成。

   (3)计算中所有像素的平均灰度值,以及新的阈值

   (4)如果,则推出即为最优阈值;否则,将赋值给,并重复步骤(2)~(4),直到获取最优阈值。

其具体实现MATLAB代码如下:

 

%image_bgt.m

function level =image_bgt(I)

%该函数使用basic global thresholding算法实现二值化

I = im2double(I);          %对图像归一化

[M,N] = size(I);

T0 = 0.001;           %设置门限

T1 = (max(max(I)) +min(min(I)))/2;          %得到初始阈值T1

%定义G1、G2的下标,同时起统计个数的作用

columns1 = 1;

columns2 = 1;

%开始遍历

while 1

    for i = 1:M

        for j = 1:N

            if I(i,j)>T1

                G1(columns1) = I(i,j);          %得到分组G1

                columns1 = columns1 + 1;

            else

                G2(columns2) = I(i,j);          %得到分组G2

                columns2 = columns2 + 1;

            end

        end

    end

       %计算G1、G2均值

    ave1 = mean(G1);

    ave2 = mean(G2);

    T2 = (ave1 + ave2)/2;           %得到新阈值T2

    if abs(T2 - T1)<T0        %判断T2是否满足条件

        break;

    end

    T1 = T2;

    columns1 = 1;

    columns2 = 1;

end

level = T2;

end

 

%DIP_exp4_2.m

%该m文件调用image_bgt函数对图像进行basic global thresholding算法阈值分割

clc;clear;closeall;

I =imread('cameraman.tif');

level =image_bgt(I);            %得到basic global thresholding算法阈值

J = im2bw(I,level);                    %实现图像二值化,即非黑即白

subplot(121),imshow(I);title('原图像');

subplot(122),imshow(J);title('basicglobal thresholding后图像');

 

运行DIP_exp4_2.m文件得结果如下:

 

  • 14
    点赞
  • 76
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是一个基于基本全局阈值MATLAB函数basic_threshold的代码: ```matlab function output_image = basic_threshold(input_image, threshold_value) %BASIC_THRESHOLD Basic global thresholding algorithm implementation % This function implements the basic global thresholding algorithm, which % thresholds an input image into a binary image based on a threshold value. % Pixels with intensities lower than the threshold value are set to 0 % (black), while pixels with intensities greater than or equal to the % threshold value are set to 1 (white). % % INPUTS: % input_image - the input grayscale image to be thresholded % threshold_value - the threshold value to use for the thresholding % % OUTPUTS: % output_image - the binary thresholded image % Convert the input image to double precision for numerical calculations input_image = im2double(input_image); % Threshold the input image into a binary image using the threshold value output_image = input_image >= threshold_value; end ``` 你可以使用该函数来对任意一张医学图像进行基本全局阈值阈值处理。例如,假设你有一张名为```medical_image.png```的医学图像,你可以使用以下代码来读取该图像并进行基本全局阈值阈值处理: ```matlab % Read in the input medical image input_image = imread('medical_image.png'); % Apply basic global thresholding with a threshold value of 0.5 threshold_value = 0.5; output_image = basic_threshold(input_image, threshold_value); % Display the input and output images side-by-side for comparison figure(); subplot(1, 2, 1); imshow(input_image); title('Input Image'); subplot(1, 2, 2); imshow(output_image); title('Thresholded Image'); ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值