# 图像的侵蚀处理

1.Erode.h Erode.c 是图像侵蚀的头文件、核心函数文件
Erode.h

#ifndef _ERODE_H_
#define _ERODE_H_

#ifdef __cplusplus
extern "C"
{
#endif

int erodeFunction(float * inData, float * outData, int dataWidth, int dataHeight, int erodeWidth, int erodeHeight, int channels);

#ifdef __cplusplus
}
#endif

#endif

Erode.c

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <memory.h>
#include "Erode.h"
#include "myMath.h"
int erodeFunction(float * inData, float * outData, int dataWidth, int dataHeight, int erodeWidth, int erodeHeight, int channels){
int widthTemp, heightTemp;
int i, j;
int m, n, z;
float * dataTemp = NULL;
int count = 0;
float medianData = 0.f;
if(channels > 32){
printf("channels is larger.\n");
return -1;
}
if(erodeWidth % 2 == 0){
widthTemp = erodeWidth / 2 - 1;
}else{
widthTemp = erodeWidth / 2;
}
if(erodeHeight % 2 == 0){
heightTemp = erodeHeight / 2 - 1;
}else{
heightTemp = erodeHeight / 2;
}

dataTemp = (float*)malloc(sizeof(float) * erodeWidth * erodeHeight);
for(z = 0; z < channels; z ++){
for(i = 0; i < dataHeight; i ++){
for(j = 0; j < dataWidth; j ++){
memset(dataTemp, 0, sizeof(float) * erodeWidth * erodeHeight);
count = 0;
medianData = 0.0f;
for(m = -heightTemp; m < erodeHeight - heightTemp; m ++){
for(n = -widthTemp; n < erodeWidth - widthTemp; n ++){
if(i + m >= 0 && i + m < dataHeight && j + n >= 0 && j + n < dataWidth){
dataTemp[count] = inData[((i + m) * dataWidth + (j + n)) * channels + z];
count ++;
}
}
}

medianData = getMinValue(dataTemp, count);
outData[(i * dataWidth + j) * channels + z] = medianData;
}
}
}
free(dataTemp);
return 0;
}

2.主程序函数

#include "stdafx.h"
#include "Tools.h"
#include "Erode.h"
#include <opencv2\opencv.hpp>
using namespace cv;
int main()
{
int imageWidth, imageHeight;
int channels = 1;
Mat mImage = imread("D:\\workSpace\\VSWorkSpace\\ImageProcess\\ImageProcess\\erodeImage.jpg", 1);
imshow("srcImage", mImage);
float * imageData = NULL;
float * imageOutData = NULL;
imageWidth = mImage.cols;
imageHeight = mImage.rows;
channels = mImage.channels();
imageData = (float*)malloc(sizeof(float) * imageWidth * imageHeight * channels);
imageOutData = (float*)malloc(sizeof(float) * imageWidth * imageHeight * channels);
uCharDataToFloatData(mImage.data, imageData, imageWidth, imageHeight, channels);
erodeFunction(imageData, imageOutData, imageWidth, imageHeight, 5, 5, channels);
floatDataToUCharData(imageOutData, mImage.data, imageWidth, imageHeight, channels, 1);
imshow("erode Image", mImage);
waitKey(0);
free(imageData);
free(imageOutData);
return 0;
}

3.getMinValue();

http://blog.csdn.net/jsf921942722/article/details/51527155
uCharDataToFloatData();
floatDataToUCharData();

http://blog.csdn.net/jsf921942722/article/details/51526673
4.效果图
****************************3通道彩色图原图********************

****************************3通道彩色图侵蚀后图********************

****************************1通道灰度图原图********************

****************************1通道灰度图侵蚀后图********************

#### [opencv]6.图片的处理：侵蚀、膨胀、反转

2016-04-28 14:38:05

#### 基于c#的.NET实现二值图像的腐蚀和膨胀（图像形态学）

2011年07月04日 49KB 下载

#### opencv 图像的腐蚀与膨胀

2015-07-31 19:03:07

#### 图像处理 腐蚀 膨胀 细化

2015-10-24 13:28:34

#### opencv(c++)图像处理（imgproc模块）

2017-11-06 19:35:09

#### 【数字图像处理学习笔记之四】图像腐蚀、膨胀

2016-07-05 21:54:45

#### C#实现图像腐蚀运算、膨胀运算、开运算与闭运算、击中击不中变换

2014年12月10日 153KB 下载

#### matlab二值图像腐蚀（黑白图像）

2016-01-27 15:58:57

#### 警惕日本文化的侵蚀

2005-03-27 22:50:00

#### 图像的膨胀与腐蚀实现

2016-10-08 11:36:02