Upload
nawarat-sornchai
View
164
Download
5
Embed Size (px)
Citation preview
คมอแนะน ำโปรแกรม MATLAB
จดท ำโดย
นางสาวลลตภทร สงวนผว
รหสนสต 58670381 กลม 3302
เสนอ
อาจารยกฤษณะ อมสวาสด
ภาคเรยนท 2/ปการศกษา 2558
มหาวทยาลยบรพา
Program Matlab
MATLAB เปนภาษาทมประสทธภาพสงใชส าหรบค านวณทางดานเทคนค ตวโปรแกรม ไดรวมเอาความสามารถในการค านวณ, การดและตดตามขอมลตางๆ รวมทงการเขยนโปรแกรม ไวในรปแบบทงายตอการใชงาน โดยทปญหาและวธการหาค าตอบ จะแสดงโดยใชสญลกษณทางคณตศาสตรทใชตามปกต การใชงานทวๆไปกมตามนครบ
ค านวณทางคณตศาสตร
พฒนาอลกอรธม (Algorithm)
สรางแบบจ าลอง (Modeling), จ าลองการท างาน(simulation), และ สรางตวตนแบบ (prototyping)
วเคราะหขอมล, ตรวจคนขอมล, และ แสดงผลขอมลในรปแบบตางๆ
แสดงภาพกราฟคตางๆ ทางวทยาศาสตรและวศวกรรมศาสตร
พฒนาโปรแกรมใชงานตางๆ อนนรวมถงการสราง Graphical User Interface (GUI) ดวย
>> ขาบนสด เปนแถบแสดงเครองมอ
>> Current folder ท าหนาท เรยก file งาน
>> Workspace บอกชนดของขอมลทเรากรอก
>> Command Window คอ หนาตางใหเราท างานหรอใส code
>> วธการใหโปรแกรมแสดงผล เชน เราอาจจะก าหนด A = ‘Hello GI’ จากนน กดปม
Enter ไดเลย
A = ‘Hello
GI’
>> ถาเราอยากทใหผลรบมาบวกกน ท าไดโดย ประกาศคาตวแปร a,b และ sum
เครองหมาย ; (semicolon) นนเปนค าสงทไมใหโปรแกรมแสดงผล
>> เมอเราตองการท าใหหนาจอของเรา วาง ท าไดโดยเราจะพมพค าสง clc ลงไป
กำรสรำงARAY
>> เราจะประกาศตวแปร โดยใส [ ] (basket)
แนวนอนเรยกวา “หลก”
แนวตงเรยกวา “แถว”
>> เครองหมาย ; จะแสดงผลในแนวตง
>> ถาเราตองการประกาศตวแปร ทมตวเลขเยอะๆ เราไมจ าเปนตองพมพตวทกตวลงไป
เชน เราจะประกาศตวแปรเทากบ 1-30 เรากท าไดโดย ใชเครองหมาย : (colon)
สรำงกรำฟโดยใชค ำสง plot
โดยกราฟจะม แกน X และ Y (โดยแกน xจะเปนแกนนอน และแกน Y จะเปนแกน ตง )
โดย เราจะก าหนด ตว เลขอะไรกได ในทน เราจะก าหนดทงแกน X และ Y ทง หมด 3 จด
ดวยกน
โดย X ม เลข 2 9 28
Y ม เลข 5 7 29
วธการใสชอกราฟโดยพมพค าวา Title (‘ชอกราฟทจะใส”)
Xlabel ชอของแกน x
Ylabel ชอของแกน y
>> วธการเปลยนสของเสนกราฟ โดยเราจะพมพcode plot(x,y, 'r')
blue สน าเงน แทนดวย b
Yellow สเหลอง แทนดวย y
black สด า แทนดวย k
white สขาว แทนดวย w
red สแดง แทนดวย r
green สเขยว แทนดวย g
>> วธการเปลยนลกษณะเสน Plot เชน ถาตองการเปลยนใหเปนเสนปะ โดยพมพ code
plot(x,y, '--r') โดย - - ทเราพมพลงไปนนหมายถงลกษณะเปนเสน ปะ
>> การแสดงแสดงกราฟหลายๆ เสนโดยก าหนดตวแปรของกราฟโดยอาจจะก าหนด x,y,z
และt
>> legend บอกถงชอของกราฟแตละเสน
Subplot (การแสดงกราฟยอยๆหรอการแยกกราฟ)
>> เราพมพ code
Subplot (2,2,1) ในวงลบเราสามารถก าหนดตวเลขได
Plot (x,t, ‘—r’)
กำรสรำงกรำฟแบบ 3 มต Plotting in 3-D
เราพมพcode นลงไป
[x,y] = meshgrid([-2:.2:2]);
Z = x.*exp(-x.^2-y.^2);
figure
surf(x,y,Z,gradient(Z))
gradient(Z)
colorbar การแสดงแทบส
กำรสรำงลกโลก
grs80 = referenceEllipsoid('grs80','km');
figure('Renderer','opengl')
ax = axesm('globe','Geoid',grs80,'Grid','on',
'GLineWidth',1,'GLineStyle','-','Gcolor',[0.9 0.9
0.1],'Galtitude',100);
ax.Position = [0 0 1 1];
axis equal off
view(3)
load topo
geoshow(topo,topolegend,'DisplayType','texturemap')
demcmap(topo)
land = shaperead('landareas','UseGeoCoords',true);
plotm([land.Lat],[land.Lon],'Color','black')
rivers = shaperead('worldrivers','UseGeoCoords',true);
plotm([rivers.Lat],[rivers.Lon],'Color','blue')
TopoToolBox
>>วธเรยกใหfile ภาพทอยใน Drive D สามารถทา ไดดงน>>ไปทแถบเมน เลอก Set Path
>> จากนน Add folder >> เลอก folder>>จากนนกด save
>> จากนนก browse for folder
>> เมอกดท browse for folder เสรจกจะปรากฏ folder ขนมา
>> ก าหนดคาตวแปร DEM
DEM มาจากค าวา Digital Elevation Mode
>> เมอเรากด enter จะปรากฏขอมลแบบนขนมา
>> เราจะตรวจสอบวาDEM ของเราใชไดหรอไม จะพมพค าสง “imagesc(DEM)” ลงไป และกด
Enter กจะปรากฎภาพ ดงนขนมา สงเกตไดวา ในภาพจะมสทตางกน สแดง คอ พนทสงท และ
สน าเงน คอพนทต า
>> ถาเราตองการใหภาพปรากฏเปนลกษณะเปนภมประเทศ เราจะพมพค าสง “gradient8” ลง
ไป กจะปรากฏภาพขนมา
>> เราสามารถท าภาพเปนภาพสามมต(3D) แบบน โดยการ พมพ code ดานลาง
ภาพทได
กำรศกษำรองน ำหรอ fillsink
>> เราจะพมพ code อนนลงไป และกด Enter
>>จะปรากฏภาพแบบนขนมา เสนทเหนคอ เสนทางของรองน า
กำรระบำยของรองน ำ
>> เราจะพมพ code เหลานลงไป
>> เมอกด enter เราจะไดผลแบบน
ภาพทได
Image processing Basic
>> Down loadภาพ ทมสสน และเปนภาพสเทา หรอ gary tone >> ตงชอภาพ
>> จากนน browse for folder >> ปรากฏ file ภาพของเราขนมา
>> จากนนพมพ code a = imread(‘ชอของภาพ.นามสกลของภาพ เชนjpg’)
>> กด Enter และจะไดผลลพธ ดงน
เลข8 คอบต
color จะมตงแต 0-255
โดยท 0 กคอสด า 255 คอสขาว
>> พมพ figure,imshow(a) เพอใหภาพแสดงบนหนาจอ
>> ถาตองการใหภาพเปนภาพสเทา(gray tone) เราจะพมพค าสง figure,imshow(gray) และกด
Enter
>> ถาเราจะดคาเชงคลนหรอhistogram ของภาพ เราจะพมพค าวา figure,imhist(gray)
>> การแปลงเปนภาพขาวด า พมพ code
Imadjust กำรปรบควำมคมชดใหกบขอมลภำพ
>> ก าหนดคา ตวแปร b = imread (‘2.jpg’)
ภาพทได
>>เราจะท าใหภาพชดขนโดย ค าสง
ภาพทได
ภาพกอนท า ภาพหลงท า
>> พมพ imhist(ad) เพอด คาเชงคลนของภาพ
Image Function
>> ก าหนดคาสตางๆ ในรปแบบของ aray
รปทได
Webcam
>> พมพค าสง imaqtool
>> เมอกด Enter จะขนหนาตาง Image Acquisition tool ขนมา
>> กดท start preview>> Start Acquisition
>>พมพ code ดานบน และกดSave และ Run
เราจะถายรปทงหมด 10แอค
vid เปนการเรยกdiverของกลองขนมา
getsnapshot เปนค าสงทใชถายภาพ
และfnaime เปนการตงชอ
i เปนจ านวนรอบในการท าซ า
>> กด chang floder กลองจะท าการบนทกภาพและเกบไวในfolder
กำรท ำภำพ 3 มต 3D
เลอกภาพจาก Internet คนหาค าวา stereo pair
>> เลอกภาพจาก Internet คนหาค าวา stereo pair จากนน save ภาพ ใสfolder
>> เปดในโปรแกรม paint
>> จากนนเราจะท าการแบงรปภาพเปนฝงซายและขวา
>> โดยการกดท select จากนนลากมนครอบรปฝงซาย และกด crop จากนน กด save และตงชอ
จากนน กด ctrl + z เพอใหภาพกลบมาเปนเปนแบบเดม และท าแบบเดยวกนในฝงขวา
>> เราจะไดภาพทงหมด 3 ภาพ
>> จากนน browse for folder >> จะขน file ท current folder
>> จากนน กดท new script
ใสcode ขางลางนลงไป เปลยน lion 1และ 2 เปนชอ file ของตวเอง
I1 = rgb2gray(imread('Lion1.jpg'));
I2 = rgb2gray(imread('Lion2.jpg'));
imshowpair(I1, I2,'montage');
title('I1 (left); I2 (right)');
figure;
imshowpair(I1,I2,'ColorChannels','red-cyan');
title('Composite Image (Red - Left Image, Cyan - Right Image)');
blobs1 = detectSURFFeatures(I1, 'MetricThreshold', 2000);
blobs2 = detectSURFFeatures(I2, 'MetricThreshold', 2000);
figure;
imshow(I1);
hold on;
plot(selectStrongest(blobs1, 30));
title('Thirty strongest SURF features in I1');
figure;
imshow(I2);
hold on;
plot(selectStrongest(blobs2, 30));
title('Thirty strongest SURF features in I2');
[features1, validBlobs1] = extractFeatures(I1, blobs1);
[features2, validBlobs2] = extractFeatures(I2, blobs2);
indexPairs = matchFeatures(features1, features2, 'Metric', 'SAD', ...
'MatchThreshold', 5);
matchedPoints1 = validBlobs1(indexPairs(:,1),:);
matchedPoints2 = validBlobs2(indexPairs(:,2),:);
figure;
showMatchedFeatures(I1, I2, matchedPoints1, matchedPoints2);
legend('Putatively matched points in I1', 'Putatively matched points in I2');
[fMatrix, epipolarInliers, status] = estimateFundamentalMatrix(...
matchedPoints1, matchedPoints2, 'Method', 'RANSAC', ...
'NumTrials', 10000, 'DistanceThreshold', 0.1, 'Confidence', 99.99);
if status ~= 0 || isEpipoleInImage(fMatrix, size(I1)) ...
|| isEpipoleInImage(fMatrix', size(I2))
error(['Either not enough matching points were found or '...
'the epipoles are inside the images. You may need to '...
'inspect and improve the quality of detected features ',...
'and/or improve the quality of your images.']);
end
inlierPoints1 = matchedPoints1(epipolarInliers, :);
inlierPoints2 = matchedPoints2(epipolarInliers, :);
figure;
showMatchedFeatures(I1, I2, inlierPoints1, inlierPoints2);
legend('Inlier points in I1', 'Inlier points in I2');
[t1, t2] = estimateUncalibratedRectification(fMatrix, ...
inlierPoints1.Location, inlierPoints2.Location, size(I2));
tform1 = projective2d(t1);
tform2 = projective2d(t2);
I1Rect = imwarp(I1, tform1, 'OutputView', imref2d(size(I1)));
I2Rect = imwarp(I2, tform2, 'OutputView', imref2d(size(I2)));
% transform the points to visualize them together with the rectified images
pts1Rect = transformPointsForward(tform1, inlierPoints1.Location);
pts2Rect = transformPointsForward(tform2, inlierPoints2.Location);
figure;
showMatchedFeatures(I1Rect, I2Rect, pts1Rect, pts2Rect);
legend('Inlier points in rectified I1', 'Inlier points in rectified I2');
Irectified = cvexTransformImagePair(I1, tform1, I2, tform2);
figure;
imshow(Irectified);
title('Rectified Stereo Images (Red - Left Image, Cyan - Right Image)');
cvexRectifyImages('lions_left.jpg', 'lion_right.jpg');
>>เปลยนชอ lion 1และ2 เปน lift และ right (หรอชออะไรกไดตามทตองการ)
>>กด SAVE >> และ กด Run จะมภาพมาปรากฎขนมาหลายๆภาพซอนกน
>> detectSURFFeatures จะวาภาพทง 2 ภาพ มอะไรคลายกนบาง จดทเหนนน จะบอกถงจดท
คลายกน
>> ใสcode เปลยน lion 1และ 2 เปนชอ file ของตวเอง
I1 = rgb2gray(imread('Lion1.jpg')); I2 = rgb2gray(imread('Lion2.jpg')); imshowpair(I1, I2,'montage'); title('I1 (left); I2 (right)'); figure; imshowpair(I1,I2,'ColorChannels','red-cyan'); title('Composite Image (Red - Left Image, Cyan - Right Image)'); blobs1 = detectSURFFeatures(I1, 'MetricThreshold', 2000); blobs2 = detectSURFFeatures(I2, 'MetricThreshold', 2000);
figure; imshow(I1); hold on; plot(selectStrongest(blobs1, 30)); title('Thirty strongest SURF features in I1');
figure; imshow(I2); hold on; plot(selectStrongest(blobs2, 30)); title('Thirty strongest SURF features in I2'); [features1, validBlobs1] = extractFeatures(I1, blobs1); [features2, validBlobs2] = extractFeatures(I2, blobs2); indexPairs = matchFeatures(features1, features2, 'Metric', 'SAD', ... 'MatchThreshold', 5); matchedPoints1 = validBlobs1(indexPairs(:,1),:); matchedPoints2 = validBlobs2(indexPairs(:,2),:); figure;
showMatchedFeatures(I1, I2, matchedPoints1, matchedPoints2); legend('Putatively matched points in I1', 'Putatively matched points in I2'); [fMatrix, epipolarInliers, status] = estimateFundamentalMatrix(... matchedPoints1, matchedPoints2, 'Method', 'RANSAC', ... 'NumTrials', 10000, 'DistanceThreshold', 0.1, 'Confidence', 99.99);
if status ~= 0 || isEpipoleInImage(fMatrix, size(I1)) ... || isEpipoleInImage(fMatrix', size(I2)) error(['Either not enough matching points were found or '... 'the epipoles are inside the images. You may need to '... 'inspect and improve the quality of detected features ',... 'and/or improve the quality of your images.']); end
inlierPoints1 = matchedPoints1(epipolarInliers, :); inlierPoints2 = matchedPoints2(epipolarInliers, :);
figure; showMatchedFeatures(I1, I2, inlierPoints1, inlierPoints2); legend('Inlier points in I1', 'Inlier points in I2'); [t1, t2] = estimateUncalibratedRectification(fMatrix, ... inlierPoints1.Location, inlierPoints2.Location, size(I2)); tform1 = projective2d(t1); tform2 = projective2d(t2); I1Rect = imwarp(I1, tform1, 'OutputView', imref2d(size(I1))); I2Rect = imwarp(I2, tform2, 'OutputView', imref2d(size(I2)));
% transform the points to visualize them together with the rectified images pts1Rect = transformPointsForward(tform1, inlierPoints1.Location); pts2Rect = transformPointsForward(tform2, inlierPoints2.Location);
figure; showMatchedFeatures(I1Rect, I2Rect, pts1Rect, pts2Rect); legend('Inlier points in rectified I1', 'Inlier points in rectified I2'); Irectified = cvexTransformImagePair(I1, tform1, I2, tform2); figure; imshow(Irectified); title('Rectified Stereo Images (Red - Left Image, Cyan - Right Image)'); cvexRectifyImages('lions_left.jpg', 'lion_right.jpg');
>> เปลยนชอ lion 1และ2 เปน lift และ right (หรอชออะไรกไดตามทตองการ)
>> กด SAVE >> และ Run
detectSURFFeatures จะวาภาพทง 2 ภาพ มอะไรคลายกนบาง จดทเหนนน จะบอกถงจดท
คลายกน
Guide
คอ โปรแกรมส าเรจรปทชวยใหเราสะดวกสบายมากขน
>> ไปท browse for folder
>> พมพ guide เมอ
>> จากนนกดท push button สรางปม
>> จากนนกดท push button สรางปม
>> จากนนคลกท axes สราง axes ขนมา
>> เราสามารถเปลยน ชอ ส สอกษร ของปมโดยการ ดบเบลคลกเขาไปจะมหนาตาง inspector
ขนมา
ไปท>>string เพอเปลยนชอ>> Backgroundcolor เพอเปลยนสพนหลง>> และ
foregroundColor เพอเปลยนสตวอกษร
>> เมอเราเปลยน ชอ สพนหลงและตวอกษรเสรจแลว จะไดแบบน
>> ผลลพธ
>> เราจะท าการเรยกภาพขนมา โดยการ คลกขวา ไปท View callback >> call back
>> วาง code
global x
[filename, pathname] = uigetfile('*.jpg','Load Image');
if filename==0
msgbox('Please Choose Image','Error')
return
end
x=imread(filename);
axes(handles.axes1);
imshow(x); title('Loaded Image');
ในบรรทดท 81-89
set(handles.axes1,'Visible','off'); ท 57 เพอซอนกราฟ
ปม gray tone
global x
a=rgb2gray(x)
axes(handles.axes1);
imshow(a); title('Gray scale');
ปม histogram พมพ code
global x
c=rgb2gray(x) imhist(c); axes(handles.axes1); ปม BW พมพcode global x c=rgb2gray(x) imhist(c); axes(handles.axes1);
>>จากนน กเลอกภาพทตองการ
ภาพสปกต
gray tone
ภาพเชงคลนหรอ histogram
ภาพขาว-ด า
กำรผสมสภำพถำยดำวเทยม
>>browse for folder >> เลอกfolder>>กด ctrl + N หรอ new >> จากนน พมพcode ลงไป
R = histeq(imread('D:\Com_pro\week13\B40.TIF'));
G = histeq(imread('D:\Com_pro\week13\B50.TIF'));
B = histeq(imread('D:\Com_pro\week13\B30.TIF'));
RGB = cat(3,R,G,B); (cat ในทนหมายถงการผสมส)
figure,imshow(RGB);
>>จากนนกด save as >> run
ภาพทได
>> เมอเราตองการเปลยนส ท าไดโดยการเปลยนตวเลข เชน เปลยนจาก
B = histeq(imread('D:\Com_pro\week13\B30.TIF')); เปน
B = histeq(imread('D:\Com_pro\week13\B40.TIF'))
ภาพทไดเมอ เปลยนสแลว