您的位置:首页 > 编程语言

腐蚀膨胀等形态学处理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代码实现的形态学处理算法,适当修改可用于嵌入式的图像处理
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: