图像细化多用于机器人视觉,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. BUUCTF PWN部分题目wp

    pwn好难啊 PWN 1,连上就有flag的pwnnc buuoj.cn 6000得到flag 2,RIP覆盖一下用ida分析一下,发现已有了system,只需覆盖RIP为fun()的地址,用peda ...

  2. LeetCode Array Easy 189. Rotate Array

    ---恢复内容开始--- Description Given an array, rotate the array to the right by k steps, where k is non-ne ...

  3. ZedGraph怎样在生成曲线时随机生成不一样的颜色

    场景 在使用ZedGraph生成多条曲线时为了能区分曲线颜色,要求随机设置曲线颜色. 首先从System.Drawing.Color中获取所有颜色的对象的数组,然后将其顺序打乱随机排序,然后在生成曲线 ...

  4. js 单行间隙滚动

    代码: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3. ...

  5. macOS添加字体

    打开macOS的Applications文件夹: 双击其中的Font Book: 把下载的字体解压后的_文件夹_ 拖进来,字体就安装好了.

  6. H5新属性 contenteditable

    contenteditable 属性规定元素内容是否可编辑 <div contenteditable style="width: 100px;height:100px"> ...

  7. Redis中存储对象区别

    1.最常用的是String结构,key和value都是字符串类型: 2.哈希:比较是用于对对象的操作: 3.List:按照插入数据顺序保存,value是可以重复的,底层是双向链表: 4.集合:是Str ...

  8. react 教程—核心概念

    react 核心概念  : https://react.docschina.org/docs/getting-started.html(官网) 或  https://www.w3cschool.cn/ ...

  9. Haskell语法

    http://www.ibm.com/developerworks/cn/java/j-cb07186.html 1. 构造符号 : 比如: 1:2:3:[] 而常用的 [1,2,3] 是一种语法糖( ...

  10. HDU 5119 Happy Matt Friends (背包DP + 滚动数组)

    题目链接:HDU 5119 Problem Description Matt has N friends. They are playing a game together. Each of Matt ...