接觸數字圖像處理最早是在高中,那時候photoshop還是4.0,可能是因為先入為主的關系,到現在都沒有學3dmax之類的興趣,2d到3d的飛躍估計是沒我什么事了,舍不得那平方到立方的高薪....呵呵。
在上大學的時候,就和同學一起寫過一些圖像處理的程序,那個時候編程還很隨意,考慮的只是如何實現,現在看來真正的技術是把握全局的能力,而不是靈光一現的神奇。前些日子接觸了一些國外的圖像處理程序,在這里算是作個總結,估計以后不會再針對性的研究圖像處理方面的東西了。
以前的一個同學曾經跟我說過.net沒有指針,現在很多培訓課好像也是這么講的,其實這是一個謬誤。只是framework不推薦使用指針,尤其是在webservise,remoting等跨進程操作中,指針都是不安全的。但用過tc的各位都應該對指針的執行效率又深刻的印象,在批量運算大規模數據的需求下,指針是不二的選擇。因而.net聰明的保留的保留了指針,并將其列入不安全方法集中。合理的使用指針將大幅度提高執行效率,我曾做過試驗,對640*480的圖像進行逐點運算,非指針運算要執行數分鐘,而指針運算幾乎是瞬間完成的。所以不要害怕使用指針。
其次就是數學,奉勸大家一定要弄明白了再寫程序,數學課不是鬧著玩的......想不明白就要躺在床上反復的想,我總覺得數學能預防老年癡呆。
言歸正傳,說說程序結構吧 :
imaging項目(濾鏡,紋理,圖像模式)
math項目(算法,邊界,定制。及常用計算方法)
主程序項目
各舉個例子來說明,我也來一回面向接口編程 ,
public interface ifilter
{
bitmap apply( bitmap img );
}
舉例來說明,我也來一回面向接口編程 ,各濾鏡都要實現這個接口,接口定義還包括一個不生成實際圖像,只生成二進制對象的借口定義,在這里暫不作考慮。以取反色濾鏡為例
public bitmap apply( bitmap srcimg )
{
// get source image size
int width = srcimg.width;
int height = srcimg.height;
pixelformat fmt = ( srcimg.pixelformat == pixelformat.format8bppindexed ) ?
pixelformat.format8bppindexed : pixelformat.format24bpprgb;
// lock source bitmap data
bitmapdata srcdata = srcimg.lockbits(
new rectangle( 0, 0, width, height ),
imagelockmode.readonly, fmt );
// create new image
bitmap dstimg = ( fmt == pixelformat.format8bppindexed ) ?
aforge.imaging.image.creategrayscaleimage( width, height ) :
new bitmap( width, height, fmt );
// lock destination bitmap data
bitmapdata dstdata = dstimg.lockbits(
new rectangle( 0, 0, width, height ),
imagelockmode.readwrite, fmt );
// copy image
win32.memcpy( dstdata.scan0, srcdata.scan0, srcdata.stride * height );
// process the filter
processfilter( dstdata, fmt );
// unlock both images
dstimg.unlockbits( dstdata );
srcimg.unlockbits( srcdata );
return dstimg;
}
是該濾鏡方法的入口,完成了處理前的準備工作,processfilter同時調用每個濾鏡類中共有的processfilter方法,而這個processfilter就是實現功能的關鍵所在了逐點運算或模版運算。
// process the filter
private unsafe void processfilter( bitmapdata data, pixelformat fmt )
{
int width = data.width;
int height = data.height;
int linesize = width * ( ( fmt == pixelformat.format8bppindexed ) ? 1 : 3 );
int offset = data.stride - linesize;
// do the job
byte * ptr = (byte *) data.scan0.topointer( );
// invert
for ( int y = 0; y < height; y++ )
{
for ( int x = 0; x < linesize; x++, ptr ++ )
{
// ivert each pixel
*ptr = (byte)( 255 - *ptr );
}
ptr += offset;
}
}
其中format8bppindexed是不必太關心的,個人認為設計初期可以不用考慮兼容它的問題。
下面來說說紋理,這個以前考慮得還不太多,但發現老外很喜歡玩這個,因為紋理在數學方面發揮的空間更大,我也不知道他們是怎么想出來的,憑空想可能還真是有難度,可能是他們誰在玩數學建模軟件的時候發現這個玩法的,于是高數老師誰也不服誰,把算法玩的火火的。反正我覺得是這么回事。。。
public interface itexturegenerator
{
/**//// <summary>
/// generate texture
/// </summary>
float[,] generate( int width, int height );
/**//// <summary>
/// reset - regenerate internal random numbers
/// </summary>
void reset( );
}
這是紋理生成器的實現接口,為了保證每次的紋理不同,還要更新隨機數以作為計算參數
private math.perlinnoise noise = new math.perlinnoise( 1.0 / 32, 0.05, 0.5, 8 );
實現紋理細節還需要靠noise實現,因而需要實現許多種noise。
// constructors
public woodtexture( ) : this( 12.0 ) { }
public woodtexture( double rings )
{
this.rings = rings;
reset( );
}
構造函數提供了默認值的設置,也就是對單位紋理大小的限定。
// generate texture
public float[,] generate( int width, int height )
{
float[,] texture = new float[height, width];
int w2 = width / 2;
int h2 = height / 2;
for ( int y = 0; y < height; y++ )
{
for ( int x = 0; x < width; x++ )
{
double xv = (double) ( x - w2 ) / width;
double yv = (double) ( y - h2 ) / height;
texture[y, x] =
math.max( 0.0f, math.min( 1.0f, (float)
math.abs( math.sin(
( math.sqrt( xv * xv + yv * yv ) + noise.function2d( x + r, y + r ) )
* math.pi * 2 * rings
))
));
}
}
return texture;
}
這就是。。。我數學不好的下場。都不知道她在說什么呢,最小值中選出最大值。算法不難找,關鍵是要看結構如何將他們整合起來。
public void reset( )
{
r = rand.next( 5000 );
}別忘了這個隨機數,數字的圖像也需要自然的美。
math工程中面向對象的觀念不它容易得到貫徹,看一看那個perlinnoise吧,拋磚引玉。
public perlinnoise( double initfrequency, double initamplitude, double persistance, int octaves )
{
this.initfrequency = initfrequency;
this.initamplitude = initamplitude;
this.persistance = persistance;
this.octaves = octaves;
}
首先要收集數據,因為圖像處理要涉及到一維和二維兩種情況,因而像noise這種底層方法要分別對應著兩種情況給出對應的方法。
/**//// <summary>
/// 1-d perlin noise function
/// </summary>
public double function( double x )
{
double frequency = initfrequency;
double amplitude = initamplitude;
double sum = 0;
// octaves
for ( int i = 0; i < octaves; i++ )
{
sum += smoothednoise( x * frequency ) * amplitude;
frequency *= 2;
amplitude *= persistance;
}
return sum;
}
/**//// <summary>
/// 2-d perlin noise function
/// </summary>
public double function2d( double x, double y )
{
double frequency = initfrequency;
double amplitude = initamplitude;
double sum = 0;
// octaves
for ( int i = 0; i < octaves; i++ )
{
sum += smoothednoise( x * frequency, y * frequency ) * amplitude;
frequency *= 2;
amplitude *= persistance;
}
return sum;
}
一維跟二維的區別是什么,上中學的時候知道了線的運動生成了面,上大學又知道了循環著變化著的線能代表面,但如果做過了邊緣識別和銳化以后話,又發現以前小看線了,其實它只是比面少一個參數而已。
/**//// <summary>
/// ordinary noise function
/// </summary>
protected double noise( int x )
{
int n = ( x << 13 ) ^ x;
return ( 1.0 - ( ( n * ( n * n * 15731 + 789221 ) + 1376312589 ) & 0x7fffffff ) / 1073741824.0 );
}
protected double noise( int x, int y )
{
int n = x + y * 57;
n = ( n << 13 ) ^ n ;
return ( 1.0 - ( ( n * ( n * n * 15731 + 789221 ) + 1376312589 ) & 0x7fffffff ) / 1073741824.0 );
}又一次證明了前面那段話,個人感覺這個x+y*57有點投影的意思。獲取相應的噪點值。但噪點不是直接就能拿來用的
/**//// <summary>
/// smoothed noise
/// </summary>
protected double smoothednoise( double x )
{
int xint = (int) x;
double xfrac = x - xint;
return cosineinterpolate( noise( xint ) , noise( xint + 1 ), xfrac );
}
protected double smoothednoise( double x, double y )
{
int xint = (int) x;
int yint = (int) y;
double xfrac = x - xint;
double yfrac = y - yint;
// get four noise values
double x0y0 = noise( xint , yint );
double x1y0 = noise( xint + 1, yint );
double x0y1 = noise( xint , yint + 1 );
double x1y1 = noise( xint + 1, yint + 1) ;
// x interpolation
double v1 = cosineinterpolate( x0y0, x1y0, xfrac );
double v2 = cosineinterpolate( x0y1, x1y1, xfrac );
// y interpolation
return cosineinterpolate( v1, v2, yfrac );
}平滑的噪點,這個稱呼似乎有點不協調,通過余弦插值,而不是離散余弦來運算。什么是余弦插值呢? /**//// <summary>
/// cosine interpolation
/// </summary>
protected double cosineinterpolate( double x1, double x2, double a )
{
double f = ( 1 - math.cos( a * math.pi ) ) * 0.5;
return x1 * ( 1 - f ) + x2 * f;
}就是這個,有些事情,大師知道就夠了,你就照著去做就行了,為什么?因為你可能一輩子也不明白,自然有人會去弄明白的,知識還在傳承。就像你不必知道自己的胃酸比例,也可以放心的吃香喝辣一樣,也不必擔心子孫后代消化不良。有些事情不必強求,有點消極了,呵呵。
畫面并不難,只要把握好調用關系就可以了,另外像photoshop那樣的懸浮窗體是最佳的選擇我認為, // invert image
private void invertcolorfiltersitem_click(object sender, system.eventargs e)
{
applyfilter(new invert());
}
// apply filter on the image
private void applyfilter(ifilter filter)
{
try
{
// set wait cursor
this.cursor = cursors.waitcursor;
// apply filter to the image
bitmap newimage = filter.apply(image);
if (host.createnewdocumentonchange)
{
// open new image in new document
host.newdocument(newimage);
}
else
{
if (host.rememberonchange)
{
// backup current image
if (backup != null)
backup.dispose();
backup = image;
}
else
{
// release current image
image.dispose();
}
image = newimage;
// update
updatenewimage();
}
}
catch (argumentexception)
{
messagebox.show("selected filter can not be applied to the image", "error", messageboxbuttons.ok, messageboxicon.error);
}
finally
{
// restore cursor
this.cursor = cursors.default;
}
}調用順暢的話,多少代碼都不會覺得亂,對于初學者來說,要善用region。
這里還有個documentshost的概念,用它來承載圖像文件,并將圖像和窗體連接起來,很方便 /**//// <summary>
/// idocumentshost interface
/// provides connectione between documents and the main widnow
/// </summary>
public interface idocumentshost
{
bool createnewdocumentonchange{get;}
bool rememberonchange{get;}
bool newdocument(bitmap image);
bool newdocument(compleximage image);
bitmap getimage(object sender, string text, size size, pixelformat format);
}
歡迎大家跟我討論
新聞熱點
疑難解答
圖片精選