# 图像的膨胀处理

1.Dilate.h Dilate.c 为图像膨胀的头文件、核心函数
Dilate.h

#ifndef _DILATE_H_
#define _DILATE_H_

#ifdef __cplusplus
extern "C"
{
#endif
/*
*图像膨胀处理
*/
int dilateFunction(float * inData, float * outData, int dataWidth, int dataHeight, int dilateWidth, int dilateHeight, int channels);

#ifdef __cplusplus
}
#endif
#endif

Dilate.c

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <memory.h>
#include "Dilate.h"
#include "myMath.h"

int dilateFunction(float * inData, float * outData, int dataWidth, int dataHeight, int dilateWidth, int dilateHeight, 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(dilateWidth % 2 == 0){
widthTemp = dilateWidth / 2 - 1;
}else{
widthTemp = dilateWidth / 2;
}
if(dilateHeight % 2 == 0){
heightTemp = dilateHeight / 2 - 1;
}else{
heightTemp = dilateHeight / 2;
}

dataTemp = (float*)malloc(sizeof(float) * dilateWidth * dilateHeight);
for(z = 0; z < channels; z ++){
for(i = 0; i < dataHeight; i ++){
for(j = 0; j < dataWidth; j ++){
memset(dataTemp, 0, sizeof(float) * dilateWidth * dilateHeight);
count = 0;
medianData = 0.0f;
for(m = -heightTemp; m < dilateHeight - heightTemp; m ++){
for(n = -widthTemp; n < dilateWidth - 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 = getMaxValue(dataTemp, count);
outData[(i * dataWidth + j) * channels + z] = medianData;
}
}
}
free(dataTemp);
return 0;
}

2.主程序

#include "stdafx.h"
#include "Tools.h"
#include "Dilate.h"
#include <opencv2\opencv.hpp>
using namespace cv;
int main()
{
int imageWidth, imageHeight;
int channels = 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);
dilateFunction(imageData, imageOutData, imageWidth, imageHeight, 3, 3, channels);
floatDataToUCharData(imageOutData, mImage.data, imageWidth, imageHeight, channels, 1);
imshow("dilate Image", mImage);
waitKey(0);
free(imageData);
free(imageOutData);
return 0;
}

getMaxValue();

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

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

*****************************3通道彩色图膨胀处理后图***********************

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

*****************************1通道灰度图膨胀处理后图***********************

07-05 1.2万

06-26 1.8万

07-24 987

12-13 12.8万

05-16 7506

10-24 1373

04-14 11.3万

12-10 1.6万

05-18 1712

12-25 8795