Matlab ile görüntü işleme yardım..[çoğunu bitirdim ]

Başlatan KaZAaRa, 06 Mayıs 2008, 21:48:26

KaZAaRa

Arkadaşlar selamlar..

Oradan buradan yardım alarak bitirme tezim olan " matlab görüntü işleme gereçlerini kullanarak kontür belirleme" konusunun sonuna yaklaşmak üzereyim..

bir şeklin dış çizgisini belirleyip onun datalarını elde etmek istiyorum..

ben bunu yaptım.. dış çizgisini belirledim ve piksel ve piksellerin yönleri olmak üzere dış çizgiyi belirledim..

bundan sonra yapmak istediğim şu:

belirlediğim yönler 8 li yön kuralına göre..

yani böyle


ama ben bunu 4 yöne indirmek istiyorum. 0,1,2,3 olarak..

yani 8li yöne göre

0=0
1=0+1
2=1
3=1+2
4=2
5=2+3
6=3
7=3+0

şeklinde..

Bunu programdaki yönleri kullanarak filan yapılabilir ama yapamadım bi türlü :):)

benden daha iyi bilen biri bunu kolaylıkla yapabilir.. yada bana yardımcı olaiblir..

Konuya ait m kod larını buradan indirebilirsiniz..

SpeedyX

Ufak bir düzeltme;
i=imread('c:\ikili.jpg'); %resmin bulundugu dizin..
kısmındaki resmi vermemişsiniz,
Resim şu olacaktı;


Yerine koyma yöntemiyle yapabilirsiniz!

KaZAaRa

dogru o bolumu duzeltmeden vermisim..

resim olarak onu değilde deve yi kullanabiiriz..

kod da ikili.jpg yerine deve.jpg yazıp..

çünkü sorunum resim değil şimdilik.. resimle alakam kalmadı.. resimden aldığım kontürün piksel yönleri ile alakam var şuan..


programı tamamen işlettikten sonra cDirection yazan bi bilgi açılıyor workspace de.. bu yönleri ifade ediyor.. ilk mesajda yazdığım gibi..

ama işte 8 li .. ben onu 4 lüye indirgemek istiyorum..

yardım bekliyorum..

picusta

kontur.m dosyasinda döngünün içinde degistirecegin yerler bariz belli.
Konu hakkinda kaynak gösterirsen (web sitesi linki) daha makbule geçer (konuyu merak eden arkadaslarin anlamasi bakimindan).

KaZAaRa

Konuyu açıklayıcı olarak buraya yazayım o zaman.. :):)

işleyeceğimiz resim bu:


Bu kod ile isleme yapiyoruz..

%% Resmi Yukleme
clear all;
close all;

i=imread('c:\deve.jpg'); %resmin bulundugu dizin..
i=im2bw(i,graythresh(i));
i=bwareaopen(i,50);
originalImage = double(i); 

%-------------------------------------------------------------------------
% Resim Isleme Baslangici
%% Resim Boyut Duzeltme
originalImage = imresize(originalImage,[320 240],'bilinear');

%% Objenin konturunun bulunmasi
originalImage = edge(originalImage,'prewitt');


%% cizim icin Arkaplani beyaz siniri siyah renge cevirmek
[x,y,pixelValues]=find(originalImage);
contourImage(1:size(originalImage,1),1:size(originalImage,2)) = 255;

for i=1:size(x)
    contourImage(x(i),y(i)) = 0;
end
figure,imshow(contourImage),title('White background and Black Boundary')

[xx,yy,pp] = find(originalImage);
%-------------------------------------------------------------------------
% Resim Isleme Sonu

%% Konturun herhangi bir noktasini baslangic noktasi kabul etmek
r = xx(40); c = yy(40);
%% Konturu kusursuz hale getirmek icin bu kullanilir 
for i=1:size(originalImage,1)
    if originalImage(i,round(size(originalImage,2)/2))== 1
        r = i;
        c = round(size(originalImage,2)/2);
        break;
    end
end

%% Konturun piksel ve piksel yonlerini belirlemek
[cpixels, cDirection] = kontur(contourImage,r,c,xx,yy);


Bu kod ile aldigimiz kontur bu şekilde ..


ve elde ettigimiz bilgiler su sekilde...

kontürün cizili olduğu piksel degerleri:


ve ilk mesajdaki yon tabirine gore yonler :


Ve en önemli yeri.. ana fonksiyonumuz kontur.m :

function [sp_x,sp_dir] = kontur(BW,row,col,xx1,yy1)


