Matlab 58170042 (1)

24
COMPUTER PROGRAMMING MATLAB เสนอ อาจารย์กฤษณะ อิ่มสวาสดิ จัดทาโดย นางสาวโสภิตา ทองสร้าง 58170042 กลุ่ม 01 นิสิตชั ้นปี ที1 ปีการศึกษา 2558 คณะภูมิสารสนเทศศาสตร์ มหาวิทยาลัยบูรพา

Transcript of Matlab 58170042 (1)

Page 1: Matlab 58170042 (1)

COMPUTER PROGRAMMING

MATLAB

เสนอ

อาจารยกฤษณะ อมสวาสด

จดท าโดย

นางสาวโสภตา ทองสราง 58170042 กลม 01

นสตชนปท 1 ปการศกษา 2558

คณะภมสารสนเทศศาสตร

มหาวทยาลยบรพา

Page 2: Matlab 58170042 (1)

แมตแลบ (องกฤษ: MATLAB: Matrix Laboratory) เปนซอฟตแวรในการค านวณและการเขยน

โปรแกรม โปรแกรมหนง ทมความสามารถครอบคลมตงแต การพฒนาอลกอรธม การสรางแบบจ าลองทาง

คณตศาสตร และการท าซมเลชนของระบบ การสรางระบบควบคม และโดยเฉพาะเรอง image processing

และ wavelet การสรางเมตรกซ ผลตโดยบรษทแมตเวรกส ตวแทนจ าหนายในประเทศไทยคอ บรษท เทค

ซอรส ซสเทมส (ประเทศไทย) จ ากด

แมตแลบเปนโปรแกรมส าเรจรปทใชกนอยางแพรหลายในแวดวงของนกวทยาศาสตรและ วศวกร

ในปจจบน ชอโปรแกรม MATLAB นนยอมาจาก Matrix Laboratory แมตแลบไดเรมตน ขนเพอตองการให

เราสามารถแกปญหาตวแปรทมลกษณะเปนเมทรดซไดงายขน แมตแลบ เรมพฒนาครงแรกโดย Dr. Cleve

Molor ซงเขยนโปรแกรมนขนมาดวยภาษาฟอรแทรน โดยโปรแกรมนได พฒนาภายใตโครงการ LINPACK

และ EISPACK

การท างานของโปรแกรม

แมตแลบสามารถท างานไดทงในลกษณะของการตดตอโดยตรง คอการเขยนค าสงเขาไปทละค าสง

เพอใหแมตแลบประมวลผลไปเรอยๆ หรอสามารถทจะรวบรวม ชดค าสงเรานนเปนโปรแกรมกได ขอ

ส าคญอยางหนงของแมตแลบกคอขอมลทกตวจะถกเกบใน ลกษณะของแถวล าดบ คอในแตละตวแปรจะ

ไดรบการแบงเปนสวนยอยเลกๆขน ซงการใชตวแปรเปนแถวล าดบ ในแมตแลบเราไมจ าเปนทจะตองจอง

มตเหมอนกบ การเขยนโปรแกรมในภาษาขนต าทวไป ซงท าใหเราสามารถทจะแกปญหาของตวแปรทอยใน

ลกษณะ ของเมทรกซและเวกเตอรไดโดยงาย ซงท าใหเราลดเวลาการท างานลงไดอยางมากเมอเทยบกบการ

เขยน โปรแกรมโดยภาษาซหรอภาษาฟอรแทรน

Page 3: Matlab 58170042 (1)

การสราง plot กราฟแบบสามมต

ปอนค าสงตอไปนลงใน command line

>> plot(a)

>> x = [-3:1:3];

>> y = [-3:1:3];

>> [xx,yy] = meshgrid(x,y);

>> zz = xx.^2 - yy.^2;

>> figure

>> surf(xx,yy,zz)

Page 4: Matlab 58170042 (1)

>> plot(a)

>> x = [-3:1:3];

>> y = [-3:1:3];

>> [xx,yy] = meshgrid(x,y);

>> zz = xx.^2 - yy.^2;

>> figure

>> surf(xx,yy,zz)

