Matlab函數1

1.tic和toc是一對計時功能的函數 \

tic 
toc 
>> 時間已過 3.393530 秒。

2.subplot函數

figure;
subplot(1,2,1);
subplot(1,2,2);
%以子框的形式在同一figure上顯示

3.imfinfo函數——查看圖像文件信息,注意參數是文件路徑和文件名,不是圖像對應的矩陣。

imfinfo('pout.tif')
ans = 

                     Filename: 'D:\Program Files\MATLAB\R2016a\toolbox\images\imdata\pout.tif'
                  FileModDate: '13-4月-2015 01:23:12'
                     FileSize: 69296
                       Format: 'tif'
                FormatVersion: []
                        Width: 240
                       Height: 291
                     BitDepth: 8
                    ColorType: 'grayscale'
              FormatSignature: [73 73 42 0]
                    ByteOrder: 'little-endian'
               NewSubFileType: 0
                BitsPerSample: 8
                  Compression: 'PackBits'
    PhotometricInterpretation: 'BlackIsZero'
                 StripOffsets: [8 7984 15936 23976 32089 40234 48335 56370 64301]
              SamplesPerPixel: 1
                 RowsPerStrip: 34
              StripByteCounts: [7976 7952 8040 8113 8145 8101 8035 7931 4452]
                  XResolution: 72
                  YResolution: 72
               ResolutionUnit: 'Inch'
                     Colormap: []
          PlanarConfiguration: 'Chunky'
                    TileWidth: []
                   TileLength: []
                  TileOffsets: []
               TileByteCounts: []
                  Orientation: 1
                    FillOrder: 1
             GrayResponseUnit: 0.0100
               MaxSampleValue: 255
               MinSampleValue: 0
                 Thresholding: 1
                       Offset: 69004
             ImageDescription: 'Copyright The MathWorks, Inc.'

4.載入Matlab自帶的核磁共振圖像

load mri %載入Matlab自帶的核磁共振圖像
imshow(D(:,:,8), map); % 顯示多幅中的一幅

% 同一窗口顯示
figure;
montage(D, map);

% 轉化成爲電影
figure;
mov=immovie(D, map);
colormap(map); %設定顏色表
movie(mov); %播放電影

5.處理圖像數據需要轉換爲double

I = im2double(I);			% 轉換數據類型爲double

6.灰度直方圖y=ax+b

clear;
close all;

%%圖片的灰度化處理(y=ax+b),a控制對比度,b控制線性平移增加亮度;
%%[H,x] = imhist(I, 64);  計算64個小區間的灰度直方圖
%%stem(x, (H/M/N), '.'); %顯示原圖像的直方圖,stem函數用於繪製火柴梗圖
%%H/M/N               %歸一化計算
I = imread('coins.png');		% 讀入原圖像

I = im2double(I);			% 轉換數據類型爲double
[M,N] = size(I);			% 計算圖像面積

figure(1);				% 打開新窗口
imshow(I);				% 顯示原圖像
title('原圖像');

figure(2);				% 打開新窗口
[H,x] = imhist(I, 64);		% 計算64個小區間的灰度直方圖
stem(x, (H/M/N), '.');		% 顯示原圖像的直方圖在matlab中,stem函數用於繪製火柴梗圖
title('原圖像');

% 增加對比度
Fa = 2; Fb = -55;
O = Fa .* I + Fb/255;

figure(3);
subplot(2,2,1);
imshow(O);
title('Fa = 2 Fb = -55 增加對比度');

figure(4);
subplot(2,2,1);
[H,x] = imhist(O, 64);
stem(x, (H/M/N), '.');
title('Fa = 2 Fb = -55 增加對比度');

% 減小對比度
Fa = 0.5; Fb = -55;
O = Fa .* I + Fb/255;

figure(3);
subplot(2,2,2);
imshow(O);
title('Fa = 0.5 Fb = -55 減小對比度');

figure(4);
subplot(2,2,2);
[H,x] = imhist(O, 64);
stem(x, (H/M/N), '.');
title('Fa = 0.5 Fb = -55 減小對比度');

% 線性增加亮度
Fa = 1; Fb = 55;
O = Fa .* I + Fb/255;

figure(3);
subplot(2,2,3);
imshow(O);
title('Fa = 1 Fb = 55 線性平移增加亮度');

figure(4);
subplot(2,2,3);
[H,x] = imhist(O, 64);
stem(x, (H/M/N), '.');
title('Fa = 1 Fb = 55 線性平移增加亮度');

% 反相顯示
Fa = -1; Fb = 255;
O = Fa .* I + Fb/255;

