改进的细化算法
本帖最后由 夏日已末 于 2018-1-24 12:51 编辑原来每次遍历都是一整张图,现在每次都记录下需要操作的起始位置,下一次直接遍历起始位置就可以了。原来的算法运行100次的时间为3.901秒改进后的时间为0.148秒。
代码如下:void RefineNew(Mat& image,int num)
//四周细化算法
void RefineNew(Mat& image, int num)
{
int p;
int top = 1, down = 1, right = 1, left = 1;
vector<Point> del;
int grayvalue = 0;
bool state;
int height = image.rows; //获取图像高度
int width = image.cols; //获取图像宽度
vector<Point> run_up_down, run_lef_right;
Point next;//存放下一次的宽度或高度
//用于存放每一行需要处理图像的宽度
for (int i = 0; i < height; i++)
{
run_lef_right.push_back(Point(1, width - 1));
}
//用于存放每一列需要处理图像的高度
for (int i = 0; i < width; i++)
{
run_up_down.push_back(Point(1, height - 1));
}
Mat *im = reinterpret_cast<Mat*>((void*)&image); //获取像素点信息
for (int ite = 0; ite < num; ite++)
{
//上下收缩
for (int i = 1; i < width - 1; i++)
{
state = 1;
for (int j = run_up_down.x; j <run_up_down.y; j++)
{
grayvalue = Get_gray(im, i, j);//获取指定点灰度值
if (grayvalue != 0) //判断中心点是否为前景
{
if (state)
{
next.x = j;
next.y = j;
state = 0;
}
else
{
next.y = j;
}
p = (Get_gray(im, i + 1, j) == 0) ? 0 : 1;
p = (Get_gray(im, i + 1, j - 1) == 0) ? 0 : 1;
p = (Get_gray(im, i, j - 1) == 0) ? 0 : 1;
p = (Get_gray(im, i - 1, j - 1) == 0) ? 0 : 1;
p = (Get_gray(im, i - 1, j) == 0) ? 0 : 1;
p = (Get_gray(im, i - 1, j + 1) == 0) ? 0 : 1;
p = (Get_gray(im, i, j + 1) == 0) ? 0 : 1;
p = (Get_gray(im, i + 1, j + 1) == 0) ? 0 : 1;
if (j < height - 2)
down = (Get_gray(im, i, j + 2) == 0) ? 0 : 1;//判断下面第二格的状态
else
down = 1;
//横向直线
if (p && (p || p || p || p) && !(p || p) && p == 0 && down)
{
del.push_back(Point(i, j));
}
if (p && (p || p || p || p) && !(p || p) && p == 0)
{
del.push_back(Point(i, j));
}
}
}
if (!state)
{
run_up_down.x = next.x;
run_up_down.y = next.y + 1;
}
else
{
run_up_down.x = 1;
run_up_down.y = 1;
}
}
for (int i = 1; i < height - 2; i++)
{
grayvalue = Get_gray(im, 0, i);
//最上边一行
if (grayvalue != 0)
{
if (Get_gray(im, 0, i - 1) && Get_gray(im, 1, i - 1) && Get_gray(im, 0, i + 1) == 0 && Get_gray(im, 1, i) == 0) //上2,上1,右上1,下1=0,右1=0
{
del.push_back(Point(0, i));
}
if (Get_gray(im, 0, i - 1) == 0 && Get_gray(im, 1, i + 1) && Get_gray(im, 1, i) == 0 && Get_gray(im, 0, i + 2))//上1=0,下1,右下1,右1=0,下2
{
del.push_back(Point(0, i));
}
}
//最下边一行
if (grayvalue != 0)
{
if (Get_gray(im, width - 1, i - 1) && Get_gray(im, width - 2, i - 1) && Get_gray(im, width - 1, i + 1) == 0 && Get_gray(im, width - 2, i) == 0) //上2,上1,左上1,下1=0,左1=0
{
del.push_back(Point(width - 1, i));
}
if (Get_gray(im, width - 1, i - 1) == 0 && Get_gray(im, width - 2, i + 1) && Get_gray(im, width - 2, i) == 0 && Get_gray(im, width - 1, i + 2))//上1=0,下1,左下1,左1=0,下2
{
del.push_back(Point(width - 1, i));
}
}
}
for (int i = 0; i < del.size(); i++)
{
uchar* data = image.ptr<uchar>(del.y);
data.x] = 0;
}
//左右收缩
for (int i = 1; i < height - 1; i++)
{
state = 1;
for (int j = run_lef_right.x; j <run_lef_right.y; j++)
{
grayvalue = Get_gray(im, j, i);//获取指定点灰度值
if (grayvalue != 0) //判断中心点是否为前景
{
if (state)
{
next.x = j;
next.y = j;
state = 0;
}
else
{
next.y = j;
}
p = (Get_gray(im, j + 1, i) == 0) ? 0 : 1;
p = (Get_gray(im, j + 1, i - 1) == 0) ? 0 : 1;
p = (Get_gray(im, j, i - 1) == 0) ? 0 : 1;
p = (Get_gray(im, j - 1, i - 1) == 0) ? 0 : 1;
p = (Get_gray(im, j - 1, i) == 0) ? 0 : 1;
p = (Get_gray(im, j - 1, i + 1) == 0) ? 0 : 1;
p = (Get_gray(im, j, i + 1) == 0) ? 0 : 1;
p = (Get_gray(im, j + 1, i + 1) == 0) ? 0 : 1;
if (j < width - 2)
right = (Get_gray(im, j + 2, i) == 0) ? 0 : 1; //判断右边第二格的状态
else
right = 1;
//竖直线
if (p && (p || p || p || p) && !(p || p) && p == 0 && right)
{
del.push_back(Point(j, i));
}
if (p && (p || p || p || p) && !(p || p) && p == 0)
{
del.push_back(Point(j, i));
}
}
}
if (!state)
{
run_lef_right.x = next.x;
run_lef_right.y = next.y + 1;
}
else
{
run_lef_right.x = 1;
run_lef_right.y = 1;
}
}
//最左边一列
for (int j = 1; j < width - 2; j++)
{
grayvalue = Get_gray(im, j, 0);
if (grayvalue != 0)
{
if (Get_gray(im, j - 1, 0) == 0 && Get_gray(im, j + 1, 0) && Get_gray(im, j + 2, 0) && Get_gray(im, j, 1) == 0 && Get_gray(im, j + 1, 1)) //左1=0,右1,右2,下1=0,右下1
{
del.push_back(Point(j, 0));
}
if (Get_gray(im, j - 1, 0) && Get_gray(im, j + 1, 0) == 0 && Get_gray(im, j, 1) == 0 && Get_gray(im, j - 1, 1))//左1,右1=0,下1=0,左下1
{
del.push_back(Point(j, 0));
}
}
}
//最右边一列
for (int j = 1; j < width - 2; j++)
{
grayvalue = Get_gray(im, j, height - 1);
if (grayvalue != 0)
{
if (Get_gray(im, j - 1, height - 1) == 0 && Get_gray(im, j + 1, height - 1) && Get_gray(im, j + 2, height - 1) && Get_gray(im, j, height - 2) == 0 && Get_gray(im, j + 1, height - 2)) //左1=0,右1,右2,下1=0,右下1
{
del.push_back(Point(j, height - 1));
}
if (Get_gray(im, j - 1, height - 1) && Get_gray(im, j + 1, height - 1) == 0 && Get_gray(im, j, height - 2) == 0 && Get_gray(im, j - 1, height - 2))//左1,右1=0,下1=0,左下1
{
del.push_back(Point(j, height - 1));
}
}
}
for (int i = 0; i < del.size(); i++)
{
uchar* data = image.ptr<uchar>(del.y);
data.x] = 0;
}
}
}} 666,顶一个 这个是什么算法啊
页:
[1]