>> shading interp

>> colorbar

Page 5: Matlab 58170042 (1)

Image Progressing

1.วธโหลดไฟลภาพเขาโปรแกรม

ใชค าสง a = imread(‘ชอไฟลรปภาพ.jpg’) กด enter

2.ค าสงเปดไฟลภาพ (ส)

ใชค าสง figure,imshow(…..ชอตวแปรทก าหนด…)

3.ค าสงแปลงสภาพใหเปนสเทา

ก าหนดตวแปร g = rgb2gray

Page 6: Matlab 58170042 (1)

4.ดกราฟฮสโตแกรมของภาพ

ใชค าสง Imhist(g)

5.ค าสงปรบแกภาพ ท าใหภาพชดขน

ใชค าสง ad = imadjust(b)

Figure,imshow(ad)

Page 7: Matlab 58170042 (1)

การท า 3D stereo

1ตดรป 3d stereo แบงออก ซาย-ขวา

Browse เอาน ารปทตดแลวเขามา

ใสโคด ใน command line

I1 = rgb2gray(imread('pic1.jpg')); I2 = rgb2gray(imread('pic2.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;

Page 8: Matlab 58170042 (1)

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');

กด Run จะแสดงผลเปนภาพ 3D

Page 9: Matlab 58170042 (1)
Page 10: Matlab 58170042 (1)

Guide

1.พมพ guide แลว Enter จะมกลองขอความแสดงขนมา

2.เลอก bank gui จะไดแบบน

Page 11: Matlab 58170042 (1)

หากชอเครองมอดายซายมอไมม ใหคลกทไฟลเลอก preferences.. ตกเลอก show name in component

สรางป มโดยใช push button

เลอก axes

Page 12: Matlab 58170042 (1)

เปลยนชอและตกแตงป ม

ใส tag เพอใชในการคยโคด

Page 13: Matlab 58170042 (1)

ออกแบบไดตามตองการ

Page 14: Matlab 58170042 (1)

ใสโคด

Save

Page 15: Matlab 58170042 (1)

หลงจาก save จะไดโคดแบบน

แลว copy โคดขางลางน

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

Page 16: Matlab 58170042 (1)

แลว copy โคดนไปใสในบรรทดท 57

set(handles.axes1,'Visible','off');

จากนนคลกทป ม gray ท าเหมอนขางบน copy โคดดานลางไปใสในบรรทดท 97

global x

a=rgb2gray(x)

Page 17: Matlab 58170042 (1)

axes(handles.axes1);

imshow(a); title('Gray scale');

จากนนคลกทป ม his ท าเหมอนขางบน copy โคดดานลางไปใสในบรรทดท 107

global x c=rgb2gray(x) imhist(c); axes(handles.axes1);

Page 18: Matlab 58170042 (1)

จากนนคลกทป ม BW ท าเหมอนขางบน copy โคดดานลางไปใสในบรรทดท 117

global x c=rgb2gray(x) imhist(c); axes(handles.axes1);

จากนนคลกทป ม reset ท าเหมอนขางบน copy โคดดานลางไปใสในบรรทดท 117

global x

axes(handles.axes1);

imshow(x);

Page 19: Matlab 58170042 (1)

จากนนกด run แลวเลอก change folder

Page 20: Matlab 58170042 (1)

จะไดดงน

จากนนกลบไปทหนาหลก กด save

Page 21: Matlab 58170042 (1)

จากนนกลบมาหนาน

กด run และอพโหลดรปภาพ จากนนลองกด gray

Page 22: Matlab 58170042 (1)

การผสมสภาพ browse folder

Select Folder

กด new script และวางโคด

R = histeq(imread('D:\Com_pro\LS5\B40.TIF')); G = histeq(imread('D:\Com_pro\LS5\B50.TIF')); B = histeq(imread('D:\Com_pro\LS5\B30.TIF'));

Page 23: Matlab 58170042 (1)

RGB = cat(3,R,G,B); figure,imshow(RGB);

กด run และ save จากนนคลก change folder

Page 24: Matlab 58170042 (1)

จะได