我一直在尝试使用OpenCV和C ++跟踪2D灰度图像中曲面的轮廓(线),但是,我一直在无限循环中运行。我已经尝试了各种调试,无法纠正我的问题。请帮忙!
我的图片如下所示:http://snag.gy/fAs9a.jpg(第四张图片)
这就是我的“初始”轮廓:http://snag.gy/fAs9a.jpg(第五张图片)
这是我到目前为止所做的:
问题:即使我的逻辑听起来合理且我的代码看起来也合理,我仍然会遇到无限循环。当前列中可能没有找到其邻居大于阈值的像素。
调试:我调试了我的代码以确定无限循环发生的位置,并发现它位于if-else
循环中的while
代码块中{1}}功能。
我已经没有关于如何删除错误的想法。再次,非常感谢帮助!
这是我的代码:
main
主要()
// function to get the neigbourhood pattern
bool checkLBP(unsigned char *GMOtemp, unsigned char *contour, int &row, int &col, int width, int thresh)
{
const int arrSize = 9;
const int semiArrSize = 6;
// for first column, need only 6 neighbours
// since it is at one end of the image
if( col == 0 ) {
int array [semiArrSize];
// start storing values in the array
array[0] = GMOtemp[(row*width) + (col)];
array[1] = GMOtemp[((row+1)*width) + (col)];
array[2] = GMOtemp[((row+2)*width) + (col)];
array[3] = GMOtemp[(row*width) + (col+1)];
array[4] = GMOtemp[((row+1)*width) + (col+1)];
array[5] = GMOtemp[((row+2)*width) + (col+1)];
int cnt = 0;
for(int i = 0; i<semiArrSize; i++) {
if(array[i] >= thresh) {
cnt++;
}
}
printf("\n cnt %d \n",cnt);
if( cnt >= 2 ) {
return true;
}
else {
return false;
}
}
// for last column, need only 6 neighbours
else if( col == width - 1) {
int array [semiArrSize];
// start storing values in the array
array[0] = GMOtemp[(row*width) + (col)];
array[1] = GMOtemp[((row+1)*width) + (col)];
array[2] = GMOtemp[((row+2)*width) + (col)];
array[3] = GMOtemp[(row*width) + (col-1)];
array[4] = GMOtemp[((row+1)*width) + (col-1)];
array[5] = GMOtemp[((row+2)*width) + (col-1)];
int cnt = 0;
for(int i = 0; i<semiArrSize; i++) {
if(array[i] >= thresh) {
cnt++;
}
}
printf("\n cnt %d \n",cnt);
if( cnt >= 2 ) {
return true;
}
else {
return false;
}
}
else {
int array [arrSize];
// start filling all the elements
array[0] = GMOtemp[((row)*width) +(col-1)];
array[1] = GMOtemp[((row)*width) +(col)];
array[2] = GMOtemp[((row)*width) +(col+1)];
array[3] = GMOtemp[((row+1)*width) +(col-1)];
array[4] = GMOtemp[((row+1)*width) +(col)];
array[5] = GMOtemp[((row+1)*width) +(col+1)];
array[6] = GMOtemp[((row+2)*width) +(col-1)];
array[7] = GMOtemp[((row+2)*width) +(col)];
array[8] = GMOtemp[((row+2)*width) +(col+1)];
int cnt = 0;
for(int i = 0; i<arrSize; i++) {
if(array[i] >= thresh) {
cnt++;
}
}
printf("\n cnt %d \n",cnt);
if( cnt >= 1) {
return true;
}
else {
return false;
}
}
}
答案 0 :(得分:0)
你正试图突破while( (mc1 != setC.end()) && (mr1 != setR.end()) ) {}
循环,我是对的吗?如果是这样,您已将break
放在错误的位置。
break
只会突破你的do{...}while(result==false)
循环,而不是你的大循环。提示是使用FLAG。如果问题仍然存在,请告诉我。和平。
答案 1 :(得分:0)
我通过强制if..else
代码块从for
循环中的当前行迭代到最后一行来解决我的无限循环。这是代码:
while( mm < ind ) {
// 'ind' corresponds to number of coordinate-sets = 254
// I used two arrays here, each was set to 256 (width of image).
int rr = setR[mm]; // get row and col
int cc = setC[mm];
//printf("\n %d %d \n", rr, cc);
rr = rr + skiprows; // increment row; to skip if needed (skiprows = 0 now)
bool result = checkLBP(GMOtemp, rr, cc, width, height, sum);
//printf("\n %d \n", rr);
if(result == true)
{
if(rr<height){
contour[rr*width + cc] = 255; // set current point to be equal to 255
mm++;
}
}
else {
int check = 0; // boolean check variable - to see if none of the neighbors of a given pixel are greater
rr = rr + skiprows; // increment row
for(int currRow = rr; currRow < height; currRow = currRow+skiprows)
{
result = checkLBP(GMOtemp, currRow, cc, width, height, sum);
if( result == true)
{
contour[currRow*width + cc] = 255;
mm++;
check = 1;
break; // break out of for-loop
}
}
if(check == 0)
{
mm++;
}
}
}