You are on page 1of 74

Dr.

Khadija ABID

10/02/2018
‫‪2‬‬ ‫‪18/02/2020‬‬

‫‪Plan‬‬
‫كيفية التعامل مع ‪ MATLAB‬ألول مرة‬

‫كيفية حل مسائل رقمية‬

‫كيفية إجراء عمليات على المصفوفات‬

‫كيفية الرسم‪Plotting :‬‬

‫كيفية حل المعادالت‬

‫برنامج السيميولينك‬
3 18/02/2020

Questions
‫‪4‬‬ ‫‪18/02/2020‬‬

‫ما هو ماتالب؟‬
‫‪Matrix Laboratory‬‬
‫برنامج ولغة لتطبيق العمليات‬
‫الرياضية للحسابات العلمية‬
‫والهندسية‬
‫مصمم لعمليات المصفوفات‬
‫صمم في بداية السبيعنات‬
‫و مبني على مكتبات‬
‫‪LINPACK FORTRAN‬‬
‫أيضاً مشهور بقدراته الخاصة‬
‫بالرسومات واألدوات المختلفة‬
‫التطبيقات‬
5 18/02/2020

‫ما هو ماتالب؟‬
‫برنامج‬
‫هندسي‬
Mechanical field
Electronics
Automotive Industry
Aerospace & Defense
Integration
MATLAB Differentiation
Algebric Equation
Partial fraction

‫التطبيقية‬ ‫األكاديمية‬
6 18/02/2020

‫ما هو ماتالب؟‬
OUTPUT OUTPUT

INPUT

INPUT

COMMANDS
7 18/02/2020

Matlab ‫نافذة‬
Matlab ‫ذاكرة‬

‫اكتب األوامر هنا‬


Workspace
Variables/Current
Directory
(‫ الجديدة‬Matlab ‫)اصدارات‬
‫األوامر المدخلة‬
ً‫سابقا‬

Command
History
(‫ الجديدة‬Matlab
‫)اصدارات‬
‫ نافذة األوامر‬Command
Window
8 18/02/2020

Matlab ‫أجزاء‬
 Operators ‫ العمليات‬
 Matrices ‫ المصفوفات‬
 Plotting ‫ الرسومات‬
 M-files ‫ الملفات‬
 Flow control ‫ التحكم بالتنفيذ‬
 Simulink Simulink 
 Misc commands ‫ أوامر أخرى‬
‫‪9‬‬ ‫‪18/02/2020‬‬

‫العمليات في برنامج ‪MATLAB‬؟‬


‫‪10‬‬ ‫‪18/02/2020‬‬

‫العمليات في برنامج ‪MATLAB‬‬


‫‪11‬‬ ‫‪18/02/2020‬‬

‫لنفترض أننا قمنا بتغيير الناتج ‪ 5‬إلى ‪ ,3‬قم بإغالق نافذة تعديل النتائج‬

‫ستالحظ عودة نافذة األوامر لوضعها األساسي‪ ,‬قم بكتابة‬


‫‪ans‬‬
‫القيمة الجديدة ‪3‬‬
‫‪12‬‬ ‫‪18/02/2020‬‬