% Syntax : 
% cpixels = kontur(BW,r,c,xx1,yy1)
%
%   Girisler :
%   BW - Siyah & Beyaz Resim (Binary Image).
%   row,col -   Kontur uzerindeki tek pikselin baslangic noktasindan itibaren 
%               satir sutun koordinat degerleri, 
%   xx1,yy1 -   Bu komutla belirlenen tum sinir noktalarin listesi:
%               [xx1,yy1,p] = find(originalImage);
%
%
%   Cikislar : 
%   sp_x  -     Kontur koordinatlarini tutar,
%               Kolon 1 X koordinatini & Kolon 2 ise Y Koordinatlarini verir.
%   sp_dir -    Konturde bulunan her pikselin yon bilgilerini tutar
% 
%   Yonler ise su sekilde sembolize edilmistir:
%   0 - Sag
%   1 - Sag ve Yukari
%   2 - Yukari
%   3 - Yukari ve Sol
%   4 - Sol
%   5 - Sol ve Asagi
%   6 - Asagi
%   7 - Asagi ve Sag
%   9 - Bilimeyen Yon



xx = xx1; yy = yy1;
k=1;loop=0;
C(1:size(BW,1),1:size(BW,2))=255;      
sp_x(1,1)=row; sp_x(1,2)=col;
C(row,col) = 0;

