"Fossies" - the Fresh Open Source Software Archive

Member "sip-0.12.1/macros/imhist.sci" (15 Oct 2011, 3578 Bytes) of package /linux/privat/sip-0.12.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Scilab source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file.

    1 function [counts, cells] = imhist(im, varargin)
    2 //
    3 // Compute and plot the histogram of an image
    4 // imhist(im)
    5 // imhist(im, 10)
    6 // imhist(im, 10, 0.8)
    7 // imhist(im, 10, '')
    8 // imhist(im, 10, 'green')
    9 // imhist(im, 10, 0.8, 'green')
   10 //
   11 // TODO
   12 //  - remove the necessity of a second parameter just to get to the third,
   13 //    ie, remove the need for imhist(im,10,'');
   14 //  - enable a better x-axis labeling, perhaps adding a colorbar
   15 //  - accept color images as input
   16 //  - try plot2d3, perhaps output bars with just pure color (e.g. black),
   17 //  without boundaries
   18 //  - perhaps make axis a different color (e.g. gray)
   19 //
   20   if(size(size(im),2)>2)
   21     error("The input matrix im should be a 2D matrix.");
   22   end
   23 
   24   bin=[];
   25   //if user specified 
   26   if length(varargin)>=1 then
   27     if varargin(1)~=floor(varargin(1)) then
   28       error("The second argument must be an integer.");
   29     elseif varargin(1)<=0 then
   30       error("The second argument must be a positive integer.");
   31     end
   32     bin = varargin(1);
   33   //elseif length(varargin)>2 then
   34   //  error("Too many arguments.");
   35   end
   36 
   37   imtype = typeof(im(1));
   38   
   39   select imtype
   40    case 'boolean' then
   41     if (isempty(bin) | bin>2) then, bin=2; end
   42     cells = linspace(0,1,bin);
   43    case 'uint8' then
   44     if isempty(bin) then, bin=2^8; end
   45     cells = linspace(0, 255, bin);
   46    case 'int8' then
   47     if isempty(bin) then, bin=2^8; end
   48     cells = linspace(-128, 127, bin);
   49    case 'uint16' then
   50     if isempty(bin) then, bin=2^16; end
   51     cells = linspace(0, 2^16-1, bin);
   52    case 'int16' then
   53     if isempty(bin) then, bin=2^16; end
   54     cells = linspace(-2^15, 2^15-1, bin);
   55    case 'int32' then
   56     if isempty(bin) then, 
   57       bin=2^16; 
   58       printf("The number of bins is set to 2^16 for int32 images."); 
   59     end
   60     cells = linspace(-2^31, 2^31-1, bin);
   61    case 'constant' then
   62     if isempty(bin) then, bin=10; end
   63     cells = linspace(0, 1, bin);
   64   else
   65     error("Data type " + imtype + " is not supported.");
   66   end
   67   
   68   if(bin > 1) then
   69     //such as for 256 bins of uint8 image
   70     //cells=[0,1,2,...,254,255]
   71     //new_cells=[-0.5, 0.5, 1.5, 2.5, ..., 254.5, 255.5]
   72     step = cells(2)-cells(1);
   73     new_cells = [cells, cells(length(cells))+step ] - step/2;
   74     [tmp, counts, info]=dsearch(double(im), new_cells);
   75   elseif(bin ==1) then
   76     counts=length(im);
   77   end
   78     
   79   cells = cells';
   80   counts = counts';
   81   
   82   if length(varargin)>1 then
   83     bar(cells, counts, varargin(2:length(varargin)));
   84   end
   85 endfunction
   86 
   87 // -------------------------------------------------------------------------
   88 // SIP - Scilab Image Processing toolbox
   89 // Copyright (C) 2002-2009  Ricardo Fabbri
   90 //
   91 // Modified starting October 15, 2011 by merging code back from:
   92 //
   93 // SIVP - Scilab Image and Video Processing toolbox
   94 // Copyright (C) 2006  Shiqi Yu
   95 //
   96 // This program is free software; you can redistribute it and/or modify
   97 // it under the terms of the GNU General Public License as published by
   98 // the Free Software Foundation; either version 2 of the License, or
   99 // (at your option) any later version.
  100 //
  101 // This program is distributed in the hope that it will be useful,
  102 // but WITHOUT ANY WARRANTY; without even the implied warranty of
  103 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  104 // GNU General Public License for more details.
  105 //
  106 // You should have received a copy of the GNU General Public License
  107 // along with this program; if not, write to the Free Software
  108 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  109 // -------------------------------------------------------------------------