图像细化多用于机器人视觉,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. 使用pip 时报错 Fatal error in launcher: Unable to create process using '"D:\pytghon2.7\python.exe" "D:\python2.7\S

    无法创建使用pip.exe创建进程,说白了就是无法启动pip安装插件. 解决方法升级pip: python -m pip install -U pip  

  2. python面试题之下面这些是什么意思:@classmethod, @staticmethod, @property?

    回答背景知识 这些都是装饰器(decorator).装饰器是一种特殊的函数,要么接受函数作为输入参数,并返回一个函数,要么接受一个类作为输入参数,并返回一个类. @标记是语法糖(syntactic s ...

  3. Python系列——常用第三方库

    幕布视图(更加方便.明了):https://mubu.com/doc/AqoVZ8x6m0 参考文献:嵩天老师的Python讲义 模块 定义 计算机在开发过程中,代码越写越多,也就越难以维护,所以为了 ...

  4. React-native 关于键盘遮挡界面问题

    //引入 KeyboardAvoidingView import { KeyboardAvoidingView } from 'react-native'; //使用 KeyboardAvoiding ...

  5. sql server 事务隔离性 snapshot 、read committed说明

    一. --该 read committed 默认事务隔离级别 在 systemuser修改事务未完成时 select * from [SystemUser] where id=62; 该语句是不可读取 ...

  6. IDEA Caused by: java.lang.OutOfMemoryError: PermGen space

    错误:OutOfMemoryError: PermGen space 非堆溢出(永久保存区域溢出) 解决方法: 在Run/Debug configuration 的你要运行行的tomcat里面的 vm ...

  7. idae父子项目Test执行报Result Maps collection already contains value for xxx

    现象:同一个springmvc工程使用eclipse和idea用Tomcat启动都没问题,但是如果走单元测试使用到了@ContextConfiguration这个spring的上下文注解idea出问题 ...

  8. OC开发系列-内存管理

    概述 移动设备的内存极其有限,每个app所有占用的内存是有限的.当app所占用的内存比较多时,系统会发出内存警告,这时得回收一些不需要再使用的内存空间. 任何集成了NSObject的对象都需要手动进行 ...

  9. React 表单元素实例

    代码实例: <!DOCTYPE html> <html lang="zh-cn"> <head> <meta charset=" ...

  10. python 对redis 键值对的操作

    我们可以将Redis中的Hashes类型看成具有String Key和String Value的键值对容器.类似python中的dict,javascript的jaon,java 的map,每一个Ha ...