My code is using OPENCV for image processing and I want to randomly create a feature set of points which are white. The program results in Assertion failed error when running the inner WHILE LOOP SECOND TIME. I have commented and tested the program to find exactly where is the problem. And i have found the problem with image.at(b,a) part of the code. It fails to run the second time. Although it runs perfectly fine the first time.
If i run the code with constant value instead of Variables b,a in the inner while loop, or with (j,j) variables it runs. I have used fout to print some debug statements. I am also attaching the .txt file output to help you see what's happening.
START OF THE OUTPUT FROM TXT FILE
start of while loop
inside the while loop
outside the while loop
[255, 255, 255]404 1173 start of the while loop
inside the while loop
END OF THE OUTPUT FROM TXT FILE
Pretty strange.Please help me figure this out!! I am pasting my code:
int main() {
int i = 0;
int a;
ofstream fout("test.txt");
int b;
int j = 0;
cv::Mat img;
cv::Scalar* scalar_low;
cv::Scalar* scalar_up;
cv::Mat newimg;
cv::Mat img2;
stringstream ss;
string s;
scalar_low = new cv::Scalar(0, 30, 60);
scalar_up = new cv::Scalar(20, 150, 255);
int imgwidth;
int imgheight;
srand(112);
while (j <= 45)
{
s="imagedata\\test"+ to_string(j);
s+=".jpg";
img=imread(s);
cvtColor(img, img2, CV_BGR2HSV, 0);
inRange(img2, *scalar_low, *scalar_up, newimg);
s="imagedata\\result"+ to_string(j);
s+=".jpg";
imwrite(s, newimg);
imgwidth = newimg.cols;
imgheight = newimg.rows;
i = 0;
while (i <= 499)
{
a = 0;
b = 0;
fout << "start of while loop \n";
while ((newimg.at<Vec3b>(b,a) == Vec3b(0,0,0)))
{
fout << "inside the while loop \n"
a = rand() % imgwidth;
b = rand() % imgheight;
}
fout << "outside the while loop \n";
fout << a;
fout << " ";
fout << b;
fout << " ";
i++;
}
j++;
fout << "\n";
}
}