c++opencv中线条细化算法
本帖最后由 夏日已末 于 2018-1-14 23:45 编辑要达到的效果就是将线条尽量细化成单像素,按照论文上的Hilditch算法试了一下,发现效果不好,于是自己尝试着写了一下细化的算法,基本原理就是从上下左右四个方向向内收缩。
1.先是根据图片中的原则确定了以下16种情况
http://images2017.cnblogs.com/blog/1300117/201801/1300117-20180114214049144-2110740391.png
2.调试过后发现,迭代次数多了之后,原来连接着的线条会断开,分析原因如下图
http://images2017.cnblogs.com/blog/1300117/201801/1300117-20180114214401441-433701658.png
3.修改了一下判断条件
http://images2017.cnblogs.com/blog/1300117/201801/1300117-20180114214443379-1029460292.png
4.调试过后发现还是会出现断的地方,再次分析原因如下图
http://images2017.cnblogs.com/blog/1300117/201801/1300117-20180114214559254-349079025.png
5.又加了判断条件,如下图
http://images2017.cnblogs.com/blog/1300117/201801/1300117-20180114214705176-1507585760.png
最终实现的效果如下
http://images2017.cnblogs.com/blog/1300117/201801/1300117-20180114215053816-239776504.jpghttp://images2017.cnblogs.com/blog/1300117/201801/1300117-20180114215149472-645184409.jpg
对比图
http://images2017.cnblogs.com/blog/1300117/201801/1300117-20180114215405347-1417919453.png
对规则曲线的效果比较好
http://images2017.cnblogs.com/blog/1300117/201801/1300117-20180114220604769-318994302.jpghttp://images2017.cnblogs.com/blog/1300117/201801/1300117-20180114220643785-42929711.jpg
但是圆的效果不太好,有待改进
http://images2017.cnblogs.com/blog/1300117/201801/1300117-20180114220737113-1592431253.jpg
//四周细化算法
void Refine(Mat& image)
{
int p;
int top=1, down=1, right=1, left=1;
vector<Point> del;
int grayvalue = 0;
int height = image.rows; //获取图像高度
int width = image.cols; //获取图像宽度
Mat *im = reinterpret_cast<Mat*>((void*)&image); //获取像素点信息
//上下收缩
for (int i = 1; i < height-1; i++)
{
for (int j = 1; j < width-1; j++)
{
grayvalue = Get_gray(im, j, i);//获取指定点灰度值
if (grayvalue != 0) //判断中心点是否为前景
{
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 (i < height - 2)
down = (Get_gray(im, j, i + 2) == 0) ? 0 : 1;
else
down = 1;
//横向直线
if (p && (p || p || p || p) && !(p || p) && p == 0 && down)
{
del.push_back(Point(j, i));
}
if (p && (p || p || p || p) && !( p || p) && p == 0)
{
del.push_back(Point(j, i));
}
}
}
}
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++)
{
for (int j = 1; j < width - 1; j++)
{
grayvalue = Get_gray(im, j, i);//获取指定点灰度值
if (grayvalue != 0) //判断中心点是否为前景
{
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));
}
}
}
}
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;
}
} 学习了。
页:
[1]