图像细化多用于机器人视觉,OCR字符识别等领域,细化后的图像经过去毛刺就成为了我们常说的图像的骨架。

 该图像细化代码依据论文: T. Y. ZHANG and C. Y. SUEN  A Fast Parallel Algorithm for Thinning Digital Patterns

代码如下:

void ThinSubiteration1(Mat & pSrc, Mat & pDst) {
int rows = pSrc.rows;
int cols = pSrc.cols;
pSrc.copyTo(pDst);
for(int i = ; i < rows; i++) {
for(int j = ; j < cols; j++) {
if(pSrc.at<float>(i, j) == 1.0f) {
/// get 8 neighbors
/// calculate C(p)
int neighbor0 = (int) pSrc.at<float>( i-, j-);
int neighbor1 = (int) pSrc.at<float>( i-, j);
int neighbor2 = (int) pSrc.at<float>( i-, j+);
int neighbor3 = (int) pSrc.at<float>( i, j+);
int neighbor4 = (int) pSrc.at<float>( i+, j+);
int neighbor5 = (int) pSrc.at<float>( i+, j);
int neighbor6 = (int) pSrc.at<float>( i+, j-);
int neighbor7 = (int) pSrc.at<float>( i, j-);
int C = int(~neighbor1 & ( neighbor2 | neighbor3)) +
int(~neighbor3 & ( neighbor4 | neighbor5)) +
int(~neighbor5 & ( neighbor6 | neighbor7)) +
int(~neighbor7 & ( neighbor0 | neighbor1));
if(C == ) {
/// calculate N
int N1 = int(neighbor0 | neighbor1) +
int(neighbor2 | neighbor3) +
int(neighbor4 | neighbor5) +
int(neighbor6 | neighbor7);
int N2 = int(neighbor1 | neighbor2) +
int(neighbor3 | neighbor4) +
int(neighbor5 | neighbor6) +
int(neighbor7 | neighbor0);
int N = min(N1,N2);
if ((N == ) || (N == )) {
/// calculate criteria 3
int c3 = ( neighbor1 | neighbor2 | ~neighbor4) & neighbor3;
if(c3 == ) {
pDst.at<float>( i, j) = 0.0f;
}
}
}
}
}
}
} void ThinSubiteration2(Mat & pSrc, Mat & pDst) {
int rows = pSrc.rows;
int cols = pSrc.cols;
pSrc.copyTo( pDst);
for(int i = ; i < rows; i++) {
for(int j = ; j < cols; j++) {
if (pSrc.at<float>( i, j) == 1.0f) {
/// get 8 neighbors
/// calculate C(p)
int neighbor0 = (int) pSrc.at<float>( i-, j-);
int neighbor1 = (int) pSrc.at<float>( i-, j);
int neighbor2 = (int) pSrc.at<float>( i-, j+);
int neighbor3 = (int) pSrc.at<float>( i, j+);
int neighbor4 = (int) pSrc.at<float>( i+, j+);
int neighbor5 = (int) pSrc.at<float>( i+, j);
int neighbor6 = (int) pSrc.at<float>( i+, j-);
int neighbor7 = (int) pSrc.at<float>( i, j-);
int C = int(~neighbor1 & ( neighbor2 | neighbor3)) +
int(~neighbor3 & ( neighbor4 | neighbor5)) +
int(~neighbor5 & ( neighbor6 | neighbor7)) +
int(~neighbor7 & ( neighbor0 | neighbor1));
if(C == ) {
/// calculate N
int N1 = int(neighbor0 | neighbor1) +
int(neighbor2 | neighbor3) +
int(neighbor4 | neighbor5) +
int(neighbor6 | neighbor7);
int N2 = int(neighbor1 | neighbor2) +
int(neighbor3 | neighbor4) +
int(neighbor5 | neighbor6) +
int(neighbor7 | neighbor0);
int N = min(N1,N2);
if((N == ) || (N == )) {
int E = (neighbor5 | neighbor6 | ~neighbor0) & neighbor7;
if(E == ) {
pDst.at<float>(i, j) = 0.0f;
}
}
}
}
}
}
}
int main(int argc, char* argv[])
{
Mat src = imread("D://thinning.png", );
Mat inputarray = src(Rect(, , src.cols - , src.rows - ));
threshold(inputarray, inputarray, , , CV_THRESH_BINARY);
Mat outputarray(inputarray.rows,inputarray.cols,CV_32FC1); bool bDone = false;
int rows = inputarray.rows;
int cols = inputarray.cols; inputarray.convertTo(inputarray, CV_32FC1); inputarray.copyTo(outputarray); //outputarray.convertTo(outputarray, CV_32FC1); /// pad source
Mat p_enlarged_src = Mat(rows + , cols + , CV_32FC1);
for (int i = ; i < (rows + ); i++) {
p_enlarged_src.at<float>(i, ) = 0.0f;
p_enlarged_src.at<float>(i, cols + ) = 0.0f;
}
for (int j = ; j < (cols + ); j++) {
p_enlarged_src.at<float>(, j) = 0.0f;
p_enlarged_src.at<float>(rows + , j) = 0.0f;
}
for (int i = ; i < rows; i++) {
for (int j = ; j < cols; j++) {
if (inputarray.at<float>(i, j) >= 20.0f) {
p_enlarged_src.at<float>(i + , j + ) = 1.0f;
}
else
p_enlarged_src.at<float>(i + , j + ) = 0.0f;
}
} /// start to thin
Mat p_thinMat1 = Mat::zeros(rows + , cols + , CV_32FC1);
Mat p_thinMat2 = Mat::zeros(rows + , cols + , CV_32FC1);
Mat p_cmp = Mat::zeros(rows + , cols + , CV_8UC1); while (bDone != true) {
/// sub-iteration 1
ThinSubiteration1(p_enlarged_src, p_thinMat1);
/// sub-iteration 2
//ThinSubiteration2(p_thinMat1, p_thinMat2);
/// compare
compare(p_enlarged_src, p_thinMat1, p_cmp, CV_CMP_EQ);
/// check
int num_non_zero = countNonZero(p_cmp);
if (num_non_zero == (rows + ) * (cols + )) {
bDone = true;
}
/// copy
p_thinMat1.copyTo(p_enlarged_src);
}
// copy result
for (int i = ; i < rows; i++) {
for (int j = ; j < cols; j++) {
outputarray.at<float>(i, j) = p_enlarged_src.at<float>(i + , j + );
}
}
imshow("src", inputarray);
imshow("dst", p_enlarged_src);
waitKey(); return ; }

