在看imadjust代码时,看到stretchlim函数,特此分析一下,代码注释如下

function lowhigh = stretchlim(varargin)
%STRETCHLIM Find limits to contrast stretch an image.
% LOW_HIGH = STRETCHLIM(I,TOL) returns a pair of gray values that can be
% used by IMADJUST to increase the contrast of an image.
%
% TOL = [LOW_FRACT HIGH_FRACT] specifies the fraction of the image to
% saturate at low and high pixel values.
%
% If TOL is a scalar, TOL = LOW_FRACT, and HIGH_FRACT = 1 - LOW_FRACT,
% which saturates equal fractions at low and high pixel values.
%
% If you omit the argument, TOL defaults to [0.01 0.99], saturating 2%.
%
% If TOL = 0, LOW_HIGH = [min(I(:)); max(I(:))].
%
% LOW_HIGH = STRETCHLIM(RGB,TOL) returns a 2-by-3 matrix of pixel value
% pairs to saturate each plane of the RGB image. TOL specifies the same
% fractions of saturation for each plane.
%
% Class Support
% -------------
% The input image can be uint8, uint16, int16, double, or single, and must
% be real and nonsparse. The output limits are double and have values
% between 0 and 1.
%
% Note
% ----
% If TOL is too big, such that no pixels would be left after saturating
% low and high pixel values, then STRETCHLIM returns [0; 1].
%
% Example
% -------
% I = imread('pout.tif');
% J = imadjust(I,stretchlim(I),[]);
% figure, imshow(I), figure, imshow(J)
%
% See also BRIGHTEN, DECORRSTRETCH, HISTEQ, IMADJUST. % Copyright 1999-2005 The MathWorks, Inc.
% $Revision: 1.7.4.8 $ $Date: 2005/11/15 01:02:11 $ [img,tol] = ParseInputs(varargin{:}); if isa(img,'uint8')
nbins = 256;
else
nbins = 65536;
end tol_low = tol(1);
tol_high = tol(2); % 获取第三维的维数
p = size(img,3); if tol_low < tol_high
ilowhigh = zeros(2,p);
% 对于第三维的每一层进行寻找
for i = 1:p % Find limits, one plane at a time
N = imhist(img(:,:,i),nbins);
cdf = cumsum(N)/sum(N); %cumulative distribution function
ilow = find(cdf > tol_low, 1, 'first'); %找到满足累计频率>tol_low的下标
ihigh = find(cdf >= tol_high, 1, 'first'); %找到满足累计频率>=tol_low的下标
if ilow == ihigh % this could happen if img is flat
ilowhigh(:,i) = [1;nbins];
else
ilowhigh(:,i) = [ilow;ihigh];
end
end
lowhigh = (ilowhigh - 1)/(nbins-1); % convert to range [0 1] else
% tol_low >= tol_high, this tolerance does not make sense. For example, if
% the tolerance is .5 then no pixels would be left after saturating
% low and high pixel values. In all of these cases, STRETCHLIM
% returns [0; 1]. See gecks 278249 and 235648.
lowhigh = repmat([0;1],1,p);
end %-----------------------------------------------------------------------------
function [img,tol] = ParseInputs(varargin) iptchecknargin(1, 2, nargin, mfilename); img = varargin{1};
iptcheckinput(img, {'uint8', 'uint16', 'double', 'int16', 'single'}, {'real', ...
'nonsparse'}, mfilename, 'I or RGB', 1);
if (ndims(img) > 3)
msgId = 'Images:stretchlim:dimTooHigh';
error(msgId,'STRETCHLIM only supports individual images.');
end % 默认值
tol = [.01 .99]; %default
if nargin == 2
tol = varargin{2};
switch numel(tol) %判断tol数组元素个数
case 1
tol(2) = 1 - tol; case 2
if (tol(1) >= tol(2))
msgId = 'Images:stretchlim:invalidTolOrder';
error(msgId,'TOL(1) must be less than TOL(2).');
end
otherwise
msgId = 'Images:stretchlim:invalidTolSize';
error(msgId,'TOL must have 1 or 2 elements.');
end
end if ( any(tol < 0) || any(tol > 1) || any(isnan(tol)) )
msgId = 'Images:stretchlim:tolOutOfRange';
error(msgId,'TOL must be in the range [0 1].');
end

主函数为stretchlim,其中子函数ParseInputs用于获取参数。

主函数中关键部分为

if tol_low < tol_high
ilowhigh = zeros(2,p);
% 对于第三维的每一层进行寻找
for i = 1:p % Find limits, one plane at a time
N = imhist(img(:,:,i),nbins);
cdf = cumsum(N)/sum(N); %cumulative distribution function
ilow = find(cdf > tol_low, 1, 'first'); %找到满足累计频率>tol_low的下标
ihigh = find(cdf >= tol_high, 1, 'first'); %找到满足累计频率>=tol_low的下标
if ilow == ihigh % this could happen if img is flat
ilowhigh(:,i) = [1;nbins];
else
ilowhigh(:,i) = [ilow;ihigh];
end
end
lowhigh = (ilowhigh - 1)/(nbins-1); % convert to range [0 1]

函数imhist是用于获取图像数据直方图,返回值是个一维矩阵,保存着图像中每个灰度等级的像素个数,如上,如果nbins等于256,代表灰度范围为0~255,则imhist获取图像img中符合每个灰度等级的像素个数,例如灰度等级3对应数组N下标4,图像中灰度等级为3的像素个数为5,则N(4)=5。