figure(3);
subplot(2,2,4);
imshow(O);
title('Fa = -1 Fb = 255 反相顯示');

figure(4);
subplot(2,2,4);
[H,x] = imhist(O, 64);
stem(x, (H/M/N), '.');
title('Fa = -1 Fb = 255 反相顯示');

7.伽馬變換
y=(x+esp)yy=(x+esp)^y

y>1時,圖像的高灰度區域對比度增強
y<1時,圖像的低灰度區域對比度增強
y=0時,圖像線性

%% imadjust(I, [ ], [ ], 0.75)
I = imread('pout.tif'); %讀入原圖像

% Gamma取0.75
subplot(1,3,1);
imshow(imadjust(I, [ ], [ ], 0.75));
title('Gamma 0.75');

% Gamma取1
subplot(1,3,2);
imshow(imadjust(I, [ ], [ ], 1));
title('Gamma 1');

% Gamma取1.5
subplot(1,3,3);
imshow(imadjust(I, [ ], [ ], 1.5));
title('Gamma 1.5');

8.灰度閾值變換

level = graythresh(I); %從灰度圖片獲得“最優閾值”
BW = im2bw(I,level);   %轉換

9.直方圖均衡化


I = imread('pout.tif'); %讀入原圖像
I = im2double(I);

% 對於對比度變大的圖像
I1 = 2 * I - 55/255;
subplot(4,4,1);
imshow(I1);
subplot(4,4,2);
imhist(I1);
subplot(4,4,3);
imshow(histeq(I1));
subplot(4,4,4);
imhist(histeq(I1));

% 對於對比度變小的圖像
I2 = 0.5 * I + 55/255;
subplot(4,4,5);
imshow(I2);
subplot(4,4,6);
imhist(I2);
subplot(4,4,7);
imshow(histeq(I2));
subplot(4,4,8);
imhist(histeq(I2));

% 對於線性增加亮度的圖像
I3 = I + 55/255;
subplot(4,4,9);
imshow(I3);
subplot(4,4,10);
imhist(I3);
subplot(4,4,11);
imshow(histeq(I3));
subplot(4,4,12);
imhist(histeq(I3));

% 對於線性減小亮度的圖像
I4 = I - 55/255;
subplot(4,4,13);
imshow(I4);
subplot(4,4,14);
imhist(I4);
subplot(4,4,15);
imshow(histeq(I4));
subplot(4,4,16);
imhist(histeq(I4));

10.分段線性變換

function out = imgrayscaling(varargin)
% IMGRAYSCALING     執行灰度拉伸功能
%   語法:
%       out = imgrayscaling(I, [x1,x2], [y1,y2]);
%       out = imgrayscaling(X, map, [x1,x2], [y1,y2]);
%       out = imgrayscaling(RGB, [x1,x2], [y1,y2]);
%   這個函數提供灰度拉伸功能,輸入圖像應當是灰度圖像,但如果提供的不是灰度
%   圖像的話,函數會自動將圖像轉化爲灰度形式。x1,x2,y1,y2應當使用雙精度
%   類型存儲,圖像矩陣可以使用任何MATLAB支持的類型存儲。

[A, map, x1 , x2, y1, y2] = parse_inputs(varargin{:});

% 計算輸入圖像A中數據類型對應的取值範圍
range = getrangefromclass(A);
range = range(2);

% 如果輸入圖像不是灰度圖,則需要執行轉換
if ndims(A)==3,% A矩陣爲3維,RGB圖像
  A = rgb2gray(A);
elseif ~isempty(map),% MAP變量爲非空,索引圖像
  A = ind2gray(A,map);
end % 對灰度圖像則不需要轉換
 
% 讀取原始圖像的大小並初始化輸出圖像
[M,N] = size(A);
I = im2double(A);		% 將輸入圖像轉換爲雙精度類型
out = zeros(M,N);
 
% 主體部分,雙級嵌套循環和選擇結構
for i=1:M
    for j=1:N
        if I(i,j)<x1
            out(i,j) = y1 * I(i,j) / x1;
        elseif I(i,j)>x2
            out(i,j) = (I(i,j)-x2)*(range-y2)/(range-x2) + y2;
        else
            out(i,j) = (I(i,j)-x1)*(y2-y1)/(x2-x1) + y1;
        end
    end
end

% 將輸出圖像的格式轉化爲與輸入圖像相同
if isa(A, 'uint8') % uint8
    out = im2uint8(out);
elseif isa(A, 'uint16')
    out = im2uint16(out);
% 其它情況,輸出雙精度類型的圖像
end

 % 輸出:
if nargout==0 % 如果沒有提供參數接受返回值
  imshow(out);
  return;