附上效果图:

未完待续。。。。

opencv 图像细化的更多相关文章

  1. OpenCV图像细化的一个例子

    转自:http://blog.csdn.net/zfdxx369/article/details/9091953?utm_source=tuicool 本文是zhang的一篇经典图像细化论文,效果很好 ...

  2. 【opencv】图像细化

    [原文:http://blog.csdn.net/qianchenglenger/article/details/19332011] 在我们进行图像处理的时候,有可能需要对图像进行细化,提取出图像的骨 ...

  3. SSE图像算法优化系列三十二:Zhang\Guo图像细化算法的C语言以及SIMD指令优化

    二值图像的细化算法也有很多种,比较有名的比如Hilditch细化.Rosenfeld细化.基于索引表的细化.还有Opencv自带的THINNING_ZHANGSUEN.THINNING_GUOHALL ...

  4. OpenCV图像金字塔:高斯金字塔、拉普拉斯金字塔与图片尺寸缩放

    这篇已经写得很好,真心给作者点个赞.题目都是直接转过来的,直接去看吧. Reference Link : http://blog.csdn.net/poem_qianmo/article/detail ...

  5. 【OpenCV新手教程之十三】OpenCV图像金字塔:高斯金字塔、拉普拉斯金字塔与图片尺寸缩放

    本系列文章由@浅墨_毛星云 出品,转载请注明出处. 文章链接:http://blog.csdn.net/poem_qianmo/article/details/26157633 作者:毛星云(浅墨) ...

  6. Opencv 图像叠加 添加水印

    Opencv 图像叠加 添加水印 C++: void Mat::copyTo(OutputArray m) const C++: void Mat::copyTo(OutputArray m, Inp ...

  7. opencv图像读取-imread

    前言 图像的读取和保存一定要注意imread函数的各个参数及其意义,尽量不要使用默认参数,否则就像数据格式出现错误(here)一样,很难查找错误原因的: re: 1.opencv图像的读取与保存; 完

  8. 学习 opencv---(12)OpenCV 图像金字塔:高斯金字塔,拉普拉斯金字塔与图片尺寸缩放

    在这篇文章里,我们一起学习下 图像金字塔 的一些基本概念,如何使用OpenCV函数pyrUp和pyrDown 对图像进行向上和向下采样,以及了解专门用于缩放图像尺寸的resize函数的用法.此博文一共 ...

  9. [OpenCV Qt教程] 在Qt图形界面中显示OpenCV图像的OpenGL Widget(第二部分)

    本文译自:http://www.robot-home.it/blog/en/software/tutorial-opencv-qt-opengl-widget-per-visualizzare-imm ...

随机推荐

  1. Java-技术专区-问题专区-应该了解的基础技术点

    Java基础 Arrays.sort实现原理和Collection实现原理 foreach和while的区别(编译之后) 线程池的种类,区别和使用场景 分析线程池的实现原理和线程的调度过程 线程池如何 ...

  2. axure破解版

    axure 破解版   https://www.cnblogs.com/lianghong/p/9385233.html   授权: zdfans.com 注册码:  gP5uuK2gH+iIVO3Y ...

  3. CSS3新增(选择器{属性选择器,结构伪类选择器,伪元素选择器})

    1.属性选择器 属性选择器,可以根据元素特定的属性来选择元素,这样就不用借助 类 或者 id选择器. E [ att ]   选择具有 att 属性的 E 元素   例如:input [ value ...

  4. 解决 使用migrations 执行update-database 出现System.InvalidOperationException: 实例失败的问题

    好久没有使用Code First的方式来创建模型了  今天重温了一下 但是出现了很多问题 现在总结一下 在我做完初期的操作的之后,使用 update-database -verbose 更新数据库时, ...

  5. 【记录】API Gateway作用? 与过滤器的区别?Nginx与Zuul区别?

    网关(gateway)的作用: 网关可以拦截客户端所有请求,对该请求进行权限控制.负载均衡.日志管理.接口调用监控等 过滤器与网关的区别是什么? 过滤器是拦截单个tomcat服务器请求. 网关是拦截整 ...

  6. nginx proxy_pass设置

    NGINX服务器的反向代理PROXY_PASS配置方法讲解 https://www.cnblogs.com/lianxuan1768/p/8383804.html Nginx配置proxy_pass转 ...

  7. nodejs模块——fs模块 读取文件

    readFile读取文件 fs.readFile(filename,[option],callback) 方法读取文件. 参数说明: filename String 文件名 option Object ...

  8. CF734E Anton and Tree

    \(\mathtt{CF734E}\) \(\mathcal{Description}\) 给一棵\(n(n\leq200000)\)个节点的树,每个点为黑色或白色,一次操作可以使一个相同颜色的连通块 ...

  9. Map集合类(一.hashMap源码解析jdk1.8)

    java集合笔记一 java集合笔记二 java集合笔记三 jdk 8 之前,其内部是由数组+链表来实现的,而 jdk 8 对于链表长度超过 8 的链表将转储为红黑树 1.属性 //节点数组,第一次使 ...

  10. sql delete语句

    如果要删除数据库表中的记录,我们可以使用DELETE语句. DELETE语句的基本语法是: DELETE FROM <表名> WHERE ...; 例如,我们想删除students表中id ...