基于隶属度函数的平均值的ostu阈值分割割算法怎么用matlab编写

查看: 16717|回复: 10|关注: 0
关于matlab Otsu阈值分割中的阈值问题!!!
<h1 style="color:# 麦片财富积分
新手, 积分 13, 距离下一级还需 37 积分
用Otsu方法进行图像阈值分割:
I = imread('coins.png');
level = graythresh(I);
BW = im2bw(I,level);
可是求得的level为什么是0到1之间的数呢?
怎样求实际的阈值?:Q
[ 本帖最后由 edifiers2008 于
18:28 编辑 ]
<h1 style="color:#9 麦片财富积分
关注者: 28
level is a normalized intensity value that lies in the range [0, 1].
help里不是说的很清楚吗?
1 提问请直接在论坛中发帖,不要发站内消息给我。
2 不要在QQ中问我提问,这样很浪费时间
<h1 style="color:# 麦片财富积分
这个我看到了,但是为什么会是0到1之间呢?
假如一个灰度图像每个像素点的灰度值是0到255之间,用最大类间方差法求得的阈值也应该是0到255之间的一个数,为什么会是0到1之间呢? 怎样求得实际阈值呢?
例如:灰度图像img的矩阵如下:
&&[& && & 0& && && &0& & 0.1000& &10.0000& &11.0000
& & 9.0000& & 7.00&&224.00
& & 8.0000& & 6.00&&220.0000& & 1.0000
&&255.00& & 7.0000& & 6.0000& & 2.0000&&]
&& level = graythresh(img);
求得阈值:
level =0.5490
但是将上述图像的像素点分为两类的阈值很明显应该是11和219之间的一个数???
有点糊涂了~~:(
<h1 style="color:#9 麦片财富积分
关注者: 28
matlab图像处理中,有的函数是会将256色等图像转换成double型的。所以就是0-1之间的。
1 提问请直接在论坛中发帖,不要发站内消息给我。
2 不要在QQ中问我提问,这样很浪费时间
<h1 style="color:# 麦片财富积分
那上例中实际的阈值应该是多少呢?
level *255=0..9950& &对吗?
<h1 style="color:#9 麦片财富积分
关注者: 28
应该是的。
1 提问请直接在论坛中发帖,不要发站内消息给我。
2 不要在QQ中问我提问,这样很浪费时间
<h1 style="color:# 麦片财富积分
:( 先这样试试吧
<h1 style="color:# 麦片财富积分
/******************************************************************************
&&This program has realized the algorithm of Otsu histogram thresholding. For the
&&detail of the algorithm, please refer to
&&Otsu. N. A thresholding selection method from grey-level histograms.
&&IEEE Trans. on Systems, Man, and Cybernetics. ): 377-393.
&&The program was written by Y.B. Mao at the Dept. of Automation,
&&NJUST, P.R.China, on May, 1st, 2005.
&&All rights reserved.
******************************************************************************/
# include &stdio.h&
# include &math.h&
# include &stdlib.h&
# include &string.h&
# include &mex.h&
# define& &EPSILON& &0.000001
/****************************************************************
计算前t级灰度的概率和
输入参数:
double * p:& && && && && & 灰度概率密度分布
int t:& && && && && && && &前t级
返回值:& && && && && && &&&前t级灰度概率和
****************************************************************/
double Calcu_p_t( double * p, int t )
& & & & double sum = 0;
& & & & for ( i = 0; i &= i++ )
& & & & & & & & sum = sum + p;
& & & & return( sum );
/****************************************************************
计算均值程序
输入参数:
double * p:& && && && && & 灰度概率密度分布
int t:& && && && && && && &前t级
返回值:& && && && && && &&&灰度均值
****************************************************************/
double Calcu_u( double * p, int t )
& & & & double sum = 0;
& & & & for ( i = 0; i &= i++ )
& & & & & & & & sum = sum + p *
& & & & return( sum );
/****************************************************************
计算方差程序
输入参数:
double * p:& && && && && & 灰度概率密度分布
double u:& && && && && && &均值
int t:& && && && && && && &前t级
返回值:& && && && && && &&&灰度方差值
****************************************************************/
double Calcu_sigma( double * p, double u, int t )
& & & & double sum = 0;
& & & & for ( i = 0; i &= i++ )
& & & & & & & & sum = sum + p * ( i - u) * ( i - u );
& & & & return( sum );
/****************************************************************
Otsu法对图像进行阈值化
输入参数:
int height:& && && && && &&&图像高(行数)
int width:& && && && && && &图像宽(列数)
unsigned char * input_image:读入的图像数据,对于灰度图像大小为WxH字节;
输出参数:
unsigned char * output_image: 二值化后的图像(为易于显示,值为0或255)
int &threshold:& && && && &&&选择出的最佳阈值
****************************************************************/
void Otsu( unsigned char * input_image, unsigned char * output_image,
& & & & & & & && & int height, int width, int & threshold )
& & & & int x,& && && && && &&&// x, y坐标
& & & & int i, G
& & & & int pi[256];& && && && &&&// 图像灰度直方图
& & & & double p[256];& && && && &// 归一化直方图
& & & & double eta[256], maxE&&// sigma_b^2/sigma_t^2
& & & & double u_T, sigma_t;& && &// 全局灰度平均值和方差
& & & & double u_t, u_0, u_1, w_0, w_1, sigma_b;&&// 类间方差计算变量
& & & & /* (1) 计算图像的归一化直方图 */
& & & & for ( i = 0; i & 256; i++ ) pi = 0;
& & & & for ( y = 0; y & y++ )
& & & & & & & & for ( x = 0; x & x++ )
& & & & & & & & {
& & & & & & & & & & & & Gray = input_image[ y*width+x ];
& & & & & & & & & & & & pi[ Gray ]++;
& & & & & & & & }
& & & & for ( i = 0; i & 256; i++ ) p = pi / (double)( height * width );
& & & & /* (2) 计算全局灰度均值和方差 */
& & & & u_T = Calcu_u( p, 255 );
& & & & sigma_t = Calcu_sigma( p, u_T, 255 );
& & & & if ( sigma_t & EPSILON ) sigma_t = 1.0; // 出错处理
& & & & /* 循环求各个eta值 */
& & & & for ( i = 0; i & 256; i++ )
& & & & & & & & eta[ i ] = 0;
& & & & & & & & /* (3) 当阈值取为i时,计算类间方差 */
& & & & & & & & w_0 = Calcu_p_t( p, i );
& & & & & & & & w_1 = 1.0 - w_0;
& & & & & & & & u_t = Calcu_u( p, i );
& & & & & & & & if ( w_0 &= EPSILON )
& & & & & & & & & & & & u_0 = u_t / w_0;
& & & & & & & & else
& & & & & & & & & & & & u_0 = 1;
& & & & & & & & if ( ( 1-w_0 &= EPSILON ) )
& & & & & & & & & & & & u_1 = ( u_T - u_t )/( 1 - w_0 );
& & & & & & & & else
& & & & & & & & & & & & u_1 = 1;
& & & & & & & & sigma_b = w_0 * w_1 * ( u_0 - u_1 ) * ( u_0 - u_1 );
& & & & & & & & /* (4) 计算类间方差和全局方差的比值eta */
& & & & & & & & eta = sigma_b / sigma_t;
& & & & /* (5) 取max (eta)时的i作为最佳阈值 */
& & & & maxEta = eta[0];
& & & & for ( i = 0; i & 256; i++ )
& & & & & & & & if ( eta &= maxEta )
& & & & & & & & {
& & & & & & & & & & & & threshold =
& & & & & & & & & & & & maxEta = eta;
& & & & & & & & }
& & & & /* (6) 用计算获得的最佳阈值,对图像进行阈值化 */
& & & & for ( y = 0; y & y++ )
& & & & & & & & for ( x = 0; x & x++ )
& & & & & & & & {
& & & & & & & & & & & & Gray = input_image[ y*width+x ];
& & & & & & & & & & & &
& & & & & & & & & & & & & & & & if ( Gray &= threshold )
& & & & & & & & & & & & & & & & & & & & output_image[ y*width+x ] = 255;
& & & & & & & & & & & & & & & & else
& & & & & & & & & & & & & & & & & & & & output_image[ y*width+x ] = 0;
& & & & & & & & }
Mex interface function of Matlab
void mexFunction( int nlhs, mxArray * plhs[], int nrhs, const mxArray * prhs[] )
& & int H, W, C;
& & int number_of_dims, iBitC
& & const int * dim_
& & int dims[3];
& & double * orig_img, * seg_
& & int x,
& & & & unsigned char * in_image, * out_
& & & & char echo[10], *
& & & & bool ECHO =
& & /* check the validity of input and output arguments */
& & if ( nrhs & 1 || nrhs & 2 )
& & & & & & & & mexPrintf( &Usage of Otsu histogram thresholding routine:\n& );
& & & & & & & & mexPrintf( &biLevelImage = OtsuSegment( OriginalImage, [if_echo] );\n& );
& & & & & & & & mexPrintf( &where\n& );
& & & & & & & & mexPrintf( &OriginalImage is the image to be segmented\n& );
& & & & & & & & mexPrintf( &and biLevelImage is the result image.\n& );
& & & & & & & & mexPrintf( &Optional input parameter [if_echo] is either 'echo' or 'noecho' to decide whether to display the found threshold or not.\n& );
& & & & & & mexErrMsgTxt( &Wrong invoke format!& );
& & & & & &
& & if ( nlhs & 1 )
& & & & & & mexErrMsgTxt( &Too many output arguments.& );
& & & & & &
& & /* Get the number of dimensions in the input argument. */
& & number_of_dims=mxGetNumberOfDimensions(prhs[0]);
& & dim_array = mxGetDimensions(prhs[0]);
& & if ( number_of_dims == 3 )
& & & & & & & & iBitCount = 24;& && && && && &&&/* 24 bits image& && && &*/
& & & & & & & & H = dim_array[0];
& & & & & & & & W = dim_array[1];
& & & & & & & & C = dim_array[2];
& & & & & & iBitCount = 8;& && && && && && &/* 256 grayscale image& &*/
& & & & & & & & H = dim_array[0];
& & & & & & & & W = dim_array[1];
& & & & & & & & C = 1;
& & if (!mxIsDouble(prhs[0]))
& & & & & & & & mexErrMsgTxt(&Elements in image matrix should be of double type.&);
& & & & & & & &
& & orig_img = mxGetPr(prhs[0]);
& & /* check the second parameter if there is one */
& & if ( nrhs == 2 )
& & & & & & & & /* Check the second parameter to be sure it is of type char. */
& & & & & & & & if ( !mxIsChar(prhs[1]) )
& & & & & & & & {
& & & & & & & & & & & & mexErrMsgTxt(&The second input parameter, if_echo, should be either 'echo' or 'noecho'.&);
& & & & & & & & & & & &
& & & & & & & & }
& & & & & & & & /* Copy the string data from prhs and place it into str. */
& & & & & & & & str = mxArrayToString(prhs[1]);
& & & & & & & & strcpy( echo, str );
& & & & & & & & if ( !strcmp( echo, &echo&) ) ECHO =
& & & & & & & & else ECHO =& & & & & & & &
& & /* create output matrix */
& & dims[0] = H; dims[1] = W; dims[2] = 1;
& & plhs[0] = mxCreateNumericArray( 3, dims, mxDOUBLE_CLASS, mxREAL );
& & seg_img = mxGetPr(plhs[0]);
& & & & /* begin operation */
& & & & in_image = new unsigned char[W*H];& & // allocate memory to save in and out metrix
& & & & out_image = new unsigned char[W*H];
& & & & if ( number_of_dims == 3 )&&// convert true color image to grey-scale one
& & & & & & & & for ( x = 0; x & W; x++ )
& & & & & & & && &&&for ( y = 0; y & H; y++ )
& & & & & & & && &&&{& &
& & & & & & & & & & & & & & & & in_image[x+y*W] = (unsigned char)(( 0.299 * orig_img[y+x*H] + 0.587 * orig_img[y+x*H+W*H]
& & & & & & & & & & & & & & & & & & & && && && && & + 0.114 * orig_img[y+x*H+W*H*2] ));
& & & & & & & & & & & & }
& & & & else& && && && && && && && &// grey-scale image
& & & & & & & & for ( x = 0; x & W; x++ )
& & & & & & & && &&&for ( y = 0; y & H; y++ )
& & & & & & & && &&&{& &
& & & & & & & & & & & & & & & & in_image[x+y*W] = (unsigned char)orig_img[y+x*H];
& & & & & & & & & & & & }
& & & & int threshold = 128;
& & & & Otsu( in_image, out_image, H, W, threshold );
& & & & if ( ECHO )
& & & & & & & & mexPrintf( &The optimal threshold for this image is: %d .\n&, threshold );
& & & & // output the result image
& & & & for ( x = 0; x & W; x++ )
& & & && &&&for ( y = 0; y & H; y++ )
& & & && &&&{& &
& & & & & & & & & & & & seg_img[y+x*H] = (double)out_image[x+y*W];
& & & & & & & & }
& & & & delete in_&&// clean out
& & & & delete out_
<h1 style="color:# 麦片财富积分
% ***************************************************************************************************************************************************************
% OTSU法对于具有双峰性质的灰度图像或是彩色图像的某一通道的分割效果很好,程序为了增加健壮性加了个可以根据实际情况确定的修正值th_set.
% **************************************************************************************************************************************************************
function T11=OTSU(a2)
%gray=rgb2gray(a2);%原图像的灰度图
%low_high=stretchlim(gray);%增强图像,似乎也不是一定需要
%gray=imadjust(gray,low_high,[]);
% subplot(224);imshow(gray);title('after adjust');
%imhist(gray);
count=imhist(a2);
% for i=2:l
%& && && &if count(i)~=0
%& && && && & NewGray(j)=count(i);
%& && && && & j=j+1;
%& && && &end
% NewGray=NewGray';
% NewCount=j-1;
& && &&&if count(i)~=0
& && && && &st=i-1;
& && && && &break
& && &&&end
%以上循环语句实现寻找出现概率不为0的最小灰度值st
for i=l:-1:1
& && &&&if count(i)~=0;
& && && && &nd=i-1;
& && && && &break
& && &&&end
st=1;nd=255;
%以上实现找出出现概率不为0的最大灰度值nd
%Aver=0.5*(st+nd);%求出灰度范围的中值
Tx=0;Ty=0;Txd=0;Tyd=0;T0=uint8(Aver);
&&for i=st:T0
& && &&&Tx=double(Tx+count(i)*double(i));
& && &&&Txd=Txd+count(i);
& & Tx1=Tx/T
& & for i=T0+1:nd
& && &&&Ty=double(Ty+count(i)*double(i));
& && &&&Tyd=Tyd+count(i);
& & Ty1=Ty/T
& &T1=0.5*(Tx1+Ty1);
while ( abs(T1-T0)&=0.0006)
& & for i=st:T0
& && &&&Tx=double(Tx+count(i)*double(i));
& && &&&Txd=Txd+count(i);
& & Tx1=Tx/T
& & for i=T0+1:nd
& && &&&Ty=double(Ty+count(i)*double(i));
& && &&&Tyd=Tyd+count(i);
& & Ty1=Ty/T
& & T0=T1;
& & T1=uint8(0.5*(Tx1+Ty1));
T11=uint8(T1);
<h1 style="color:# 麦片财富积分
谢谢大家帮忙,不过运行上面的程序出现了错误:
&& J=imread('11-46-24.jpg');
&& I1 = imresize(J, 0.5);
&& I=rgb2gray(I1);
&& otsu(I);
??? Attempted to access count(195.16); index must be a positive integer or logical.
Error in ==& otsu at 57
& && &&&Ty=double(Ty+count(i)*double(i));
能告诉我用graythresh函数怎样求得实际的阈值吗?
不知道为什么用上面函数求得的阈值都是0和1之间的。
站长推荐 /2
Powered bythresh 用matlab编写的最大方差法计算灰度分割门限代码,用该函数实现利用 阈值 图像的
238万源代码下载-
&文件名称: thresh
& & & & &&]
&&所属分类:
&&开发工具: matlab
&&文件大小: 1 KB
&&上传时间:
&&下载次数: 18
&&提 供 者:
&详细说明:用matlab编写的最大方差法计算灰度分割门限代码,用该函数实现利用灰度阈值分割图像的功能。-Written by matlab calculate the maximum variance threshold gray code split, with the functions for image segmentation using gray scale features.
文件列表(点击判断是否您需要的文件,如果是垃圾请在下面评价投诉):
&&thresh.m
&近期下载过的用户:
&相关搜索:
&输入关键字,在本站238万海量源码库中尽情搜索:
&[] - 应用Matlab工具箱演示对比度增强,局部平滑,中值滤波,小波工具,边缘检测,图像二值化,Hough变换直线提取,灰度阈值分割,四叉树分裂合并法等,并完成这些处理程序的GUI集成。
&[] - 该gui函数基本上包括图像处理里面的最基本处理,相当于一个小型photoshop。比如读取文件,几何变换中的垂直镜像,平移,旋转,缩放;正交变换的DFT,FFT,DCT,DST,DHT,DWashT;灰度处理中的反色,直方图均衡,全局线性变换,分段线性变换,指数非线性变换,对数非线性变换;图像增强里
&[] - K均值图像分割,读入彩色图片,输出区域分割后的图片
&[] - 图像分割,实现最大实现最大方差法计算分割门限
&[] - 最大方差自动取阈法和分段线性变换的MATLAB程序
&[] - 本程序是一个最佳阈值分割算法,可对灰度图像进行分分割.图像OTSU阈值分割的程序设计_百度文库
两大类热门资源免费畅读
续费一年阅读会员,立省24元!
图像OTSU阈值分割的程序设计
上传于||文档简介
&&m&#8203;a&#8203;t&#8203;l&#8203;a&#8203;b&#8203;制&#8203;作&#8203;的&#8203;相&#8203;关&#8203;课&#8203;程&#8203;设&#8203;计
阅读已结束,如果下载本文需要使用5下载券
想免费下载本文?
定制HR最喜欢的简历
下载文档到电脑,查找使用更方便
还剩11页未读,继续阅读
定制HR最喜欢的简历
你可能喜欢图像处理实验二_百度文库
两大类热门资源免费畅读
续费一年阅读会员,立省24元!
图像处理实验二
上传于||暂无简介
阅读已结束,如果下载本文需要使用1下载券
想免费下载本文?
定制HR最喜欢的简历
下载文档到电脑,查找使用更方便
还剩6页未读,继续阅读
定制HR最喜欢的简历
你可能喜欢Matlab优化方法之黄金分割算法
% 优化方法之黄金分割算法
% 黄金分割算法适用于一元函数f(x)在给定区间[a, b]内搜索极小点的问题
% 其基本原理为: 按照黄金分割比例原则逐步缩小搜索区间, 可类比二分法, 二分法是取a和b的中点逐渐缩小搜索空间,
而黄金分割算法是取a和b的黄金分割点
一、Matlab脚本文件,在此文件进行相应修改,然后运行即可
% 1.设置要求的目标函数和搜索区间
x;&&&&&&&&&&&&&&&
%定义x为自变量
y = (x-1)^2 + 1;&%要求的目标函数
a = 0.1; b =
%a,b为搜索区间
epsilon = 1e-3;&&
%epsilon为收敛精度
% 2.调用黄金分割算法函数求解
[best_x, best_y] = golddiv(y, x, a, b, epsilon)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
二、黄金分割算法的函数文件
function [best_x, best_y] = golddiv(y, x, a, b, epsilon)
% 本函数实现黄金分割算法
% y是目标函数, x是自变量, a,b为区间范围, epsilon为精度
% best_x为黄金分割算法找到的最优点
% best_y为最优点处的函数值
if nargin ==
%如果输入参数没有精度要求
epsilon=0.001;& %设置默认的epsilon
x1 = a + 0.382 * (b - a);&&
%根据黄金分割比例确定搜索点
f1 = subs(y, x,
x1);&&&&&&&
%函数y在x1处的值
x2 = a + 0.618 * (b - a);&&
%根据黄金分割比例确定搜索点
f2 = subs(y, x,
x2);&&&&&&&
%函数y在x2处的值
while(abs(b - a) & epsilon)
&&& if f1 &
%如果f1小于f2
%b为新的右边界
x2 = x1;&&&
x1 = a + 0.382 * (b - a);&&
f1 = subs(y, x, x1);
x2 = a + 0.618 * (b - a);
f2 = subs(y, x, x2);
best_x = (a + b) / 2;&%最优的x值取a和b的平均值
best_y = subs(y, x, best_x);&&
%最优的函数值
如果你有所收获,欢迎用微信扫一扫进行打赏,赏金随意。
已投稿到:
以上网友发言只代表其个人观点,不代表新浪网的观点或立场。}

我要回帖

更多关于 matlab图像阈值分割 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信