美文网首页
图像膨胀腐蚀--膨胀部分优化

图像膨胀腐蚀--膨胀部分优化

作者: 教训小磊 | 来源:发表于2022-11-03 13:57 被阅读0次

针对输入图像为灰度图,而非二值图出现的问题进行优化,主要针对膨胀部分进行了优化,同时解决了膨胀后图像四周出现黑框的问题。

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<math.h>
#include<stdlib.h>

//腐蚀
void MorphErosion(unsigned char* src, unsigned char* dst, int width, int height, int strutWidth, int structHeight)

{

    if (width - strutWidth < 0 && height - structHeight < 0)return;
    int midY = (structHeight + 1) / 2 - 1;
    unsigned char val = 255;
    for (int i = midY; i < height - midY; i++)
    {
        for (int j = midY; j < width - midY; j++)
        {
                for (int n = 0; n < strutWidth; n++)
                {
                    val &= src[i * width + j + n];
                }
            dst[i * width + j] = val;
            val = 255;
        }
    }
}


//膨胀改
void MorphDilition(unsigned int* src, unsigned int* dst, int width, int height, int strutWidth, int structHeight)

{

    if (width - strutWidth < 0 && height - structHeight < 0)return;
    int midY = (structHeight + 1) / 2 - 1;

    unsigned int val = 0;

    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j++)
        {
            if (j >(width - strutWidth))
            {
                val = src[i * width + j];
            }
            else
            {
                for (int n = 0; n < structHeight; n++)
                {
                    if (src[i * width + j + n] > val)
                    {
                        val = src[i * width + j + n];
                    }
                }
            }
            dst[i * width + j] = val;
            val = 0;
        }
    }
}


void MorphOpen(unsigned int* src, unsigned int* tmp, int width, int height, int strutWidth, int structHeight)

{

    //MorphErosion(src, tmp, width, height, strutWidth, structHeight);

    //MorphDilition(tmp, src, width, height, strutWidth, structHeight);

    MorphDilition(src, tmp, width, height, strutWidth, structHeight);


}

int main()
{
    int iRet = 0;
    unsigned int **src = NULL;
    unsigned int *osrc = NULL;
    unsigned int *odst = NULL;
    int width = 864;
    int heigh = 360;
    int kernel_w = 31;
    int kernel_h = 31;

    //为输入图像分配内存
    src = (unsigned int**)malloc(heigh * sizeof(unsigned int*));
    for (int i = 0; i < heigh; i++)
    {
        src[i] = (unsigned int*)malloc(width * sizeof(unsigned int));
    }

    //读取输入图像
    FILE* fpsrc;
    if ((fpsrc = fopen("0.txt", "r")) == NULL)
    {
        printf("OPEN ERROR\n");
        return -1;
    }
    else
    {
        for (int h = 0; h < heigh; h++)
        {
            for (int w = 0; w < width; w++)
            {
                fscanf(fpsrc, "%d", &src[h][w]);
            }
        }
    }

    ////对输入图像的像素值设定阈值
    //for (int h = 0; h < heigh; h++)
    //{
    //    for (int w = 0; w < width; w++)
    //    {
    //        int tmp = src[h][w];
    //        if (tmp > 100)
    //        {
    //            src[h][w] = 255;
    //        }
    //        else if (tmp <= 100)
    //        {
    //            src[h][w] = 0;
    //        }
    //    }
    //}

    //FILE *fpout;
    //fpout = fopen("out_pic.txt", "w");
    //for (int h = 0; h < heigh; h++)
    //{
    //    for (int w = 0; w < width; w++)
    //    {
    //        fprintf(fpout, "%d ", src[h][w]);
    //    }
    //    fputs("ok!!!!\n", fpout);
    //}

    osrc = (unsigned int*)malloc(width*heigh * sizeof(unsigned int));
    for (int h = 0; h < heigh; h++)
    {
        for (int w = 0; w < width; w++)
        {
            osrc[h*width + w] = src[h][w];
        }
    }

    odst = (unsigned int*)malloc(width*heigh * sizeof(unsigned int));
    memset(odst, 0, width*heigh * sizeof(unsigned int));

    MorphOpen(osrc, odst, width, heigh, kernel_w, kernel_h);

    FILE *fpout;
    fpout = fopen("dilition_31x31.txt", "w");
    for (int h = 0; h < heigh; h++)
    {
        for (int w = 0; w < width; w++)
        {
            fprintf(fpout, "%d\n", odst[h*width + w]);
        }
    }

    system("pause");
    return iRet;
}

相关文章

  • 图像膨胀腐蚀--膨胀部分优化

    针对输入图像为灰度图,而非二值图出现的问题进行优化,主要针对膨胀部分进行了优化,同时解决了膨胀后图像四周出现黑框的问题。

  • 22、图像形态学:膨胀与腐蚀

    注:都是基于黑白像素而言,无论是彩色图像还是二值化图像。膨胀是趋于白色的区域膨胀;腐蚀是基于白色区域腐蚀 腐蚀 膨胀

  • 图片处理-opencv-5.图像形态学(腐蚀,膨胀,开运算,闭运

    图像腐蚀与图像膨胀 图像的膨胀(Dilation)和腐蚀(Erosion)是两种基本的形态学运算,主要用来寻找图像...

  • OpenCV (iOS)中的腐蚀和膨胀(9)

    膨胀和腐蚀 如实对膨胀和腐蚀不了解需要先看一下这篇文章,了解图像处理 腐蚀 膨胀 细化 膨胀和腐蚀这两种操作是形态...

  • 图像膨胀腐蚀——opencv

    图像腐蚀: 图像二值化,将图像的灰度值根据阈值进行0,1处理得到的图像; 卷积核,对应信号处理中的高低频滤波器。常...

  • C++开发技术人脸识别实战,年薪40W起步 !

    C++ 人脸识别,图像处理有:光线补偿、皮肤颜色建模、膨胀、腐蚀、去掉假区域、再次膨胀、再次腐蚀、得到人脸区域、C...

  • opencv2(2017.5.5)

    1.图像的腐蚀和膨胀 前提:输入的是二值图像腐蚀:用该像素周围像素集合中最小像素替换当前像素;膨胀:用该像素周围像...

  • [CV]图像的开运算、闭运算

    所谓开运算图像开运算是图像依次经过腐蚀、膨胀处理后的过程。图像被腐蚀后,去除了噪声,但是也压缩了图像;接着对腐蚀过...

  • DIP

    常用的图像形态学操作包括膨胀、腐蚀、闭运算、开运算。 膨胀操作会扩大(粗化)图像中物体的轮廓,可以用来弥补(填充)...

  • Python图像处理:图像腐蚀与图像膨胀!

    本篇文章主要讲解Python调用OpenCV实现图像腐蚀和图像膨胀的算法,基础性知识希望对您有所帮助。 1.基础理...

网友评论

      本文标题:图像膨胀腐蚀--膨胀部分优化

      本文链接:https://www.haomeiwen.com/subject/mhhetdtx.html