腐蚀膨胀等形态学处理c代码

// 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代码实现的形态学处理算法,适当修改可用于嵌入式的图像处理

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值