void HereBeDragons::MyNoblerPartToMyGrossBodysTreason(cv::Mat &My){ My = 255 - My; My /= 255; cv::Mat prev = cv::Mat::zeros(My.size(), CV_8UC1); cv::Mat diff; do { thinningIteration(My, 0); thinningIteration(My, 1); cv::absdiff(My, prev, diff); My.copyTo(prev); } while (cv::countNonZero(diff) > 0); My *= 255; My = 255 - My; }
/* * Function for thinning the given binary image * * Based off of code found from here: http://opencv-code.com/quick-tips/implementation-of-thinning-algorithm-in-opencv/ * * This is part of the Zhang-Suen algorithm, documentation on how it works can be seen here: http://read.pudn.com/downloads99/sourcecode/graph/texture_mapping/403914/Parallel%20thinning%20with%20two- subiteration%20algorithms.pdf * * @param im Binary image with range = 0-255 */ void ImageProcessor::thinning(cv::Mat& im){ im /= 255; cv::Mat prev = cv::Mat::zeros(im.size(), CV_8UC1); cv::Mat diff; do{ thinningIteration(im, 0); thinningIteration(im, 1); cv::absdiff(im, prev, diff); im.copyTo(prev); } while (cv::countNonZero(diff) > 0); im *= 255; }
Mat Thinning::doThinning(Mat img){ // Fast thinning algorithm implmented from https://github.com/bsdnoobz/zhang-suen-thinning img /= 255; Mat prev = Mat::zeros(img.size(), CV_8UC1); Mat diff; do{ thinningIteration(img, 0); thinningIteration(img, 1); absdiff(img, prev, diff); img.copyTo(prev); } while (countNonZero(diff) > 0); img *= 255; return img; }
/** * Function for thinning the given binary image * * Parameters: * src The source image, binary with range = [0,255] * dst The destination image */ void thinningImage(const cv::Mat& src, cv::Mat& dst) { dst = src.clone(); dst /= 255; // convert to binary image cv::Mat prev = cv::Mat::zeros(dst.size(), CV_8UC1); cv::Mat diff; do { thinningIteration(dst, 0); thinningIteration(dst, 1); cv::absdiff(dst, prev, diff); dst.copyTo(prev); } while (cv::countNonZero(diff) > 0); dst *= 255; }
// Apply the thinning procedure to a given image void thinning(InputArray input, OutputArray output){ Mat processed = input.getMat().clone(); // Enforce the range of the input image to be in between 0 - 255 processed /= 255; Mat prev = Mat::zeros(processed.size(), CV_8UC1); Mat diff; do { thinningIteration(processed, 0); thinningIteration(processed, 1); absdiff(processed, prev, diff); processed.copyTo(prev); } while (countNonZero(diff) > 0); processed *= 255; output.assign(processed); }
/** * Function for thinning the given binary image * * @param im Binary image with range = 0-255 * @param useGuoHall Use Guo-Hall or Zhang-Suen algorithm */ void thinning(cv::Mat& im, bool useGuoHall = false) { im /= 255; if (useGuoHall) { cv::Mat diff; cv::Mat prev = cv::Mat::zeros(im.size(), CV_8UC1); do { thinningGuoHallIteration(im, 0); thinningGuoHallIteration(im, 1); cv::absdiff(im, prev, diff); im.copyTo(prev); } while (cv::countNonZero(diff) > 0); } else { cv::Mat marker = cv::Mat::ones(im.size(), CV_8UC1); while (thinningIteration(im, marker) > 0); } im *= 255; }