stop an infinite loop
I have been trying to trace the contour (line) of a surface in an 2D grayscale image using OpenCV and C++, but, I keep running into an infinite loop. I've tried all sorts of debugging, and cannot rectify my problem. Please help!
My image looks like this: http://snag.gy/fAs9a.jpg (fourth image)
This is what my "initial" contour looks like: http://snag.gy/fAs9a.jpg (fifth image)
This is what I have done so far:
Problem: I keep running into an infinite loop even though my logic seems sound and my code also looks reasonable. There might not be a pixel found in the current column that has its neighbors greater than a threshold.
Debugging: I debugged my code to determine where the infinite loop was happening and found out that it was in the if-else
code block under the while
loop in the main
function.
I have run out of ideas on how to remove my error. Again, help would be appreciated very much!
This is my code:
// 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;
}
}
}
main()
// ... previous code to do blurring of the image.
// code to get the point-coordinates from the "initial mask" image
std::vector<int> setC;
std::vector<int> setR;
for(int xc = 0; xc < input.cols; xc++) {
for(int xr = 0; xr < input.rows; xr++) {
int val = mask[xr*input.cols + xc];
if(val == 255) {
//setC[i] = xc;
//setR[i] = xr;
//i++;
setC.push_back(xc);
setR.push_back(xr);
}
}
}
// get the LBP pattern
// LBP PATTERN IS A 3X3 NEIGHBOURHOOD
std::vector<int>::iterator mc1 = setC.begin();
std::vector<int>::iterator mr1 = setR.begin();
int rr = 0;
int cc = 0;
int width = input.cols;
//rowcount = 0;
int infiniteCount = 0;
unsigned char *GMOtemp;
unsigned char *contour;
GMOtemp = (unsigned char*) malloc(sizeof(unsigned char) * input.rows * input.cols);
contour = (unsigned char*) malloc(sizeof(unsigned char) * input.rows * input.cols);
memcpy(GMOtemp, OCVout2.data, sizeof(unsigned char) * input.rows * input.cols);
memset(contour, 0, sizeof(unsigned char) * input.rows * input.cols);
while( (mc1 != setC.end()) && (mr1 != setR.end()) ) {
//looping:
rr = *mr1; // get row and col
cc = *mc1;
printf("n %d %d n", rr, cc);
rr++; // increment the row
bool result = checkLBP(GMOtemp, contour, rr, cc, width, sum);
//printf("n %d n", rr);
if(result == true)
{
contour[rr*width + cc] = 255; // set current point to be equal to 255
mc1++; // increment the vector iterators - so, go to next column
mr1++;
}
else {
do{
rr++;
result = checkLBP(GMOtemp, contour, rr, cc, width, sum);
infiniteCount++;
//printf("n rr %d n", rr);
//printf("n %d n", infiniteCount);
if (rr == (input.rows - 1) || infiniteCount == 200) {
contour[rr*width + cc] = 255;
mr1++;
mc1++;
infiniteCount = 0;
result = true;
break;
}
//printf("n rr %d n", rr);
} while(result == false);
}
You are trying to break out of the while( (mc1 != setC.end()) && (mr1 != setR.end()) ) {}
loop, am I right? If so, you have placed your break
in the wrong place.
The break
will only break out of your do{...}while(result==false)
loop and not your big while loop. A hint would be to use a FLAG. Do let me know if problem persists. Peace.
I solved my infinite loop by forcing the if..else
code block to iterate from the current row to the last row in a for
loop. This is the code:
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++;
}
}
}
链接地址: http://www.djcxy.com/p/89800.html
上一篇: 使用libvlc smem从视频获取帧并将其转换为opencv Mat。 (C ++)
下一篇: 停止无限循环