while(1)
    xxTemp = xx; yyTemp = yy;
    prev_size=size(sp_x,1);

    % check the central pixel
    if BW(sp_x(k,1),sp_x(k,2)) < 255
        index = 0;
        % extract the coordinates of active neighbours in 3x3 + a padded
        % area
        if BW(sp_x(k,1),sp_x(k,2)-1) < 255
            index = index + 1;
            neighbours(index,1) = sp_x(k,1);  neighbours(index,2) = sp_x(k,2)-1; %0,-1
            direction(index,:) = '4';%'UU';
        end

        if BW(sp_x(k,1),sp_x(k,2)+1) < 255
            index = index + 1;
            neighbours(index,1) = sp_x(k,1);  neighbours(index,2) = sp_x(k,2)+1; %0,+1
            direction(index,:) = '0';%'DD';            
        end

        if BW(sp_x(k,1)-1,sp_x(k,2)) < 255
            index = index + 1;
            neighbours(index,1) = sp_x(k,1)-1;  neighbours(index,2) = sp_x(k,2); %-1,0
            direction(index,:) = '2';%'LL';            
        end

        if BW(sp_x(k,1)+1,sp_x(k,2)) < 255
            index = index + 1;
            neighbours(index,1) = sp_x(k,1)+1;  neighbours(index,2) = sp_x(k,2); %+1,0
            direction(index,:) = '6';%'RR';            
        end

        if BW(sp_x(k,1)-1,sp_x(k,2)-1) < 255
            index = index + 1;
            neighbours(index,1) = sp_x(k,1)-1;  neighbours(index,2) = sp_x(k,2)-1; %-1,-1
            direction(index,:) = '3';            
        end

        if BW(sp_x(k,1)-1,sp_x(k,2)+1) < 255
            index = index + 1;
            neighbours(index,1) = sp_x(k,1)-1;  neighbours(index,2) = sp_x(k,2)+1; %-1,+1
            direction(index,:) = '1';            
        end

        if BW(sp_x(k,1)+1,sp_x(k,2)-1) < 255
            index = index + 1;
            neighbours(index,1) = sp_x(k,1)+1;  neighbours(index,2) = sp_x(k,2)-1; %+1,-1
            direction(index,:) = '5';            
        end
        
        if BW(sp_x(k,1)+1,sp_x(k,2)+1) < 255
            index = index + 1;
            neighbours(index,1) = sp_x(k,1)+1;  neighbours(index,2) = sp_x(k,2)+1; %+1,+1        
            direction(index,:) = '7';            
        end
        
        %neighbours that are two pixels away but have priority
        if BW(sp_x(k,1),sp_x(k,2)-2) < 255
            index = index + 1;
            neighbours(index,1) = sp_x(k,1);  neighbours(index,2) = sp_x(k,2)-2; %0,-2    
            direction(index,:) = '4';            
        end

        if BW(sp_x(k,1),sp_x(k,2)+2) < 255
            index = index + 1;
            neighbours(index,1) = sp_x(k,1);  neighbours(index,2) = sp_x(k,2)+2; %0,+2        
            direction(index,:) = '0';            
        end
        
        if BW(sp_x(k,1)-2,sp_x(k,2)) < 255
            index = index + 1;
            neighbours(index,1) = sp_x(k,1)-2;  neighbours(index,2) = sp_x(k,2); %-2,0        
            direction(index,:) = '2';            
        end
        
        if BW(sp_x(k,1)+2,sp_x(k,2)) < 255
            index = index + 1;
            neighbours(index,1) = sp_x(k,1)+2;  neighbours(index,2) = sp_x(k,2); %+2,0        
            direction(index,:) = '6';            
        end

        if BW(sp_x(k,1)-2,sp_x(k,2)-1) < 255
            index = index + 1;
            neighbours(index,1) = sp_x(k,1)-2;  neighbours(index,2) = sp_x(k,2)-1; %-2,-1        
            direction(index,:) = '3';            
        end

        if BW(sp_x(k,1)-2,sp_x(k,2)+1) < 255
            index = index + 1;
            neighbours(index,1) = sp_x(k,1)-2;  neighbours(index,2) = sp_x(k,2)+1; %-2,+1        
            direction(index,:) = '1';            
        end
        
        if BW(sp_x(k,1)+2,sp_x(k,2)-1) < 255
            index = index + 1;
            neighbours(index,1) = sp_x(k,1)+2;  neighbours(index,2) = sp_x(k,2)-1; %+2,-1        
            direction(index,:) = '5';            
        end

        if BW(sp_x(k,1)+2,sp_x(k,2)+1) < 255
            index = index + 1;
            neighbours(index,1) = sp_x(k,1)+2;  neighbours(index,2) = sp_x(k,2)+1; %+2,+1        
            direction(index,:) = '7';            
        end
        
        % add the coordinates which are unique in priority order
        for i=1:index
            if (C(neighbours(i,1),neighbours(i,2))~=0)
                sp_x(k+1,1) = neighbours(i,1);
                sp_x(k+1,2) = neighbours(i,2);
                sp_dir(k+1,:) = direction(i,:);
                k = k+1;
                C(neighbours(i,1),neighbours(i,2)) = 0;
                break; 
            end
        end
    
    end

    curr_size=size(sp_x,1);
    if curr_size == prev_size
          %-----------------------
          % find unique points
            index = 1; newX = 0; newY = 0;
            for i=1:size(sp_x,1)
                pX = sp_x(i,1); pY = sp_x(i,2); dir = sp_dir(i,:);
                count = 0;
                for j=1:size(sp_x,1)
                    if pX == sp_x(j,1) && pY == sp_x(j,2)
                        count = count + 1;
                        if count > 1
                           sp_x(j,1) = -1; sp_x(j,2) = -1;   
                        end
                    end
                end
                if pX ~= -1 && pY ~= -1
                    newX(index) = pX; newY(index) = pY;
                    newDir(index,:) = dir; 
                    index = index + 1;
                end    
            end
            clear sp_x; clear sp_dir;
            sp_x(:,1) = newX'; sp_x(:,2) = newY'; sp_dir = newDir;
            curr_size=size(sp_x,1); k = size(sp_x,1);
            %------------------------       
          
          if (curr_size + 0) < size(xx,1)            
              % retry to get the boundary
              for i=1:size(xx,1)
                  cX = xxTemp(i);  cY = yyTemp(i);
                  for j=1:size(sp_x,1)
                       if cX == sp_x(j,1) && cY == sp_x(j,2) 
                           xxTemp(i) = 1000; yyTemp(i) = 1000;
                           break;
                       end
                  end
              end
              
              distX = abs(xxTemp - sp_x(k,1));
              distY = abs(yyTemp - sp_x(k,2));
              tDist = abs(distX + distY);           
              minT = tDist(1);
              rIndex = 1;
              for i=1:size(distX,1)
                  if minT > tDist(i)
                        rIndex = i;
                        minT = tDist(i);
                  end          
              end
              sp_x(k+1,1) = xxTemp(rIndex);
              sp_x(k+1,2) = yyTemp(rIndex);
              sp_dir(k+1,:) = '9';
              k = k+1;             
          else
            break;
        end
    end
    
    neighbours = 0;
end



Gösterdiğim piksel ve yönler tamamen kontur.m in içinde belirleniyor. Tabi bu kontur.m dosyasında 8 yönlü olarak. Benim istediğim bunu 4 yöne indirgemek.. sonrada 2 yöne indireceğim..

Düzenleme yapılacak yerler görülebiliyor. ama basıl bi düzenleme yapacağım bilemedim.. bikaç deneme yaptım ama yemedi.. :evil:

picusta

Madem sadece 2 yön ile ilgileniyorsun, o zaman Prewitt filtresine gerek yok. Sobel filtresi yeter.
Kaynakça göstermedin hala, bekliyoruz.

KaZAaRa

kaynak olarak yararlandığım yer mathworks sitesindeki user community bölümü..

orada paylaşılan m kodları..

http://www.mathworks.com/matlabcentral/



sobel filtresi ile nasıl yapılabilir..

bildiğiniz veya yazdığınız bir kod var mı?

yardımda bulunabilir misiniz ?