در این مثال ابتدا تصویر RGB را با رنگ اصلی پر می کنیم.بنابرین مجموع ارزش پیکسل هارو پیدا می کنیم.اگر مجموع ارزش بیشتر از صفر باشد رنگ سفید است اگر صفر باشد سیاه است .
در مثال دوم به تبدیل تصویر RGB به باینری نیازمندیم .
1-تبدیل تصویر RGB به تصویرخاکستری(gary scale)
2-مقدار حد آستانه را پیدا کنیم .اگر مجموع پیگسل ها از حد آستانه(threshold) بیشتر بود سفید د غیر این صورت سیاه خواهد بود
the threshold value then the value will be 1(white) else zero (black).
function mybinary
global GIm T1;
A=imread(‘shapes.bmp’);
figure,imshow(A);
title(‘Original image’);
B=zeros(size(A,1),size(A,2));
for l=1:size(A,1)
for m=1:size(A,2)
if(sum(A(l,m,:))>0)
B(l,m)=1;
end
end
end
B=logical(B);
figure,imshow(B);
Im=imread(‘gantrycrane.png’);
Im=imread(‘gantrycrane.png’);
figure,imshow(Im);
title(‘Original Image’);
%0.2989 * R + 0.5870 * G + 0.1140 * B
GIm=uint8(zeros(size(Im,1),size(Im,2)));
for m=1:size(Im,1)
for n=1:size(Im,2)
GIm(m,n)=0.2989*Im(m,n,1)+0.5870*Im(m,n,2)+0.1140*Im(m,n,3);
end
end
ما بدون استفاده از حلقه LOOP نیز می توانیم به تصویر garyscale تبدیل کنیم
=
ssz = get(0,’ScreenSize’);
T.fg=figure(‘Visible’,’on’,’Name’,’IMAGE THRESHOLDING’,’NumberTitle’,’off’,’Position’, ssz);
T.holder=axes(‘units’,’pixels’,’Position’,[ssz(3)/35 ssz(4)/4 ssz(3)-(ssz(3)/3) ssz(4)-(ssz(4)/3)]);
imshow(GIm);
set(T.holder,’xtick’,[],’ytick’,[])
T.slid=uicontrol(‘Style’,’Slider’,’Visible’,’on’,’Value’,1,’Max’,255,’Min’,0,’Sliderstep’,[1 1],’Position’,[ssz(3)/35 ssz(4)/5 ssz(3)-(ssz(3)/3) 20],’Callback’, @tresher);
T.ent=uicontrol(‘Style’,’pushbutton’,’Visible’,’on’,’String’,’THRESHOLD VALUE’,’Position’,[ssz(3)-(ssz(3)/4) ssz(4)-(ssz(4)/8) 105 30]);
T.ed=uicontrol(‘Style’,’edit’,’Visible’,’on’,’String’,’0′,’Value’,1,’Position’,[ssz(3)-(ssz(3)/4) ssz(4)-(ssz(4)/6) 90 20]);
function tresher(object,~)
val=get(object,’value’);
in=GIm;
T1=Imthreshold1(in,val);
T.view1=imshow(T1);
set(T.holder,’xtick’,[],’ytick’,[])
set(T.ed,’String’,val);
end
function Im=Imthreshold1(Image,Tvalue)
sz=size(Image);
mybin=zeros(size(Image));
for i=1:sz(1)
for j=1:sz(2)
if(Image(i,j)>Tvalue)
mybin(i,j)=1;
end
end
end
بجای حلقه loop میتوان از کد زیر نیز ااستفاده کرد
%mybin(find(Image>Tvalue))=1;
Explanation:
The output of find(Image>Tvalue) will be the values that are greater than Tvalue.
For instance,
consider a matrix,
>> A=[1,2,3,4;2,4,6,8;3,6,9,12];
>> A
A =
1 2 3 4
2 4 6 8
3 6 9 12
>> find(mod(A,2)==0)
ans =
2
4
5
6
8
10
11
12
Im=logical(mybin);
end
end
پاسخ دادن