2013-06-04 172 views
-2

我目前正在閱讀17張圖片(24位,1200 x 1600)。讀17張圖像花了我大約0.078秒但是,我想將這個5760000大小的內存塊轉換爲192000的黑白圖像大小來做我的laplacian edge_detection。現在,我使用下面的方法:提高程序速度:矢量速度,內存塊速度

images.resize(rows * cols); 
images.reserve(rows * cols); 

for(int z = 0; z < rows * cols; z ++){ 
    pix.blue = (int) *(pChar + z * 3); 
    pix.green = (int) *(pChar + z * 3 + 1); 
    pix.red = (int) *(pChar + z * 3 + 2); 
    pix.black_white = pix.blue * .11 + pix.green * .59 + pix.red *.3; 
    images.at(z).black_white = pix.blue * .11 + pix.green * .59 + pix.red *.3; 
} 

然而,這讀取PCHAR內存塊和寫入的192萬矢量大小花費我的2.262秒總時間,讀取17倍的圖像處理。有沒有更快的方法可以處理這個問題?

我曾嘗試使用以下不同的代碼嘗試,但在pChar2不斷告訴我,它在調試模式VS2010一個badptr:(data_grey,PCHAR,pChar2變量是一個無符號的char *)

pChar = (unsigned char*) malloc (sizeof(char)*3*rows*cols); 
pChar2 = (unsigned char*) malloc (sizeof(char) * rows * cols); 
fread(pChar, sizeof(char), 3*rows*cols, bmpInput); 
images.at(i).data = pChar; 

for(int z = 0; z < rows * cols; z ++){ 
    pix.blue = (int) *(pChar + z * 3); 
    pix.green = (int) *(pChar + z * 3 + 1); 
    pix.red = (int) *(pChar + z * 3 + 2); 
    pix.black_white = pix.blue * .11 + pix.green * .59 + pix.red *.3; 
    pChar2 += (unsigned char) pix.black_white; 
} 
    images.at(i).data_grey = pChar2; 

我的想法是,我可能以不正確的方式寫入pChar2內存塊。但這第二種方法要快得多,所以我想知道我應該如何解決它。如果我爲images.at(i).data_grey獲得了一塊黑白內存塊,那將是理想的選擇。我主要是想這樣做,因爲它比矢量快得多,但是在基於矢量的代碼中,我做了一些錯誤,使得它比較慢? (我個人覺得載體更容易使用,但如果我需要的速度實在太差了,所以我會處理與存儲塊的工作,如果它應該是更快)

回答

0

我認爲你需要做的是改變

pChar2 += (unsigned char) pix.black_white; 

pChar2[z] = (unsigned char) pix.black_white; 

假設你要什麼,我認爲你正在試圖做的(值分配給內存塊被指向pChar2,然後將指針移動到下一個8位內存塊?)

+0

是的,我的意圖是。 Thxs – user2427671

0

請勿使用邊界檢查元素存取器at()。這將每次檢查您的索引,因爲如果使用超出範圍的索引,它必須拋出異常。這絕不應該發生,因爲您最初調整了矢量的大小。

因此,您應該使用無邊界檢查運算符[]

您也可以像C數組一樣直接寫入矢量。一些純粹主義者可能會爲此感到不安,但我認爲可以用矢量做:

images.resize(rows*cols); 
unsigned char *bwPixel = &images[0]; 

for(...) 
{ 
    // ... 
    *bwPixel++ = (unsigned char) (pix.blue * .11 + pix.green * .59 + pix.red *.3); 
} 
+0

thxs的想法不好改變它,看看它如何改變性能 – user2427671