纯手写边缘检测算法

前言

由于要在开发板上实现一些基础的图像处理算法,本想着直接使用opencv,怎奈何移植后占用空间太大,放弃了这个想法。于是乎自己手lu图像处理算法代码,顺便复习一下数字图像处理的相关知识。

思路

定义卷积核,使用卷积核进行卷积运算。存读图片使用了stbi这个库,是一个独立的头文件,直接拷贝到工程中就可以使用,非常方便。LVGL项目中使用这个库进行图片存取简直太棒了,省下了去复习png文件结构的时间。

代码

废话不多说,直接上边缘检测代码。

#define STB_IMAGE_IMPLEMENTATION
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "stb_image.h"
#include <stdio.h>
#include "stb_image_write.h"
#include <stdlib.h>

int sobelFilter(int *pixels, int width, int height, int x, int y) {
    int kernelX[3][3] = {
        {-1, 0, 1},
        {-2, 0, 2},
        {-1, 0, 1}
    };

    int kernelY[3][3] = {
        {-1, -2, -1},
        {0, 0, 0},
        {1, 2, 1}
    };

    int sumX = 0, sumY = 0;

    for (int i = -1; i <= 1; i++) {
        for (int j = -1; j <= 1; j++) {
            int pixelX = x + i;
            int pixelY = y + j;

            if (pixelX >= 0 && pixelX < width && pixelY >= 0 && pixelY < height) {
                int index = pixelY * width + pixelX;
                int gray = (pixels[index] >> 16) & 0xFF; 
                sumX += kernelX[i + 1][j + 1] * gray;
                sumY += kernelY[i + 1][j + 1] * gray;
            }
        }
    }

    return abs(sumX) + abs(sumY); // 使用Sobel算子计算边缘强度
}



void convertToGrayImage(unsigned char *image, int *grayImage, int width, int height, int channels) {
    for (int i = 0; i < width * height; i++) {
        int r = image[i * 4];
        int g = image[i * 4 + 1];
        int b = image[i * 4 + 2];
        grayImage[i] = (r << 16) | (g << 8) | b;
    }
}

void applySobelFilter(int *grayImage, unsigned char *edges, int width, int height) {
    for (int y = 0; y < height; y++) {
        for (int x = 0; x < width; x++) {
            int index = y * width + x;
            edges[index] = sobelFilter(grayImage, width, height, x, y);
        }
    }
}

void createOutline(unsigned char *edges, unsigned char *output, int width, int height) {
    for (int i = 0; i < width * height; i++) {
    
        output[i * 4] = 0;
        output[i * 4 + 1] = 0;
        output[i * 4 + 2] = edges[i] > 128 ? 255 : 0;
        output[i * 4 + 3] = edges[i] > 128 ? 128 : 0;
    }
}

void processImage(const char *inputFile, const char *outputFile) {
    int width, height, channels;
    unsigned char *image = stbi_load(inputFile, &width, &height, &channels, 0);
    if (!image) {
        printf("Failed to load image.\n");
        return;
    }

    int *grayImage = (int *)malloc(width * height * sizeof(int));
    convertToGrayImage(image, grayImage, width, height, channels);

    unsigned char *edges = (unsigned char *)malloc(width * height);
    applySobelFilter(grayImage, edges, width, height);

    unsigned char *output = (unsigned char *)malloc(width * height * 4);
    createOutline(edges, output, width, height);

    stbi_write_png(outputFile, width, height, 4, output, width * 4);

    stbi_image_free(image);
    free(grayImage);
    free(edges);
    free(output);

    printf("Image processing complete.\n");
}

int main() {
    processImage("tomato.png", "outline.png");
    return 0;
}

在这里插入图片描述
在这里插入图片描述

以上代码验证通过,在LVGL里面配合定时器进行闪烁,则可以实现一个食材选中的效果。
在这里插入图片描述

  • 6
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值