2011-01-26 53 views
1

如何在iPhone應用程序中實現Matlab算法,以在Objective-C中縮略/縮小二元(黑色 和白色)圖像?我將如何在iPhone上實現Matlab鏤空/細化算法?

+0

將是巨大的,如果你能告訴我們Matlab的算法是如何工作的?我剛剛發現bwmorph(),它應該做的一樣我下面提供的方式。爲操作員實現8個小3x3矩陣,將它們硬編碼到您的例程中,並將它們「滑動」到圖像的每個像素(x和y方向上的兩個循環)。在每個像素上,您可以使用運算符來查看是否可以通過比較圖像的相應像素和運算符來侵蝕某些東西。如果你在周圍放置一段時間(真正的)循環,然後遍歷像素並侵蝕和侵蝕,直到你剩下骨架。 – evident 2011-01-26 23:55:00

回答

3

嗯,基本上你可以使用morpholocial運營商爲這個...

建設8條命中或錯過運營商這樣的:

 0 0 0 
St1 = x 1 x (for deleting upper pixels) 
     1 1 1 

旋轉此4次得到它的4個側面。然後還建立4個OFR這樣的角落:

 0 0 x 
St5 = 0 1 1 (rotate this again 4 times for the 4 corners) 
     x 1 1 

然後你蠶食你的圖像(循環),直到沒有運營商的可再使用......剩下的就是這種形象的骨架.. 。

這應該不是太難的目標CI猜測來實現......(不熟悉)......這是一個總體戰略...

希望幫助...如果不是,不斷詢問... ;-)

+0

你是什麼意思,腐蝕你的圖像與循環? – vini 2012-01-28 08:52:20

+1

帶循環我的意思是說,你只是將每個操作符一次又一次地疊加在圖像上......這將會阻止外部的像素。如果你這樣做,你可以保持一個小的骨架線...侵蝕itsel也描述在這裏:http://homepages.inf.ed.ac.uk/rbf/HIPR2/erode.htm 也許這有助於... – evident 2012-02-01 17:54:21

0

編輯:

我寫過GLSL片段着色器,它可以在圖像上執行快速構圖。你可以在循環中應用這個着色器,直到你得到你需要的。 GLSL着色器代碼:

uniform sampler2D Texture0; 
varying vec2 texCoord; 

// 3x3 pixel window 
// (-1,+1) (0,+1) (+1,+1) 
// (-1,0) (0,0) (+1,0) 
// (-1,-1) (0,-1) (+1,-1) 

float dtex = 1.0/float(textureSize(Texture0,0)); 

vec4 pixel(int dx, int dy) { 
    return texture2D(Texture0,texCoord + 
        vec2(float(dx)*dtex, float(dy)*dtex)); 
} 

int exists(int dx, int dy) { 
    return int(pixel(dx,dy).r < 0.5); 
} 

int neighbors() { 
    return exists(-1,+1) + 
      exists(0,+1) + 
      exists(+1,+1) + 
      exists(-1,0) + 
      exists(+1,0) + 
      exists(-1,-1) + 
      exists(0,-1) + 
      exists(+1,-1); 
} 

int transitions() { 
    return int(
      clamp(float(exists(-1,+1))-float(exists(0,+1)),0.,1.) + // (-1,+1) -> (0,+1) 
      clamp(float(exists(0,+1))-float(exists(+1,+1)),0.,1.) + // (0,+1) -> (+1,+1) 
      clamp(float(exists(+1,+1))-float(exists(+1,0)),0.,1.) + // (+1,+1) -> (+1,0) 
      clamp(float(exists(+1,0))-float(exists(+1,-1)),0.,1.) + // (+1,0) -> (+1,-1) 
      clamp(float(exists(+1,-1))-float(exists(0,-1)),0.,1.) + // (+1,-1) -> (0,-1) 
      clamp(float(exists(0,-1))-float(exists(-1,-1)),0.,1.) + // (0,-1) -> (-1,-1) 
      clamp(float(exists(-1,-1))-float(exists(-1,0)),0.,1.) + // (-1,-1) -> (-1,0) 
      clamp(float(exists(-1,0))-float(exists(-1,+1)),0.,1.) // (-1,0) -> (-1,+1)   
     ); 
} 

int MarkedForRemoval() { 
    int neib = neighbors(); 
    int tran = transitions(); 

    if (exists(0,0)==0 // do not remove if already white 
     || neib==0  // do not remove an isolated point 
     || neib==1  // do not remove tip of a line 
     || neib==7  // do not remove located in concavity 
     || neib==8  // do not remove not a boundary point 
     || tran>=2  // do not remove on a bridge connecting two or more edge pieces 
     ) 
     return 0; 
    else 
     return 1; 
} 

void main(void) 
{ 
    int remove = MarkedForRemoval(); 
    vec4 curr = texture2D(Texture0,texCoord); 
    vec4 col = vec4(remove,remove,remove,1.0); 
    gl_FragColor = (remove==1)? col:((curr.r > 0.05)? 
            vec4(1.0,1.0,1.0,1.0):curr); 
} 

只是這一次的代碼是基於this lecture(實際上是在演講的第一部分,這樣的算法有一些錯誤:-)) 見當黑猩猩差不斷受到這種GLSL着色器送入發生了什麼:

迭代0 enter image description here

迭代5 enter image description here

ITE比10 enter image description here

迭代15 enter image description here