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

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

作者: 教训小磊 | 来源:发表于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;
    }
    

    相关文章

      网友评论

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

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