//#include "stdafx.h"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include
#include
using namespace cv;
using namespace std;
/*
* @brief 对输入图像进行细化,骨骼化
* @param src为输入图像,用cvThreshold函数处理过的8位灰度图像格式,元素中只有0与1,1代表有元素,0代表为空白
* @param dst为对src细化后的输出图像,格式与src格式相同,元素中只有0与1,1代表有元素,0代表为空白
*/
void thinImage(Mat & src, Mat & dst)
{
int width = src.cols;
int height = src.rows;
src.copyTo(dst);
vector mFlag; //用于标记需要删除的点
while (true)
{
//步骤一
for (int i = 0; i < height; ++i)
{
uchar * p = dst.ptr(i);
for (int j = 0; j < width; ++j)
{
//获得九个点对象,注意边界问题
uchar p1 = p[j];
if (p1 != 1) continue;
uchar p2 = (i == 0) ? 0 : *(p - dst.step + j);
uchar p3 = (i == 0 || j == width - 1) ? 0 : *(p - dst.step + j + 1);
uchar p4 = (j == width - 1) ? 0 : *(p + j + 1);
uchar p5 = (i == height - 1 || j == width - 1) ? 0 : *(p + dst.step + j + 1);
uchar p6 = (i == height - 1) ? 0 : *(p + dst.step + j);
uchar p7 = (i == height - 1 || j == 0) ? 0 : *(p + dst.step + j - 1);
uchar p8 = (j == 0) ? 0 : *(p + j - 1);
uchar p9 = (i == 0 || j == 0) ? 0 : *(p - dst.step + j - 1);
if ((p2 + p3 + p4 + p5 + p6 + p7 + p8 + p9) >= 2