‫بعض األمثلة‬
‫هنا نطلب من المستخدم ادخال الدالة‬
‫;)'‪reply = input('Entez un fonction: ','s‬‬
‫و هنا نحول المتغير السابق الذي هو عبارة عن نص الى دالة يفهمها الماتالب‬
‫;)‪f = inline(reply‬‬
‫هنا معرف اسم المتغير الذي سوف نشتق ألجله الدالة‬
‫‪syms‬و اجراء‬
‫ادخال;‪ x‬دالة‬
‫عملية االشتقاق؟‬ ‫و هنا عملية اإلشتقاق‬
‫;)‪chain_f=diff(char(f), x‬‬
‫و هنا طباعة اسم الدالة المشتقة على الشاشة‬
‫;)‪disp (chain_f‬‬
‫‪13‬‬ ‫‪18/02/2020‬‬

‫بعض األمثلة‬
‫هو تقريبا نفس الكود السابق فنحن نطلب من المستخدم‬
‫ادخال الدالة ثم ادخال حد التكامل األول ثم الثاني ثم نقوم‬
‫بحساب قيمة التكامل للدالة على هذا المجال‬
‫;)'‪reply = input('Entez un fonction: ','s‬‬
‫;)‪f = inline(reply‬‬
‫;)' ‪a = input('Entez la 1 er borne de lintegrale:‬‬
‫‪b = input('Entez la 2 emme‬‬ ‫‪borne‬‬
‫لدالة‬ ‫‪de lintegrale:‬‬
‫التكامل‬ ‫قيمة‬ ‫;)'‬
‫;‪syms x‬‬
‫هنا نحسب التكامل على المجال المعني‬
‫;)‪chain_f=int(char(f),x, a, b‬‬
‫;)‪disp (chain_f‬‬
‫‪14‬‬ ‫‪18/02/2020‬‬

‫متغيرات ‪Matlab‬‬
‫قيمة ‪‬‬ ‫تخزن جميع متغيرات كأرقام حقيقية وبحجم ‪ 8‬بايت لكل‬
‫ال فرق بين األرقام الصحيحة والحقيقية‪ :‬‬
‫;‪>> p = 3‬‬
‫;‪>> p = 3.0‬‬ ‫نفس األمر!‬
‫كقيم‪ :‬‬ ‫يمكن تخزين أرقام أو حروف‬
‫;‪>>number_123=7‬‬
‫;'‪>>text04='This is GE204‬‬
‫والصغيرة‪ :‬‬ ‫يوجد فرق بين الحروف الكبيرة‬
‫;‪>> a = 4‬‬
‫;‪>> A = 10‬‬ ‫‪Aa‬‬
‫الذاكرة‪ :‬‬ ‫يمكن مسح المتغير من‬
‫‪>> clear a‬‬
15 18/02/2020

Matlab ‫بعض دوال‬


 exp(x) ex  pi p
 sin(x) sin x  i,jimaginaryunit
 asin(x) sin-1x  NaNNot-a-Number
 log(x) ln x  Inf 
 log10(x)log x
 sqrt(x) x
 abs(x) |x|
 sum(x) x i
‫‪16‬‬ ‫‪18/02/2020‬‬

‫‪Matlab Help‬‬

‫• اكتب‬
‫‪>>help‬‬
‫لمعرفة‬
‫المزيد عن‬
‫أوامر ودوال‬
‫‪Matlab‬‬
17 18/02/2020

Matlab Help

>> help sqrt

square root
function help
18 18/02/2020
‫‪19‬‬ ‫‪18/02/2020‬‬

‫المصفوفات‬
‫]‪>> A=[6 5; 3 4‬‬
‫=‪A‬‬
‫‪6 5‬‬ ‫‪ ‬تبرز قوة ‪ Matlab‬عند‬
‫استخدام المصفوفات‬
‫‪3‬‬ ‫‪4‬‬
‫‪Arrays‬المصفوفات‬

‫‪ ‬الدخال مصفوفة‪ ،‬استخدم‬


‫]‪>> B=[1 2 3 4‬‬
‫األقواس المربعة [ ]‬
‫=‪B‬‬
‫‪ ‬الدخال صف آخر‪ ،‬اضغط ; أو‬
‫‪1 2 3 4‬‬ ‫‪Enter‬‬
‫‪>> C=[4 8‬‬
‫;]‪7 5‬‬

‫??‬
‫‪20‬‬ ‫‪18/02/2020‬‬

‫المصفوفات‬
‫]‪>> A=[6 5; 3 4‬‬
‫‪ ‬كل مصفوفة تتكون من عناصر‬
‫=‪A‬‬
‫‪ ‬عناصر المصفوفة تكون الصفوف‬
‫‪6‬‬ ‫‪5‬‬ ‫واألعمدة‬
‫‪3‬‬ ‫‪4‬‬ ‫‪ ‬يمكن التعامل مع عناصر‬
‫‪>> A(1,1)=10‬‬ ‫المصفوفة باستخدام رقم الصف‬
‫‪Arrays‬المصفوفات‬

‫والعمود‬

‫أمثلة ؟‬
‫=‪A‬‬
‫‪10‬‬ ‫‪5‬‬ ‫‪ ‬يمكن توليد متجه (مصفوفة ذات‬
‫‪3‬‬ ‫‪4‬‬ ‫بعد واحد) باستخدام األمر‬
‫التالي‪:‬‬
‫‪>> 1:2:7‬‬
‫‪initial:step:final‬‬
‫=‪ans‬‬
‫النهاية‪:‬الخطوة‪:‬البداية‬
‫‪1 3 5 7‬‬
‫(اذا لم يتم تحديد الخطوة‪،‬‬
‫‪>> 1:4‬‬ ‫سيفرض أنها ‪)1‬‬
‫=‪ans‬‬
‫‪1‬‬ ‫‪2‬‬ ‫‪3‬‬ ‫‪4‬‬
‫‪21‬‬ ‫‪18/02/2020‬‬

‫عمليات المصفوفات‬
‫*‬ ‫‪ ‬الضرب‬
‫*‪.‬‬ ‫‪ ‬ضرب العناصر‬
‫‪/‬‬ ‫‪ ‬القسمة‬
‫‪./‬‬ ‫‪ ‬قسمة العناصر‬
‫‪Arrays‬المصفوفات‬

‫^‬ ‫‪ ‬األس‬
‫^‪.‬‬ ‫‪ ‬األس للعناصر‬
‫))(‪'(or transpose‬‬ ‫‪ ‬التبديل‬
‫))(‪^-1 (or inv‬‬ ‫‪ ‬المعكوس‬
‫)(‪length‬‬ ‫‪ ‬طول المصفوفة‬
‫)(‪size‬‬ ‫‪ ‬أبعاد المصفوفة‬
‫‪22‬‬ ‫‪18/02/2020‬‬

‫أمثلة لعمليات المصفوفات‬


‫‪1 2 3‬‬ ‫‪ 2 3 4‬‬ ‫‪4 5‬‬
‫‪A‬‬ ‫‪‬‬ ‫‪B‬‬ ‫‪‬‬ ‫‪6 7 ‬‬
‫‪4 5 6‬‬ ‫‪5 6 7 ‬‬ ‫‪C‬‬ ‫‪‬‬
‫‪‬‬ ‫‪‬‬
‫صحيحة‬ ‫‪8 9 ‬‬
‫‪ ‬لجمع وطرح‬
‫;‪>>D=A+B‬‬ ‫المصفوفات ‪ ،‬يجب أن‬
‫‪Arrays‬المصفوفات‬

‫;‪>>D=A-B‬‬ ‫تكون المصفوفات من‬


‫;‪>>D=A*C‬‬ ‫ضرب‬
‫نفس الحجم‬
‫المصفوفات غير ;‪>>D=C*A‬‬
‫;‪>>D=A.*B‬‬ ‫ابدالي‬ ‫‪ ‬لضرب المصفوفات ال بد‬
‫من تساوي األبعاد‬
‫خطأ‬
‫الداخلية‬
‫;‪>>D=A+C‬‬
‫;‪>>D=A*B‬‬
‫;‪>>D=B*A‬‬
‫;‪>>D=A.*C‬‬
‫‪23‬‬ ‫‪18/02/2020‬‬

‫عمليات المصفوفات‬
‫أمر ‪Matlab‬‬ ‫العملية الرياضية‬

‫;‪>> C=A\B‬‬ ‫‪1‬‬


‫‪CA B‬‬
‫‪Arrays‬المصفوفات‬

‫‪1‬‬
‫;‪>> C=B/A‬‬ ‫‪C  BA‬‬
24 18/02/2020

‫تبديل المصفوفة‬
‫ العملية الرياضية‬
Matlab ‫أمر‬
C=AT
>>C=A’;
1 2 3
‫المصفوفات‬Arrays

A 
 4 5 6 
>>B=A’;

1 4 
B  2 5
 
 3 6 
25 18/02/2020

‫تضمين المصفوفات‬
1 2 3  2 3 4
A  B 
 4 5 6   5 6 7 
‫المصفوفات‬Arrays

>> D=[A B];


A B

1 2 3 2 3 4
D 
4 5 6 5 6 7
26 18/02/2020

‫تطبيق‬
 Créez un tableau tab contenant les entiers pairs
de 0 à 42,
 écrivez un script qui parcourt le tableau et
remplace chacune des valeurs par son carré.
‫المصفوفات‬Arrays

>>tab = 0:2:42;
puis dans un script :

for i = 1:size(tab,2)
tab(i) = tab(i).^2;
end
27 18/02/2020

‫تطبيق‬
 Définissez une matrice M = [1 2 ; 3 4] puis
essayez les opérations suivantes dans
l’interpréteur:
>> 2 * M + 3
‫المصفوفات‬Arrays

>> M + M
>> sqrt(M)
>> M * M
>> M .* M
>> ones(4)
>> ones(3, 5)
28 18/02/2020

Comment peut-on créer facilement


‫تطبيق‬
une matrice 54 42 ne contenant
que des 7 ?

>> M = 7 * ones(54, 42);


‫المصفوفات‬Arrays
29 18/02/2020
‫‪30‬‬ ‫‪18/02/2020‬‬

‫;‪>> x=0:.1:20‬‬ ‫رسومات ‪Matlab‬‬


‫>>‬ ‫;)‪y=exp(0.1*x).*sin(x‬‬
‫>>‬ ‫)‪plot(x,y‬‬ ‫‪ ‬من مزايا برنامج ‪Matlab‬‬
‫>>‬ ‫)'‪xlabel('Time (t) in Seconds‬‬ ‫الرسومات‬
‫>>‬ ‫)'‪ylabel('Response in mm‬‬ ‫‪ ‬توجد العديد من الخيارات‪،‬‬
‫>>‬ ‫)'‪title('A Simple 2-D Plot‬‬ ‫حيث يمكن التحكم بـ‪:‬‬
‫‪Plotting‬الرسومات‬