cdf = cumsum(N)/sum(N);

这一行中cumsum(N)计算一个数组各个元素的累加值,例如数组 N = [1 3 5],则cumsum(N) = [1 4 9],sum(N)获取数组N的元素个数,因此这个式子代表意思是每个灰度等级的累积频率,例子中sum(N)=3,则cdf=[1/3 4/3 9/3]。

ilow = find(cdf > tol_low, 1, 'first');        %找到满足累计频率>tol_low的下标
ihigh = find(cdf >= tol_high, 1, 'first'); %找到满足累计频率>=tol_low的下标

找到下标ilow和ihigh,可对应满足条件的灰度等级,因为cdf数组中下标也就是对应灰度等级的值加1。

lowhigh = (ilowhigh - 1)/(nbins-1);  % convert to range [0 1]

归一化,将范围[ilow ihigh]映射到[0 1]

stretchlim函数分析的更多相关文章

  1. split(),preg_split()与explode()函数分析与介

    split(),preg_split()与explode()函数分析与介 发布时间:2013-06-01 18:32:45   来源:尔玉毕业设计   评论:0 点击:965 split()函数可以实 ...

  2. string函数分析

    string函数分析string函数包含在string.c文件中,经常被C文件使用.1. strcpy函数原型: char* strcpy(char* str1,char* str2);函数功能: 把 ...

  3. start_amboot()函数分析

    一.整体流程 start_amboot()函数是执行完start.S汇编文件后第一个C语言函数,完成的功能自然还是初始化的工作 . 1.全局变量指针r8设定,以及全局变量区清零 2.执行一些类初始化函 ...

  4. uboot的jumptable_init函数分析

    一.函数说明 函数功能:安装系统函数指针 函数位置:common/exports.c 二.函数分析 void jumptable_init (void) { int i; gd->jt = (v ...

  5. Linux-0.11内核源代码分析系列:内存管理get_free_page()函数分析

    Linux-0.11内存管理模块是源码中比較难以理解的部分,如今把笔者个人的理解发表 先发Linux-0.11内核内存管理get_free_page()函数分析 有时间再写其它函数或者文件的:) /* ...

  6. 31.QPainter-rotate()函数分析-文字旋转不倾斜,图片旋转实现等待

    在上章和上上上章: 28.QT-QPainter介绍 30.QT-渐变之QLinearGradient. QConicalGradient.QRadialGradient 学习了QPainter基础绘 ...

  7. 如何验证一个地址可否使用—— MmIsAddressValid函数分析

    又是一篇内核函数分析的博文,我个人觉得Windows的内核是最好的老师,当你想实现一个功能之前可以看看Windows内核是怎么做的,说不定就有灵感呢:) 首先看下官方的注释说明: /*++ Routi ...

  8. STM32F10X固件库函数——串口清状态位函数分析

    STM32F10X固件库函数——串口清状态位函数分析 最近在测试串口热插拔功能的时候,意外发现STM32F10X的串口库函数中,清理串口状态位函数稍稍有点不解.下面是改函数的源码: /******** ...

  9. 常用string函数分析

    string函数分析string函数包含在string.c文件中,经常被C文件使用.1. strcpy函数原型: char* strcpy(char* str1,char* str2);函数功能: 把 ...

随机推荐

  1. Java 执行终端命令实现,调用执行另外一个Java文件

    Test.java package com.journaldev.files; public class Test { public static void main(String[] args) { ...

  2. canvas元素大小与绘图表面大小

    原文链接:canvas总结:元素大小与绘图表面大小 前言 我们使用canvas的时候一般在canvas元素中直接设置它的width和height: 1 <canvas id="myCa ...

  3. 可以ping通,浏览器打不开网页 - 解决办法

    网络故障表现为: 1.电脑显示网络连接正常,DNS配置和hosts配置均正常 2.cmd可以ping通网址,域名 3.所有浏览器无法打开网页,有道云笔记置灰,微信二维码刷新失败 解决办法: 管理员权限 ...

  4. BZOJ 1017 魔兽地图DotR(树形DP)

    题意:有两类装备,高级装备A和基础装备B.现在有m的钱.每种B有一个单价和可以购买的数量上限.每个Ai可以由Ci种其他物品合成,给出Ci种其他物品每种需要的数量.每个装备有一个贡献值.求最大的贡献值. ...

  5. LeetCode_Combinations

    Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For exampl ...

  6. KEIL 程序定位

    用Keil做51的开发也4年多了,代码量基本上维持在5~10K左右,说大不大,说小也不小,也就是个中等货色.这段期间工作上难得有稍许的空间,潜心研究了一下keil中如何在 CODE中定位C程序的方法. ...

  7. qsort的另类玩法,无聊写着耍耍

    #include <stdio.h>#include <stdlib.h>char cmp_shellcode[] = "\x55" "\x89\ ...

  8. js深入研究之类定义与使用

    js可以定义自己的类 很有意思 <script type="text/javascript"> var Anim = function() { alert('nihao ...

  9. myeclipse中控制台日志比实际晚8小时解决方法及java日志处理

    今天终于忍不住要解决myeclipse控制台中日志显示比实际晚8小时的问题,开始以为myeclipse编辑器时间问题,后来想想不对,myeclipse控制台打印的是tomcat的日志,随后以为是log ...

  10. Linux服务器SNMP常用OID (转)

    原文地址:http://www.haiyun.me/archives/linux-snmp-oid.html 收集整理一些Linux下snmp常用的OID,用做服务器监控很不错. 服务器负载: 1 2 ...