end
%-----------------------------------------------------------------------------
function [A, map, x1, x2, y1, y2] = parse_inputs(varargin);
% 這就是用來分析輸入參數個數和有效性的函數parse_inputs
% A       輸入圖像,RGB圖 (3D), 灰度圖 (2D), 或者索引圖 (X)
% map     索引圖調色板 (:,3)
% [x1,x2] 參數組 1,曲線中兩個轉折點的橫座標
% [y1,y2] 參數組 2,曲線中兩個轉折點的縱座標
% 首先建立一個空的map變量,以免後面調用isempty(map)時出錯
map = [];
 
%   IPTCHECKNARGIN(LOW,HIGH,NUM_INPUTS,FUNC_NAME) 檢查輸入參數的個數是否
%   符合要求,即NUM_INPUTS中包含的輸入變量個數是否在LOW和HIGH所指定的範圍
%   內。如果不在範圍內,則此函數給出一個格式化的錯誤信息。
iptchecknargin(3,4,nargin,mfilename);
 
%   IPTCHECKINPUT(A,CLASSES,ATTRIBUTES,FUNC_NAME,VAR_NAME, ARG_POS) 檢查給定
%   矩陣A中的元素是否屬於給定的類型列表。如果存在元素不屬於給定的類型,則給出
%   一個格式化的錯誤信息。
iptcheckinput(varargin{1},...
              {'uint8','uint16','int16','double'}, ...
              {'real', 'nonsparse'},mfilename,'I, X or RGB',1);
 
switch nargin
 case 3 %            可能是imgrayscaling(I, [x1,x2], [y1,y2]) 或 imgrayscaling(RGB, [x1,x2], [y1,y2])
  A = varargin{1};
  x1 = varargin{2}(1);
  x2 = varargin{2}(2);
  y1 = varargin{3}(1);
  y2 = varargin{3}(2);
 case 4
  A = varargin{1};%               imgrayscaling(X, map, [x1,x2], [y1,y2])
  map = varargin{2};
  x1 = varargin{2}(1);
  x2 = varargin{2}(2);
  y1 = varargin{3}(1);
  y2 = varargin{3}(2);
end

% 檢測輸入參數的有效性
% 檢查RGB數組
if (ndims(A)==3) && (size(A,3)~=3)   
    msg = sprintf('%s: 真彩色圖像應當使用一個M-N-3維度的數組', ...
                  upper(mfilename));
    eid = sprintf('Images:%s:trueColorRgbImageMustBeMbyNby3',mfilename);
    error(eid,'%s',msg);
end
 
if ~isempty(map) 
% 檢查調色板
  if (size(map,2) ~= 3) || ndims(map)>2
    msg1 = sprintf('%s: 輸入的調色板應當是一個矩陣', ...
                   upper(mfilename));
    msg2 = '並擁有三列';
    eid = sprintf('Images:%s:inColormapMustBe2Dwith3Cols',mfilename);
    error(eid,'%s %s',msg1,msg2);
    
  elseif (min(map(:))<0) || (max(map(:))>1)
    msg1 = sprintf('%s: 調色板中各個分量的強度 ',upper(mfilename));
    msg2 = '應當在0和1之間';
    eid = sprintf('Images:%s:colormapValsMustBe0to1',mfilename);
    error(eid,'%s %s',msg1,msg2);
  end
end
 
% 將int16類型的矩陣轉換成uint16類型
if isa(A,'int16')
  A = int16touint16(A);
end

11.直方圖圓規定化

I = imread('pout.tif'); %讀入原圖像
I1 = imread('coins.png'); %讀入要匹配直方圖的圖像
I2 = imread('circuit.tif'); %讀入要匹配直方圖的圖像

% 計算直方圖
[hgram1, x] = imhist(I1);
[hgram2, x] = imhist(I2);

% 執行直方圖均衡化
J1=histeq(I,hgram1);
J2=histeq(I,hgram2);

% 繪圖
subplot(2,3,1);
imshow(I);title('原圖');
subplot(2,3,2);
imshow(I1); title('標準圖1');
subplot(2,3,3);
imshow(I2); title('標準圖2');
subplot(2,3,5);
imshow(J1); title('規定化到1')
subplot(2,3,6);
imshow(J2);title('規定化到2');

% 繪直方圖
figure;

subplot(2,3,1);
imhist(I);title('原圖');

subplot(2,3,2);
imhist(I1); title('標準圖1');

subplot(2,3,3);
imhist(I2); title('標準圖2');

subplot(2,3,5);
imhist(J1); title('規定化到1')

subplot(2,3,6);
imhist(J2);title('規定化到2');

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章