‫نوع الرسم‬ ‫‪‬‬


‫نوع النقاط والخطوط‬ ‫‪‬‬
‫العناوين‬ ‫‪‬‬
‫التحكم بالمحاور‬ ‫‪‬‬
‫عدد الرسومات‬ ‫‪‬‬
‫‪31‬‬ ‫‪18/02/2020‬‬

‫أمر ‪plot‬‬
‫;‪>> n=0:11‬‬ ‫‪ !… , y1, x1‬ال بد من تساوي أبعاد‬
‫>>‬
‫;)‪y=sin((pi/6)*n‬‬
‫‪Plotting‬الرسومات‬

‫)‪>> plot(n,y‬‬

‫رسم بدون أية خيارات‬


‫‪32‬‬ ‫‪18/02/2020‬‬

‫يمكن معرفة المزيد‬


‫)]‪ plot(x1,y1[,x2,y2,x3,y3.....‬عن األمر بكتابة‪:‬‬
‫‪>> help plot‬‬

‫)'‪>> plot(n,y,'--‬‬

‫رسم باستخدام‬
‫خط متقطع‬
‫‪33‬‬ ‫‪18/02/2020‬‬

‫أمر ‪plot‬‬
‫‪ ‬كيفية رسم دالة معينة؟‬
‫‪ ‬االمر ‪ PLOT‬لرسم مصفوفة يكون‬

‫]‪>> m = [1, 2; 1, 3; 1, 2‬‬


‫)‪>> plot(m‬‬
‫‪ ‬لتغير الشكل‬

‫‪>> plot([1, 1, 1, 2, 4, 8, 4],‬‬


‫)’‪’r+--‬‬
‫‪34‬‬ ‫‪18/02/2020‬‬

‫أمر ‪subplot‬‬
‫رقم الرسمة‬
‫)‪subplot(m,n,p‬‬
‫عدد الصفوف‬ ‫عدد األعمدة‬
‫‪Plotting‬الرسومات‬
‫‪35‬‬ ‫‪18/02/2020‬‬

‫دوال الرسومات‬
‫‪ title‬‬ ‫‪ ‬لكتابة اسم الرسمة‬
‫‪ xlabel‬‬ ‫‪ ‬لكتابة اسم محور س‬
‫‪ ylabel‬‬ ‫‪ ‬لكتابة اسم محور ص‬
‫‪ grid‬‬ ‫‪ ‬لعرض شبكة بالرسم‬
‫‪Plotting‬الرسومات‬

‫‪ axis‬‬ ‫‪ ‬لتحديد القيم الصغرى والكبرى‬


‫للمحاور‬
‫‪ legend‬‬ ‫‪ ‬لتوضيح أسماء الخطوط‬
‫‪ figure‬‬
‫المرسومة‬
‫‪ plot3‬‬
‫‪ ‬لالنتقال الى رسمة أخرى‬
‫‪ ‬رسمة ثالثية األبعاد‬
36 18/02/2020
‫‪37‬‬ ‫‪18/02/2020‬‬

‫التحكم بالتنفيذ‬
‫;‪a=2‬‬
‫;‪b=3‬‬
‫;‪c=a+b‬‬
‫‪ ‬عادة‪ ،‬يكون تنفيذ األوامر بالتسلسل‬
‫;)‪d=sqrt(b‬‬
‫‪Flow control‬التحكم بالتنفيذ‬

‫‪ ‬باستخدام أوامر التحكم بالتنفيذ‪ ،‬يمكن تغيير هذا‬


‫التسلسل‬
‫‪ ‬األوامر المهمة‪:‬‬
‫‪if‬‬ ‫إذا‬ ‫‪‬‬
‫أوامر شرطية (تنفذ مرة واحدة)‬ ‫إذا‪-‬غير ذلك ‪if-else‬‬ ‫‪‬‬
‫‪while‬‬ ‫طالما‬ ‫‪‬‬
‫‪for‬‬ ‫لــ‪...‬‬ ‫‪‬‬

‫أوامر تكرار‬
38 18/02/2020

while ‫ و‬if ‫أوامر‬


while expression if expression if expression
statements statements statements1
‫التحكم بالتنفيذ‬Flow control

end end else


statements2
end

‫) فقط إذا‬statements( ‫ بتنفيذ األوامر‬Matlab ‫ يقوم‬


)expression( ‫تحقق الشرط‬
‫ إما‬Matlab ‫ يخرج‬،)expression( ‫ عند التحقق من الشرط‬
)false( ‫) أو غير صحيح‬true( ‫بنتيجة صحيح‬
Matlab ‫) هي أي أوامر بالـ‬statements( ‫ األوامر‬
‫‪39‬‬ ‫‪18/02/2020‬‬

‫حساب الشرط‬
‫أية معادلة‬
‫المعامالت الشرطية‬
‫حسابية‬ ‫==يساوي‬
‫;‪a=4‬‬
‫;‪b=5‬‬ ‫<أقل من‬
‫;‪c=5‬‬ ‫>أكبر من‬
‫‪Flow control‬التحكم بالتنفيذ‬

‫ألن الناتج ≠ ‪if a+b “true” 0‬‬ ‫=<أقل من أو يساوي‬


‫ألن الناتج = ‪if b-c “false”  0‬‬ ‫=>أكبر من أو يساوي‬
‫=~ال يساوي‬

‫انتبه من أخطاء التقريب!‬ ‫المعامالت المنطقية‬


‫”‪if sin(0) “false‬‬ ‫و ‪& and‬‬
‫”‪if sin(pi)“true‬‬ ‫‪| or‬‬ ‫أو‬
‫‪sin(pi) = 1.22e-16‬‬
‫)‪while(3<=a)&(a<=5‬‬
40 18/02/2020

)if/if-else( ‫أمثلة‬
>> a=6; >> a=50;
>> if a<10 >> if a<10
b=a/2; b=a/2;
‫التحكم بالتنفيذ‬Flow control

end end
>> b >> b
b = 3 ??? Undefined function or variable 'b'.

>> a=6; >> a=50;


>> if a<10 >> if a<10
b=a/2; b=a/2;
else else
b=a/5; b=a/5;
end end
>> b >> b
b = 3 b = 10
41 18/02/2020

)while( ‫أمثلة‬
>> a=1;
>> a=1;
>> while a<5
>> while a<5
a=a+1
b=a+1
end ‫أمر‬ end
‫التحكم بالتنفيذ‬Flow control

a = 2
a = 3 !‫تكرار‬ b = 2
b = 3
a = 4
b = 4
a = 5
b = 5
>> a=10; b = 6
>> while a<5 b = 7
a=a+1 .
end .
>> a .
.
 loop!
a = 10
‫‪42‬‬ ‫‪18/02/2020‬‬

‫أمر ‪for‬‬
‫‪for index = start : [increment :] end‬‬
‫أمر‬
‫‪statements‬‬
‫تكرار!‬ ‫‪end‬‬
‫‪Flow control‬التحكم بالتنفيذ‬

‫‪ ‬قيمة الزيادة (‪ )increment‬اختيارية‪ ،‬وستفرض قيمتها ‪ 1‬إذا لم‬


‫توضع‬
‫‪ ‬يمكن لقيم البداية والزيادة والنهاية أن تكون كسور‬
‫‪ ‬يمكن للزيادة أن تكون موجبة أو سالبة‬
‫‪ ‬ينتهي التكرار إذا كان المتغير > النهاية (أو المتغير < النهاية )‬
‫‪43‬‬ ‫‪18/02/2020‬‬

‫المعادالت الرمزية‬
‫‪ sym/syms‬‬ ‫‪ ‬لتعريف متغيرات رمزية (‪sym‬‬
‫يحتوي على خيارات اكثر)‬
‫‪ diff‬‬ ‫‪ ‬تفاضل‬
‫‪Misc. commands‬أوامر أخرى‬

‫‪ int‬‬ ‫‪ ‬تكامل‬


‫‪ solve‬‬ ‫‪ ‬حل معادلة‬
‫‪ expand‬‬ ‫‪ ‬لكتابة المعادلة بشكل كامل‬
‫‪ poly2sym‬‬ ‫‪ ‬تحويل المعامالت الى معادلة‬
‫‪ pretty‬‬ ‫‪ ‬لطباعة المعادلة بشكل رياضي‬
‫‪44‬‬ ‫‪18/02/2020‬‬

‫أوامر إضافية‬
‫‪ clc‬‬ ‫‪ ‬مسح الشاشة‬
‫‪ clf‬‬ ‫‪ ‬مسح الرسومات‬
‫‪ format‬‬ ‫‪ ‬التحكم بعرض األرقام‬
‫‪Misc. commands‬أوامر أخرى‬

‫‪ save/load‬‬ ‫‪ ‬تخزين‪/‬تحميل للـ‪Workspace‬‬


‫‪ pwd‬‬ ‫‪ ‬المجلد الحالي‬
‫‪ cd‬‬ ‫>‪<drive:\folder‬‬ ‫‪ ‬لتغيير المجلد الحالي‬
‫‪ fprintf‬‬ ‫‪ ‬للطباعة على الشاشة‬
‫‪ diary‬‬ ‫‪ ‬لتخزين النواتج‬
45 18/02/2020
‫‪46‬‬ ‫‪18/02/2020‬‬

‫ما هو ‪Simulink‬؟‬
‫‪Simulink‬هو برنامج للنمذجة و المحاكاة و تحليل االنظمة‬ ‫‪‬‬

‫الديناميكية سواء كانت خطية او غير خطية و يقوم أيضا بنمذجة‬


‫االنظمة سواء فى الزمن المستمر او فى الزمن الغير مستمر ‪.‬‬
‫وباستخدام ال ‪simulink‬يمكنك بناء نماذج من البداية او التعديل‬
‫على انظمة موجودة بالفعل والفائدة من ذلك هو دراسة خصائص‬
‫نظام التحكم اوالمنظومة قبل البدء فى التنفيذ حتى نحدد مدى‬
‫استجابة النظام لما نقوم بعمله‬
‫‪47‬‬ ‫‪18/02/2020‬‬

‫ما هو ‪Simulink‬؟‬
‫‪ ‬ويعتبر ال ‪SIMULINK‬اداة ممتازة لى ‪Model-Based‬‬
‫‪Design‬وهذا معناه ان البرنامج ليس فقط قاصرا على‬
‫االنظمة المثالية ولكن يمكنك ايضا من نمذجة انظمة‬
‫حقيقة والتى يوجد بها عوامل موثرة لجعلها غير خطية‬
‫‪nonlinear‬مثل االحتكاك ومقامة الهواء وانزالق‬
‫التروس والظواهر الطبيعية االخرى ‪.‬‬
‫‪48‬‬ ‫‪18/02/2020‬‬

‫أمثلة‬
‫‪49‬‬ ‫‪18/02/2020‬‬

‫أمثلة‬
‫‪50‬‬ ‫‪18/02/2020‬‬

‫أمثلة‬
‫‪51‬‬ ‫‪18/02/2020‬‬

‫أمثلة‬
‫‪52‬‬ ‫‪18/02/2020‬‬

‫أمثلة‬
‫‪53‬‬ ‫‪18/02/2020‬‬

‫أمثلة‬
‫‪54‬‬ ‫‪18/02/2020‬‬

‫أمثلة‬
55 18/02/2020
‫‪56‬‬ ‫‪18/02/2020‬‬

‫التعامل مع الصور في ماتالب‬


‫‪57‬‬ ‫‪18/02/2020‬‬

‫التعامل مع الصور في ماتالب‬


58 18/02/2020

‫التعامل مع الصور في ماتالب‬


 A = imread(filename)
 A = imread(filename,fmt)
 A = imread(___,idx)
 A = imread(___,Name,Value)
 [A,map] = imread(___)
 [A,map,transparency] = imread(___)
 Description
 example
 A = imread(filename) reads the image from the file
specified by filename, inferring the format of the file
from its contents. If filename is a multi-image file,
then imread reads the first image in the file.
59 18/02/2020

‫التعامل مع الصور في ماتالب‬


 A = imread(filename,fmt) additionally specifies
the format of the file with the standard file
extension indicated by fmt. If imread cannot find
a file with the name specified by filename, it
looks for a file named filename.fmt.
 A = imread(___,idx) reads the specified image or
images from a multi-image file. This syntax
applies only to GIF, PGM, PBM, PPM, CUR, ICO,
TIF, and HDF4 files. You must specify a filename
input, and you can optionally specify fmt.
 example
 A = imread(___,Name,Value) specifies format-
specific options using one or more name-value
pair arguments, in addition to any of the input
arguments in the previous syntaxes.
60 18/02/2020

‫التعامل مع الصور في ماتالب‬


 example
 [A,map] = imread(___) reads the indexed image
in filename into A and reads its associated
colormap into map. Colormap values in the
image file are automatically rescaled into the
range [0,1].
 example
 [A,map,transparency] = imread(___) additionally
returns the image transparency. This syntax
applies only to PNG, CUR, and ICO files. For PNG
files, transparency is the alpha channel, if one is
present. For CUR and ICO files, it is the AND
(opacity) mask.
61 18/02/2020

‫التعامل مع الصور في ماتالب‬


 example
 [A,map] = imread(___) reads the indexed image
in filename into A and reads its associated
colormap into map. Colormap values in the
image file are automatically rescaled into the
range [0,1].
 example
 [A,map,transparency] = imread(___) additionally
returns the image transparency. This syntax
applies only to PNG, CUR, and ICO files. For PNG
files, transparency is the alpha channel, if one is
present. For CUR and ICO files, it is the AND
(opacity) mask.
62 18/02/2020

‫االلوان و الصور في ماتالب‬


RGB ‫قبل تحويل الصور يجب علينا تحويلها الى‬

 Convert indexed image to RGB image


 Syntax
 RGB = ind2rgb(X,map)
 Description
 RGB = ind2rgb(X,map) converts the indexed
image X and corresponding colormap map
to RGB (truecolor) format.
63 18/02/2020

‫االلوان و الصور في ماتالب‬


Read the first image in the sample indexed
image file, corn.tif.
 [X,cmap] = imread('corn.tif');
 The indexed image X is a 415-by-312 array of
type uint8. The colormap cmap is a 256-by-3
matrix of type double, therefore there are 256
colors in the indexed image.

Display the image.


 imshow(X,cmap)
64 18/02/2020

‫االلوان و الصور في ماتالب‬


 I = rgb2gray(RGB)
 newmap = rgb2gray(map)
Description
 example
 I = rgb2gray(RGB) converts the truecolor image
RGB to the grayscale image I. The rgb2gray
function converts RGB images to grayscale by
eliminating the hue and saturation information
while retaining the luminance. If you have
Parallel Computing Toolbox™ installed, rgb2gray
can perform this conversion on a GPU.
 example
 newmap = rgb2gray(map) returns a grayscale
colormap equivalent to map.
65 18/02/2020

‫عرض الصور في ماتالب‬


 Examples
 Convert RGB Image to Grayscale Image

 Read and display an RGB image, and then


convert it to grayscale.
 Read the sample file, peppers.png, and
display the RGB image.
 RGB = imread('peppers.png');
 imshow(RGB)
 I = rgb2gray(RGB); figure imshow(I)
66 18/02/2020

‫عرض الصور في ماتالب‬


 Display Multiple Images in Separate Figure
Windows
 imshow(I(:,:,:,1))
 figure, imshow(I(:,:,:,2))
 figure, imshow(I(:,:,:,3))

 onion = imread('onion.png'); onionArray =


repmat(onion, [ 1 1 1 4 ]);
 montage(onionArray);
 :‫ثم جرب‬
 montage(onionArray,'size',[1 NaN]);
67 18/02/2020

‫عرض الصور في ماتالب‬


 Display Multiple Images in Separate Figure
Windows
 imshow(I(:,:,:,1))
 figure, imshow(I(:,:,:,2))
 figure, imshow(I(:,:,:,3))

 onion = imread('onion.png'); onionArray =


repmat(onion, [ 1 1 1 4 ]);
 montage(onionArray);
 :‫ثم جرب‬
 montage(onionArray,'size',[1 NaN]);
68 18/02/2020

‫إضافة عنوان الصور في ماتالب‬


 caption = sprintf('Original Image #%d out of
%d', k, n);
 title(caption, 'FontSize', 14);

 subplot(1,3,1); imshow(rand(300)); mt(1) =


title('TEST1');
 subplot(1,3,2); imshow(rand(300)); mt(2) =
title('TEST2');
 subplot(1,3,3); imshow(rand(300)); mt(3) =
title('TEST3');
 set(mt,'Position',[150
300],'VerticalAlignment','top','Color',[1 0 0])
69 18/02/2020

histeq
 J = histeq(I,hgram)
 J = histeq(I,n)
 [J,T] = histeq(I)
 newmap = histeq(X,map)
 newmap = histeq(X,map,hgram)
 [newmap,T] = histeq(X,___)

 J = histeq(I,hgram) transforms the grayscale image I so that


the histogram of the output grayscale image J with
length(hgram) bins approximately matches the target
histogram hgram.
You optionally can perform histogram equalization of grayscale
images using a GPU (requires Parallel Computing Toolbox™).
 J = histeq(I,n) transforms the grayscale image I, returning in J
an grayscale image with n discrete gray levels. A roughly
equal number of pixels is mapped to each of the n levels in J,
so that the histogram of J is approximately flat. The histogram
of J is flatter when n is much smaller than the number of
discrete levels in I.
70 18/02/2020

histeq
 [J,T] = histeq(I) returns the grayscale transformation T that
maps gray levels in the image I to gray levels in J.
 newmap = histeq(X,map) transforms the values in the
colormap so that the histogram of the gray component of
the indexed image X is approximately flat. It returns the
transformed colormap in newmap.
 This syntax is not supported on a GPU.
 newmap = histeq(X,map,hgram) transforms the colormap
associated with the indexed image X so that the histogram
of the gray component of the indexed image (X,newmap)
approximately matches the target histogram hgram. The
histeq function returns the transformed colormap in
newmap. length(hgram) must be the same as size(map,1).
 This syntax is not supported on a GPU.
 [newmap,T] = histeq(X,___) returns the grayscale
transformation T that maps the gray component of map to
the gray component of newmap.
 This syntax is not supported on a GPU.
71 18/02/2020

histeq
I = imread('tire.tif');
 Enhance the contrast of an intensity image using
histogram equalization.
 J = histeq(I);
 Display the original image and the adjusted
image.
 imshowpair(I,J,'montage') axis off
72 18/02/2020

imadjust
J = imadjust(I)
 J = imadjust(I,[low_in high_in])
 J = imadjust(I,[low_in high_in],[low_out high_out])
 J = imadjust(I,[low_in high_in],[low_out
high_out],gamma)
 J = imadjust(RGB,[low_in high_in],___)
 newmap = imadjust(cmap,[low_in high_in],___)
73 18/02/2020

imadjust
I = imread('pout.tif');
 imshow(I)
 Adjust the contrast of the image so that 1% of the
data is saturated at low and high intensities, and
display it.
 J = imadjust(I); figure imshow(J)
74 18/02/2020

You might also like