[問題] 遞迴與堆疊-Connected Component label
看板C_and_CPP (C/C++)作者MaconChou (得罪了方丈還想走)時間15年前 (2010/09/01 13:25)推噓3(3推 0噓 7→)留言10則, 4人參與討論串1/2 (看更多)
先進:
抱歉!小弟初學,如有不足或詞意不明請多多包涵。
遇到的問題:
使用Recursive,出現問題stack overflow -.-
希望得到的正確結果:
和本問題不相關的,想一起問先進的!!以下資訊是!?謝謝。
'VC08182010.exe': Loaded 'D:\Program\Vehicle_LicensePlate\VC08182010\Debug\VC08182010.exe', Symbols loaded.
'VC08182010.exe': Loaded 'C:\Windows\System32\ntdll.dll', Cannot find or open the PDB file
'VC08182010.exe': Loaded 'C:\Windows\System32\kernel32.dll', Cannot find or open the PDB file
'VC08182010.exe': Loaded 'C:\Windows\System32\KernelBase.dll', Cannot find or open the PDB file
'VC08182010.exe': Loaded 'C:\Windows\System32\msvcr100d.dll', Symbols loaded.
'VC08182010.exe': Loaded 'C:\Windows\System32\apphelp.dll', Cannot find or open the PDB file
以下是錯誤資訊!!
First-chance exception at 0x000425ea in VC08182010.exe: 0xC00000FD: Stack overflow.
Unhandled exception at 0x000425ea in VC08182010.exe: 0xC00000FD: Stack overflow.
The program '[1336] VC08182010.exe: Native' has exited with code -1073741571 (0xc00000fd).
程式說明:
此function為要在圖片上找尋是否有255的光點如有的話
,進行connected component label由1~n,圖片大小目前為640*480。
開發平台: (例: VC++ or gcc/g++ or Dev-C++, Windows or Linux)
win7 / VC++ 2010
有問題的code: (請善用置底文標色功能)
// The function is 8-neighborhood
#include <stdio.h>
#include <stdlib.h>
void FindPixels(unsigned char **, int, int, int *, int ,int);
int ConnectedComponents(unsigned char **Y_Difference, int Width, int Height)
{
int i, j;
int block = 1; // candidate number
char file_name_ConnectedComponents[] = "ConnectedComponents";
WriteXls(Y_Difference, file_name_ConnectedComponents, Width, Height);
for(i = 0; i <= Height - 1; i++)
for(j = 0 ; j <= Width - 1; j++){
if(255 == *(*(Y_Difference + i) + j)){
printf("\nCandidate %d", block);
FindPixels(Y_Difference, i, j, &block, Width, Height);
block++;
}
}
block--;
return block;
}
void FindPixels(unsigned char **Y_Difference, int i, int j, int *block, int Width, int Height)
{
*(*(Y_Difference + i) + j) = *block;
if(i - 1 >=0 && j - 1 >= 0 &&
255 == *(*(Y_Difference + i - 1) + j - 1))
FindPixels(Y_Difference, (i - 1), (j - 1), block, Width, Height);
if(i - 1 >=0 &&
255 == *(*(Y_Difference + i - 1) + j ))
FindPixels(Y_Difference, (i - 1), (j ), block, Width, Height);
if(i - 1 >= 0 && j + 1 <= Width - 1 &&
255 == *(*(Y_Difference + i - 1) + j + 1))
FindPixels(Y_Difference, (i - 1), (j + 1), block, Width, Height);
if(j - 1 >= 0 &&
255 == *(*(Y_Difference + i ) + j - 1))
FindPixels(Y_Difference, (i ), (j - 1), block, Width, Height);
if(j + 1 <= Width - 1 &&
255 == *(*(Y_Difference + i ) + j + 1))
FindPixels(Y_Difference, (i ), (j + 1), block, Width, Height);
if(i + 1 <= Height - 1 && j - 1 >= 0 &&
255 == *(*(Y_Difference + i + 1) + j - 1))
FindPixels(Y_Difference, (i + 1), (j - 1), block, Width, Height);
if(i + 1 <= Height - 1 &&
255 == *(*(Y_Difference + i + 1) + j ))
FindPixels(Y_Difference, (i + 1), (j ), block, Width, Height);
if(i + 1 <= Height - 1 && j + 1 <= Width - 1 &&
255 == *(*(Y_Difference + i + 1) + j + 1))
FindPixels(Y_Difference, (i + 1), (j + 1), block, Width, Height);
return;
}
/*
8-neighborhood Connected Components Label
pixel location is (x, y) for start, and other pixels of find pixels
(x - 1, y - 1) (x - 1, y ) (x - 1, y + 1)
(x , y - 1) (x , y ) (x , y + 1)
(x + 1, y - 1) (x + 1, y ) (x + 1, y + 1)
*/
補充說明:
紅色的字為遞迴。
1.是否是我的遞迴執行過多次!?程式錯誤在4377的遞迴,4377以下的遞迴皆可完成。
但是目前知其中一區塊需遞迴5003次還可以跑完。
2.有試著改爬文中先進所說的變更stack size還是一樣不行,想請問所改的/Fnumber
這number指的是byte還是!?可能是我試的不夠大嗎!?
3.若在X86上以如此,如移植到DSP晶片上是否應該更不盡理想吧!?
4.是否有撰寫遞迴時須注意的小地方!?
5.或許除了使用遞迴之外是否有比較理想的方式!?
先感謝,回應的先進。
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 210.240.245.74
※ 編輯: MaconChou 來自: 210.240.245.74 (09/01 13:27)
推
09/01 14:33, , 1F
09/01 14:33, 1F
推
09/01 20:24, , 2F
09/01 20:24, 2F
→
09/01 20:24, , 3F
09/01 20:24, 3F
→
09/01 20:27, , 4F
09/01 20:27, 4F
推
09/01 21:36, , 5F
09/01 21:36, 5F
→
09/01 21:37, , 6F
09/01 21:37, 6F
→
09/03 16:53, , 7F
09/03 16:53, 7F
→
09/03 16:54, , 8F
09/03 16:54, 8F
→
09/03 17:15, , 9F
09/03 17:15, 9F
→
09/03 17:15, , 10F
09/03 17:15, 10F
討論串 (同標題文章)
以下文章回應了本文:
完整討論串 (本文為第 1 之 2 篇):
C_and_CPP 近期熱門文章
PTT數位生活區 即時熱門文章