腐蚀膨胀等形态学处理c代码
2016-05-15 22:12
441 查看
// Morph.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" #include "cv.h" #include "highgui.h" #include "stdio.h" #include "stdlib.h" #include "vector" using namespace std; //腐蚀 void MorphErosion(unsigned char* src, unsigned char* dst, int width, int height, int strutWidth, int structHeight) { if (width - strutWidth < 0 && height - structHeight < 0)return; if (strutWidth != structHeight)return; int mid = (strutWidth + 1) / 2 - 1; unsigned char val = 255; for (int i = mid; i < height - mid; i++) { for (int j = mid; j < width - mid; j++) { for (int m = 0; m < strutWidth; m++) { for (int n = 0; n < structHeight; n++) { if (m == mid && n == mid )continue; val &= src[(i+m) * width + j + n]; } } dst[i * width + j] = val; val = 255; } } } //膨胀 void MorphDilition(unsigned char* src, unsigned char* dst, int width, int height, int strutWidth, int structHeight) { if (width - strutWidth < 0 && height - structHeight < 0)return; if (strutWidth != structHeight)return; int mid = (strutWidth + 1) / 2 - 1; unsigned char val = 0; for (int i = mid; i < height - mid; i++) { for (int j = mid; j < width - mid; j++) { for (int m = 0; m < strutWidth; m++) { for (int n = 0; n < structHeight; n++) { if (m == mid && n == mid )continue; val |= src[(i+m) * width + j + n]; } } dst[i * width + j] = val; val = 0; } } } //先腐蚀后膨胀 void MorphOpen(unsigned char* src, unsigned char* tmp, int width, int height, int strutWidth, int structHeight) { MorphErosion(src, tmp, width, height, strutWidth, structHeight); MorphDilition(tmp, src, width, height, strutWidth, structHeight); } int _tmain(int argc, _TCHAR* argv[]) { IplImage * src, *dst ; src = cvLoadImage("src.bmp",0); IplImage* s=cvCreateImage(cvGetSize(src), IPL_DEPTH_8U,1); cvThreshold(src,src,180,255,CV_THRESH_BINARY); dst = cvCloneImage(src); MorphOpen((unsigned char*)src->imageData, (unsigned char*)dst->imageData, src->widthStep, src->height, 3, 3); cvShowImage("src",src); cvShowImage("dst",dst) ; cvWaitKey(0) ; cvReleaseImage(&src) ; cvReleaseImage(&dst) ; return 0; }
这是一段用C代码实现的形态学处理算法,适当修改可用于嵌入式的图像处理
相关文章推荐
- Python中Function(函数)和methon(方法)
- JSP--JavaBean
- struts2中struts.xml中的属性值含义
- 编译raspberrypi b+
- 理解SpringAOP
- Mongoose学习(1)
- Java构造器小结
- mysql connector c++ {realStr=<读取字符串的字符时出错。> }
- Java使用SmartUploadServlet进行文件的上传与下载
- Java面向对象之继承
- PAT-模拟赛--Java-L1-7. 跟奥巴马一起画方块
- 从源码理解WeakHashMap.java
- PHP语言 -- Smarty分页查询
- Python学习笔记
- Spring框架初探【3】
- Java并发-同步器CyclicBarrier,Exchanger,Phaser
- Matlab Tricks(十)—— padarray 的实现
- c/c++栈与队的数组实现方法
- C++静态成员变量和静态成员函数使用总结
- c++ primer(第五版)学习笔记及习题答案代码版(第八章)IO库