22
24情報処理技術講義 よる画像処理 MATLABよる画像処理1 17

MATLABによる画像処理1yamasho/old/IP1.pdfMATLABによる画像処理 画像処理とは 画像から特徴となる情報を取り出すために行われる処理 画像にはたくさんの情報(次元)がある

Embed Size (px)

Citation preview

第24回 情報処理技術講義

よる画像処理MATLABによる画像処理1

17

MATLABによる画像処理

画像処理とは

画像から特徴となる情報を取り出すために行われる処理

画像にはたくさんの情報(次元)がある画像にはたくさんの情報(次元)がある

道の方向季節

車の色種影による太陽方向陽方向

寸法車の数

寸法

18画像処理は次元を減らすことで特徴量を抽出する

MATLABによる画像処理

画像処理の準備

http://www.ess.ic.kanagawa-it.ac.jp/app_images_j.html①標準画像のdownload

(L さんは特に有名 htt // d ill f f /l /)(color.zipのみ)

(Lenaさんは特に有名 http://ndevilla.free.fr/lena/)

Z:ドライブに¥matlabを作成して、そこに保存する圧縮ファイルになっているので、¥matlabで解凍する

②画像の読み込み

・Matlabを起動するMatlabを起動する・current Directry をz:¥matlabに変更する・以下のコマンドを打ち込む

I i d(‘ ¥ tl b¥P t b ’)Img=imread(‘z:¥matlab¥Parrots.bmp’);

・workspaceで確認 何画素の画像ですか?どのように格納されていますか?

19

MATLABによる画像処理

画像処理の準備

③画像の表示figure(1);imagesc(Img);imagesc(Img);figure(2),subplot(1,3,1), imagesc(Img(:, :, 1)); どのチャンネルがR, G, B

なのか わかりますか?subplot(1,3,2), imagesc(Img(:, :, 2));subplot(1,3,3), imagesc(Img(:, :, 3));colormap(gray);

なのか、わかりますか?

④画像の書き込み

I G i t8((0 299*I ( 1)+0 687*I ( 2)+0 114*I ( 3))/3)Imwrite(ImgG, ‘z:¥matlab¥ParrotsG.bmp’, ‘BMP’);ImgG=uint8((0.299*Img(:,:,1)+0.687*Img(:,:,2)+0.114*Img(:,:,3))/3);

NTSC規格でのRGB比率です

20

NTSC規格でのRGB比率です。グレイ画像ファイルが生成されましたか?

MATLABによる画像処理

画像処理の流れと分類

画像入力-画像変換-情報計測

画像処理は次元を減らすことで特徴量を抽出する

(例)

知覚色変換(HSV) 黄色領域のみを抽出入力画像

x画素階調

y画素階調

21長方形と思われるものも 視点を変えると… 特徴を抽出して調査

MATLABによる画像処理

画像変換

階調変換 光強度に応じた階調を変換して特徴を顕著にする手法反転

RImgG=255-ImgG;figure(2),subplot(1,2,1), imagesc(ImgG);subplot(1,2,2), imagesc(RImgG);

g g ;

階調減少

p ( , , ), g ( g );colormap(‘gray’);

figure(2),

ImgG16=fix(ImgG./16).*16+16;ImgG8=fix(ImgG./32).*32+32;

g ( ),subplot(1,3,1), imagesc(ImgG);subplot(1,3,2), imagesc(ImgG16);subplot(1,3,3), imagesc(ImgG8);

22

colormap(‘gray’);

MATLABによる画像処理

画像変換ガンマ(GOGモデル)

ImgGA=uint8(ImgG*5);

= Gain Offset Gamma modelGain:a

g ( g );Offset:b

Gamma:γ

ImgGB=uint8(ImgG-15);Gamma:γ

aveImgG=mean(mean(ImgG));ImgGR=uint8(((double(ImgG)/aveImgG).^3)*aveImgG);

figure(2),subplot(2,2,1), imagesc(ImgG);subplot(2 2 2) imagesc(ImgGA);subplot(2,2,2), imagesc(ImgGA);subplot(2,2,3), imagesc(ImgGB);subplot(2,2,4), imagesc(ImgGR);colormap(‘gray’);

23

p( g y );

MATLABによる画像処理

画像変換ヒストグラム(累積密度関数)

[xsize ysize]=size(ImgG);hg=zeros(256,1);for i=1:xsizefor i 1:xsize

for j=1:ysizehg(ImgG(i, j)+1)=hg(ImgG(i, j)+1)+1;

endendfigure(2),subplot(1,2,1), imagesc(ImgG);p ( ) g ( g )subplot(1,2,2), bar(hg);colormap(gray);

24

MATLABによる画像処理

画像変換ヒストグラム均一化

累積密度を階調と置累積密度を階調と置

き換えて(逆変換)、階調の均一化を図る

25

MATLABによる画像処理

画像変換 ヒストグラム均一化

[xsize,ysize]=size(ImgG); len=xsize*ysize;ImgG2=reshape(ImgG,len,1);histogram = hist(ImgG2,[0:256-1]); cumhist(1)= histogram(1);

finalimage=reshape(newimage, xsize, ysize);finalhistogram=hist(newimage,[0:256-1]);

figure(1),cumhist(1) histogram(1);

for i=2:256cumhist(i)= cumhist(i-1)+ histogram(i);

end

figure(1),subplot(3,1,1),stem([0:256-1],histogram)subplot(3,1,2),stem([0:256-1],trans)subplot(3,1,3),stem([0:256-1],finalhistogram)figure(2),

for i=1:256trans(i)=round(((256-1)/(len))*cumhist(i));

end

g ( ),subplot(1,2,1),image(ImgG),colormap('gray')subplot(1,2,2),image(finalimage),colormap('gray')

newimage=zeros(len,1);for i=2:256

transx(i-1)=trans(i);endfor i=1:len

if ImgG2(i)~=0newimage(i)=transx(ImgG2(i));

else

26

newimage(i)=trans(1);end

end

MATLABによる画像処理

画像変換ヒストグラム(任意分割)

累積密度関数の分解能を変えて様々な分析をする

[xsize ysize]=size(ImgG);for i=1:xsize

for j=1:ysizej yhg((i-1)*i+j)=double(ImgG(i, j));

endendfigure(2),subplot(1,2,1), imagesc(ImgG);subplot(1,2,2), hist(hg, 16);

( )←16分割にして評価

colormap(gray);

27

MATLABによる画像処理

画像変換2値化

[xsize ysize]=size(ImgG);avg= mean(mean(ImgG(i, j)));g ( ( g ( , j)));for i=1:xsize

for j=1:ysize if (ImgG(i, j) > avg)

ImgBw(i, j)=0;else

ImgBw(i, j)=255;end

endendfi (2)figure(2),subplot(1,2,1), imagesc(ImgG);subplot(1,2,2), imagesc(ImgBw);colormap(‘gray’);

28

colormap( gray );

MATLABによる画像処理

画像変換

2値化におけるしきい値の決め方が重要(論理的な理由が必要)

統計的手法(ヒストグラム)からの決定

p-タイル法モード法大津の手法微分ヒストグラム法判別分析

状況判断からの決定

輝度分別領域分別外乱分別

29

MATLABによる画像処理

画像変換 傾き

幾何変換 スケール(倍率)

回転

スケール変換 正射影画像として倍率が変化する

【拡大】

【縮小】

clear all

【縮小】

30

clear allImg=imread(‘z:¥MATLAB¥Parrots.bmp');ImgG=uint8((0.299*Img(:,:,1)+0.687*Img(:,:,2)+0.114*Img(:,:,3))/3);

MATLABによる画像処理

幾何変換 スケール変換

Mag=0.25;Mag 0.25;[xsize,ysize]=size(ImgG); X=zeros(2*xsize,2*ysize); for x=1:2*xsize

for y=1:2*ysize oldx=round((x-xsize)/Mag);oldx round((x xsize)/Mag);oldy=round((y-ysize)/Mag); oldx=oldx+round(xsize/2);oldy=oldy+round(ysize/2); if oldx>0 & oldx<=xsize & oldy>0 & oldy<=ysize;y y y ;

X(x,y)=ImgG(oldx,oldy);end

endendScaledImgG=X(xsize-round(xsize*Mag/2)+1: xsize+round(xsize*Mag/2),...

ysize-round(ysize*Mag/2)+1: ysize+round(ysize*Mag/2));figure(2),subplot(1,2,1), imagesc(ImgG);axis([0 xsize 0 ysize]);p ( , , ), g ( g ); ([ y ]);subplot(1,2,2), imagesc(uint8(ScaledImgG));axis([0 xsize 0 ysize]);colormap('gray');

figure(3), 補間が重要技術

31

g ( ),subplot(1,2,1), imagesc(ImgG);axis([0 xsize 0 ysize]);subplot(1,2,2), imagesc(uint8(ScaledImgG));axis([0 xsize*Mag 0 ysize*Mag]);colormap('gray');

MATLABによる画像処理

画像変換補間法 'linear' 線形補間(デフォルト)

'nearest' 最近傍点による補間'spline' キュービックスプライン補間' bi 'キ ビ ク補間'cubic' キュービック補間

[x,y] = meshgrid(-3:1:3);z = peaks(x y);z = peaks(x,y);figure(1), surf(x,y,z)[xi,yi] = meshgrid(-3:0.25:3);zi1 = interp2(x y z xi yi 'linear');zi1 = interp2(x,y,z,xi,yi, linear );zi2 = interp2(x,y,z,xi,yi,'nearest');zi3 = interp2(x,y,z,xi,yi,'spline');zi4 = interp2(x y z xi yi 'cubic');zi4 interp2(x,y,z,xi,yi, cubic );figure(2), subplot(2,2,1), surf(xi,yi,zi1)subplot(2,2,2), surf(xi,yi,zi2)subplot(2,2,3), surf(xi,yi,zi3)

32

subplot(2,2,3), surf(xi,yi,zi3)subplot(2,2,4), surf(xi,yi,zi4)

MATLABによる画像処理

画像変換補間法 'linear' 線形補間(デフォルト)

'nearest' 最近傍点による補間'spline' キュービックスプライン補間' bi 'キ ビ ク補間'cubic' キュービック補間

[xsize ysize]=size(ScaledImgG);[x,y] = meshgrid(1:1:xsize);[x,y] meshgrid(1:1:xsize);z = ScaledImgG;figure(1), imagesc(z); colormap('gray');[xi,yi] = meshgrid(1:0.25:xsize);i1 i t 2( i i 'li ')zi1 = interp2(x,y,z,xi,yi,'linear');

zi2 = interp2(x,y,z,xi,yi,'nearest');zi3 = interp2(x,y,z,xi,yi,'spline');zi4 = interp2(x,y,z,xi,yi,'cubic');zi4 interp2(x,y,z,xi,yi, cubic );figure(2), subplot(2,2,1),imagesc(zi1);subplot(2,2,2),imagesc(zi2);subplot(2,2,3),imagesc(zi3);

b l t(2 2 4) i ( i4)

33

subplot(2,2,4),imagesc(zi4);colormap('gray');

MATLABによる画像処理

画像変換

幾何変換

アフィン変換 画像の平行移動や回転をつかさどる変換手法

一般形

行列表現

同時座標系

アフィン変換は射影変換の部分集合である

(本来, 図形の変形は射影変換である)

34

MATLABによる画像処理

画像変換

アフィン変換

angle=pi/3; xmove=0; ymove=0;[xsize, ysize]=size(ImgG);matrix=[cos(angle) sin(angle) xmove; -sin(angle) cos(angle) ymove; 0 0 1];AffineImgG = zeros(xsize);for i=1:xsize

for j=1:ysizefor j 1:ysizeA=[i-round(xsize/2) j-round(ysize/2) 1];origx=matrix*A';origx=round(origx+[xsize/2; ysize/2; 1]);if origx(1)>0 & origx(1)<=xsize & origx(2)>0 & origx(2)<=ysize;

AffineImgG(i, j)=ImgG(origx(1), origx(2));end

endendendfigure(2),subplot(1,2,1), imagesc(ImgG);

b l (1 2 2) i (Affi I G)

35

subplot(1,2,2), imagesc(AffineImgG);colormap('gray'); 注:補間処理はしていない

MATLABによる画像処理

画像変換

幾何変換

カメラ変換 Tsaiのpin-holeモデル

f 焦点距離f -焦点距離k -歪係数(円周方向)Cx, Cy –歪の中心Sx, Sy –画像アスペクト比のスケールファクタy

歪のない理想像点(Xu,Yu)と歪んだ像点(Xd, Yd)の関係は、

( )2 ( )2( )21 krXX ud += ( )21 krYY ud +=

となる。ここで、 である。実際には歪中心(画像中心)とスケールファクタが考慮されるため 画素比率(d d )を導入して 最終画像は以下の式のように求められる

22dd YXr +=

されるため、画素比率(dx, dy)を導入して、最終画像は以下の式のように求められる。

xx

dxf C

dXSX += y

y

dyf C

dYS

Y +=

36

x y

http://homepages.inf.ed.ac.uk/rbf/CVonline/LOCAL_COPIES/DIAS1/

MATLABによる画像処理

画像変換 カメラ変換

ImgG=repmat([ones(32) zeros(32); zeros(32) ones(32)],4,4);cx=0.5; cy=0.5; k=-0.2;[xsize,ysize]=size(ImgG); distImgG=zeros(xsize*2,ysize*2); rmax=sqrt((xsize/2)^2+(ysize/2)^2);rmax=sqrt((xsize/2) 2+(ysize/2) 2);for x=1:2*xsize

for y=1:2*ysizerdd=((x-cx*ysize)^2+(y-cy*xsize)^2);rd2_norm=rdd/rmax^2;kr2=k*rd2_norm;oldx=round(x+(x-cx*ysize)*kr2);oldy=round(y+(y-cy*xsize)*kr2);oldy round(y+(y cy xsize) kr2);if oldx>0 & oldx<=xsize & oldy>0 & oldy<=ysize;

distImgG(x,y)=ImgG(oldx,oldy);end

注:補間処理はしていない

endendfigure(2),subplot(1 2 1) imagesc(ImgG);axis([0 xsize 0 ysize]);

37

subplot(1,2,1), imagesc(ImgG);axis([0 xsize 0 ysize]);subplot(1,2,2), imagesc(uint8(distImgG));axis([0 xsize 0 ysize]);colormap('gray');

MATLABによる画像処理

画像変換

このカメラキャリブレーションは非常に重要な技術である

だって、画像処理が人間より優位性を発揮できる点だから…

を扱う 資料を 度 読むhttp://research.microsoft.com/en-us/um/people/zhang/calib/

カメラを扱う人は下記資料を一度は読むこと!

http://chihara.naist.jp/people/2005/yasuhi-y/pukiwiki/index php?%B8%A6%B5%E6%2FGML%20C%2B%2B%20Camera%20

実用的にはツールが存在する

38

y/pukiwiki/index.php?%B8%A6%B5%E6%2FGML%20C%2B%2B%20Camera%20Calibration%20Toolbox