Hi I found an algorithm online which produces the results I need, I have implemented this algorithm into my program. However, I keep getting an Access violation reading location error. I'm not sure why this is? I should say I am a newbie at C++ and using libraries such as OpenCV
My main function is this:
int main(int argc, char** argv)
{
cout << "Welcome to the Image to Graph function"<< endl;;
cv::Mat src = cv::imread("M:/Documents/Internship Project/Dr_V_Project/Output Images/FC 100x2_no_holes100.png");
if (!src.data)
return -1;
cv::Mat bw;
cv::cvtColor(src, bw, CV_BGR2GRAY);
thinningGuoHall(bw);
waitKey(0);
return 0;
}
The other functions of this program are:
void thinningGuoHallIteration(cv::Mat& im, int iter)
{
cv::Mat marker = cv::Mat::zeros(im.size(), CV_8UC1);
for (int i = 1; i < im.rows; i++)
{
for (int j = 1; j < im.cols; j++)
{
uchar p2 = im.at<uchar>(i - 1, j);
uchar p3 = im.at<uchar>(i - 1, j + 1);
uchar p4 = im.at<uchar>(i, j + 1); // ERROR SEEMS TO OCCUR HERE!
uchar p5 = im.at<uchar>(i + 1, j + 1);
uchar p6 = im.at<uchar>(i + 1, j);
uchar p7 = im.at<uchar>(i + 1, j - 1);
uchar p8 = im.at<uchar>(i, j - 1);
uchar p9 = im.at<uchar>(i - 1, j - 1);
int C = (!p2 & (p3 | p4)) + (!p4 & (p5 | p6)) +
(!p6 & (p7 | p8)) + (!p8 & (p9 | p2));
int N1 = (p9 | p2) + (p3 | p4) + (p5 | p6) + (p7 | p8);
int N2 = (p2 | p3) + (p4 | p5) + (p6 | p7) + (p8 | p9);
int N = N1 < N2 ? N1 : N2;
int m = iter == 0 ? ((p6 | p7 | !p9) & p8) : ((p2 | p3 | !p5) & p4);
if (C == 1 && (N >= 2 && N <= 3) && m == 0)
marker.at<uchar>(i, j) = 1;
}
}
im &= ~marker;
}
void thinningGuoHall(cv::Mat& im)
{
im /= 255;
cv::Mat prev = cv::Mat::zeros(im.size(), CV_8UC1);
cv::Mat diff;
do {
thinningGuoHallIteration(im, 0);
thinningGuoHallIteration(im, 1);
cv::absdiff(im, prev, diff);
im.copyTo(prev);
} while (cv::countNonZero(diff) > 0);
im *= 255;
}
Could someone help me resolve this error?