实施Sobel滤波器时的奇怪结果

时间:2013-06-02 20:04:08

标签: c image-processing

我一直在学习计算机视觉,并想在C中实现一些简单的技术。对于第一种技术,我正在做Sobel边缘检测滤波器。我理解它是如何工作的,所以我认为编码应该相当容易,但我得到了非常奇怪的结果。

我使用的是以下图片:

Box

并将其作为结果

Filtered box

新结果!:

Filtered box...a little more correct

应该注意的是,我使用.ppm图像格式(链接是jpgs,因为我找不到支持.ppm的图像主机)

无论如何,这是我的代码中实现Sobel的部分:

/**********************************************************
This program takes in an image file and applies the Sobel
Filter edge detection technique to it.
**********************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "ppmReader.h"

void sobelFilter(){


    //Sobel kernels dx (horizontal) and dy (vertical)
    int horizFilter[3][3] = {{ 1,   0,  -1},
                             { 2,   0,  -2},
                             { 1,   0,  -1}};
    int vertFilter[3][3]  = {{ 1,   2,   1},
                             { 0,   0,   0},
                             {-1,  -2,  -1}};
    int pixVal = 0; 
    int horizPixVal = 0;
    int vertPixVal = 0;
    int x, y, i, j;

    //Quick check to make sure dimensions are correct
    printf("Using a Width of: %d\n", width);
    printf("Using a Height of: %d\n\n", height);

    //Start filtering process here
    for(x = 0; x < width; x++){
        for(y = 0; y < height; y++){
            pixVal = 0;
            horizPixVal = 0;
            vertPixVal = 0;
            if(!((x == 0) || (x == width-1) || (y == 0) || (y == height-1))){           //If the current pixel is along the border, ignore it and set to zero
                for(i = -1; i <= 1; i++){                                               //because the kernel does not align to it
                    for(j = -1; j <= 1; j++){
                        horizPixVal += (int)(image[y + j][x + i][0]) * horizFilter[i + 1][j + 1];       //Only need to focus on one of the RGB values since the output is
                        vertPixVal  += (int)(image[y + j][x + i][0]) * vertFilter[i + 1][j + 1];        //greyscale and all three values are the same
                    }
                }
            }
            pixVal = sqrt((horizPixVal * horizPixVal) + (vertPixVal * vertPixVal));     //Calculate magnitude
            pixVal = sqrt(horizPixVal * horizPixVal);
            if(pixVal > 255) pixVal = 255;                                              //Clamp value within 8-bit range
            filteredImage[y][x][0] = (unsigned char)pixVal;                             
        }
    }
}

以下是读取.ppm文件的代码:

unsigned char image[MAX_IMAGE_HEIGHT][MAX_IMAGE_WIDTH][3];
unsigned char filteredImage[MAX_IMAGE_HEIGHT][MAX_IMAGE_WIDTH][3];

void readPPMImageData(){
    char fileName[MAX_NAME];
    char imageBuff[MAX_BUFF];
    width = 0;
    height = 0;
    maxColor = 0;
    int x;
    int y;

    FILE* file;

    printf("------------------------------------------------------------\n");
    printf("Now attempting to read in the .ppm image file data...\n");
    printf("------------------------------------------------------------\n\n");
    printf("What is the image file name (*.ppm)?  : ");
    scanf("%s", fileName);
    file = fopen(fileName, "rb");               //open the file specified by the user in binary read mode
    if(file == NULL){                           //but if the file was not found, terminate program
        printf("\nThe file %s could not be found! Terminating program...\n", fileName);
        exit(1);
    }

    //The first step is to read in the file type and check it agains P6 (file type of .ppm images)
    fgets(imageBuff, MAX_BUFF, file);
    if(imageBuff[0] != 'P' || imageBuff[1] != '6'){
        printf("\nInvalid image type! Acceptable type is: %s --- Received type is: %c%c\n\n", "P6", imageBuff[0], imageBuff[1]);
    }
    printf("Magic Number is: %c%c\n", imageBuff[0], imageBuff[1]);

    while(width == 0 || height == 0){
        fgets(imageBuff, MAX_BUFF, file);
        if(imageBuff[0] != '#') {
            sscanf(imageBuff, "%d %d", &width, &height);
        }
    }
    printf("Width is: %d\n", width);
    printf("Height is: %d\n", height);
    //if(feof(file)){
    //
    //}

    while(maxColor == 0){
        fgets(imageBuff, MAX_BUFF, file);
        if(imageBuff[0] != '#') {
            sscanf(imageBuff, "%d", &maxColor);
        }
    }
    printf("Maximum color value is: %d\n", maxColor);

    for(x = 0; x < width; x++){
        for(y = 0; y < height; y++){
            image[y][x][0] = (unsigned char)fgetc(file); //Get Red value
            image[y][x][1] = (unsigned char)fgetc(file); //Get Green value
            image[y][x][2] = (unsigned char)fgetc(file); //Get Blue value
        }
    }
    printf("Finished reading image data!\n\n");

    fclose(file);
}

以下是过滤后创建新.ppm文件的代码:

void createPPMImage(){
    char fileName[MAX_NAME]; 
    FILE* file;
    int x;
    int y;

    printf("------------------------------------------------------------\n");
    printf("Now attempting to create new .ppm image file...\n");
    printf("------------------------------------------------------------\n\n");
    printf("What is the name of the output image file (*.ppm)?  : ");
    scanf("%s", fileName);

    printf("Width is: %d\n", width);
    printf("Height is: %d\n", height);
    printf("Maximum color value is: %d\n", maxColor);

    file = fopen(fileName, "wb");
    fputs("P6\n", file);
    fprintf(file, "%d %d\n", width, height);
    fprintf(file, "%d\n", maxColor);

    for(x = 0; x < width; x++){
        for(y = 0; y < height; y++){
            fputc(filteredImage[y][x][0], file); //Write Red value
            fputc(filteredImage[y][x][0], file); //Write Green value
            fputc(filteredImage[y][x][0], file); //Write Blue value
        }
    }
    printf("Finished creating new filtered image!\n\n");

    fclose(file);
}

我100%确定问题不在于图像的读取或写入,因为我在没有应用过滤器的情况下测试了这些功能,只有在使用上述功能后才会出现问题。

任何帮助都值得赞赏,因为据我所知,索引/公式似乎已正确实现,但显然不是这样。

编辑:正如Dave和其他人所指出的那样,我不再100%确定错误是在Sobel功能范围内而且看起来这只是我在使用时所犯的一些索引错误.ppm格式。我继续发布我的.ppm阅读器/写入器功能的代码以及我在应用下面的anatolyg提出的[y] [x] [color]方案后得到的新结果。我很抱歉,如果我的帖子太长,如果是,请告诉我,因为这是我的第一篇文章,我不完全确定什么是正确的。

1 个答案:

答案 0 :(得分:2)

图片通常首先使用y坐标和x秒进行索引,如下所示:

... image[y + j][x + i] ...

这是一种让人们在处理C中的图像时不会感到困惑的惯例。不幸的是,它与Matlab使用的相矛盾,所以我只希望你在C中完成所有这些。

此外,PPM format specification表示红色/绿色/蓝色值是交错的,因此“颜色平面”必须是最后一个索引:

... image[y + j][x + i][0] ...

除非在将输入文件加载到内存中时对输入文件进行了一些重新排序。您没有显示从文件中读取的代码,因此很难知道它是否进行了任何重新排序。


添加:读取和写入文件应遵循光栅排序,即在前一行之前完成一行像素:

for(y = 0; y < height; y++){
    for(x = 0; x < width; x++){
        ...
    }
}

还建议以这种方式进行处理;这不是绝对必须的,但它会减少混淆,并且可能使您的处理速度更快(通过更有效地使用CPU缓存)。