opencv 图像细化
图像细化多用于机器人视觉,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 图像细化的更多相关文章
- OpenCV图像细化的一个例子
转自:http://blog.csdn.net/zfdxx369/article/details/9091953?utm_source=tuicool 本文是zhang的一篇经典图像细化论文,效果很好 ...
- 【opencv】图像细化
[原文:http://blog.csdn.net/qianchenglenger/article/details/19332011] 在我们进行图像处理的时候,有可能需要对图像进行细化,提取出图像的骨 ...
- SSE图像算法优化系列三十二:Zhang\Guo图像细化算法的C语言以及SIMD指令优化
二值图像的细化算法也有很多种,比较有名的比如Hilditch细化.Rosenfeld细化.基于索引表的细化.还有Opencv自带的THINNING_ZHANGSUEN.THINNING_GUOHALL ...
- OpenCV图像金字塔:高斯金字塔、拉普拉斯金字塔与图片尺寸缩放
这篇已经写得很好,真心给作者点个赞.题目都是直接转过来的,直接去看吧. Reference Link : http://blog.csdn.net/poem_qianmo/article/detail ...
- 【OpenCV新手教程之十三】OpenCV图像金字塔:高斯金字塔、拉普拉斯金字塔与图片尺寸缩放
本系列文章由@浅墨_毛星云 出品,转载请注明出处. 文章链接:http://blog.csdn.net/poem_qianmo/article/details/26157633 作者:毛星云(浅墨) ...
- Opencv 图像叠加 添加水印
Opencv 图像叠加 添加水印 C++: void Mat::copyTo(OutputArray m) const C++: void Mat::copyTo(OutputArray m, Inp ...
- opencv图像读取-imread
前言 图像的读取和保存一定要注意imread函数的各个参数及其意义,尽量不要使用默认参数,否则就像数据格式出现错误(here)一样,很难查找错误原因的: re: 1.opencv图像的读取与保存; 完
- 学习 opencv---(12)OpenCV 图像金字塔:高斯金字塔,拉普拉斯金字塔与图片尺寸缩放
在这篇文章里,我们一起学习下 图像金字塔 的一些基本概念,如何使用OpenCV函数pyrUp和pyrDown 对图像进行向上和向下采样,以及了解专门用于缩放图像尺寸的resize函数的用法.此博文一共 ...
- [OpenCV Qt教程] 在Qt图形界面中显示OpenCV图像的OpenGL Widget(第二部分)
本文译自:http://www.robot-home.it/blog/en/software/tutorial-opencv-qt-opengl-widget-per-visualizzare-imm ...
随机推荐
- Encode
by kinsly 本文的内容均基于python3.5 编码一直是python中的大坑,反正我是一直没搞明白,今天在做爬虫的时候,觉得实在是有必要把这些东西整理一下. 什么是编码 简单的来说就是,为了 ...
- Linux 版本查詢
# uname -a 查看 Kernel版本 # cat /etc/redhat-release查看 linux版本(以RedHat為例) 1.核心查詢:uname -a結果:Linux 2.x.x ...
- 【linux】记录一下遇到的各种问题
1. 解决办法: pthread不是Linux系统的默认库,编译时加上-lpthread参数,以调用链接库 gcc -o 文件名.out 文件名.c -lpthread 输出的时候直接 ./文件名.o ...
- Oracle多种表连接方式
1. 内连接(自然连接) 2. 外连接 (1)左外连接 (左边的表不加限制) (2)右外连接(右边的表不加限制) (3)全外连接(左右两表都不加限制) 3. 自连接(同一张表内的连接) SQL的标准语 ...
- python之合并多个字典或映射
问题 现在有多个字典或者映射,你想将它们从逻辑上合并为一个单一的映射后执行某些操作, 比如查找值或者检查某些键是否存在. 解决方案 假如你有如下两个字典: a = {'x': 1, 'z': 3} b ...
- servlet配置url-pattern的匹配规则
<servlet> <servlet-name>hello</servlet-name> <servlet-class>com.qf.servlet.H ...
- LeetCode Array Easy 118. Pascal's Triangle
Description Given a non-negative integer numRows, generate the first numRows of Pascal's triangle. I ...
- 出现异常: 非介入式客户端验证规则中的验证类型名称必须唯一。下列验证类型出现重复: required
在将web.config文件中的<add key="ClientValidationEnabled" value="false" /> 设为fals ...
- zabbix快速安装(Ubuntu18.04, Nginx)
ubuntu18.04快速安装zabbix4.0 https://blog.csdn.net/qq_33317586/article/details/83867756 需要安装的东西:nginx,ph ...
- 转。Nas配置。想找原版没找到,全是转载的,也没注出处,无语。
随着家用宽带的不断提速和高清电影的普及外带单反的家庭占有率越来越搞,仅靠台式机里那几块硬盘越来越不够用了. 简单的计算了一下,家里的台式机上2T的容量(1T+640G+320G)已经接近于80%满,外 ...