0

I'm trying to implement Hough algorithm using C programming language and 2D raw image.

I have written the code in order to get separate output image for edge detection and Hough transform. When I do edge detection only I'm getting the proper edge detected output image. But here I'm getting the output raw image of size 0KB.I'm not getting where I made mistake. Can anybody please help me to rectify this problem and give idea for further development such as draw line based on Hough space for the below code. Thank you in advance.

#include <stdio.h>
#include <math.h>

#define PI 3.14159265

void
hough_transform(unsigned char out[256][256], unsigned char h_out[][256],
    int w, int h)
{
    int i, j;
    int thetaDeg;
    float thetaRad;
    int rho;
    int distMax;
    int thetamax;

    distMax = sqrt((h * h) + (w * w));

    thetamax = 360;

    int Acc[180][180];

    for (i = 0; i < w; i++) {
        for (j = 0; j < h; j++) {
            if (out[i][j] > 0) {
                for (thetaDeg = 0; thetaDeg < thetamax; ++thetaDeg) {
                    thetaRad = thetaDeg * (PI / 180);
                    rho = i * cos(thetaRad) + j * sin(thetaRad);
                    // find rho value that is closest to this
                    if (distMax < rho) {
                        int min = abs(rho - distMax);
                    }

                    if (distMax <= 1) {
                        // Increment a value in an accumulator array
                        Acc[rho][thetaDeg] += 1;
                    }
                }
            }
        }
    }

    for (rho = 0; rho < distMax; rho++) {
        for (thetaDeg = 0; thetaDeg < thetamax; thetaDeg++) {
            h_out[rho][thetaDeg] = Acc[rho][thetaDeg];
        }
    }
}

void
edge_detect(unsigned char input[256][256], unsigned char out[][256],
    int width, int height)
{
    int in[3][3] = { {1, 2, 1}, {0, 0, 0}, {-1, -2, -1} };
    int pixel;
    int i;
    int j;
    int x;
    int y;

    for (y = 1; y < height - 1; y++) {
        for (x = 1; x < width - 1; x++) {
            pixel = 0;

            for (j = -1; j <= 1; j++) {
                for (i = -1; i <= 1; i++) {
                    pixel += in[j + 1][i + 1] * input[y + j][x + i];
                }
            }

            out[y][x] = (unsigned char) abs(pixel);
        }
    }
}

int
main()
{
    FILE *fp;
    FILE *fp1;
    FILE *fp2;

    unsigned char input[256][256];
    unsigned char out[256][256];
    unsigned char h_out[256][256];

    int width = 256;
    int height = 256;

    fp = fopen("Image.raw", "rb");
    fp1 = fopen("output.raw", "wb");
    fp2 = fopen("h_output.raw", "wb");

    fread(input, 256 * 256, 1, fp);

    edge_detect(input, out, width, height);

    hough_transform(out, h_out, width, height);

    fwrite(out, 256 * 256, 1, fp1);
    fwrite(h_out, 256 * 256, 1, fp2);

    fclose(fp);
    fclose(fp1);
    fclose(fp2);

    return (0);
}
Craig Estey
  • 30,627
  • 4
  • 24
  • 48
  • Probably not your issue, but you do: `int min = abs(rho - distMax);` inside an `if` block and then do nothing with it (`abs` is defined in `stdlib.h`). So, when compiled with warnings enabled (`-Wall`) the statement is flagged with "unused variable" – Craig Estey Feb 09 '23 at 18:56
  • 1
    An output of zero bytes suggests to me that there was a crash in your Hough transformation, very likely when you assign to `Acc` out of bounds. The dimensions of the array are not consistent. For example, `rho` can be negative and `thetaDeg` can be larger than 180. I guess you need a normalization here somewhere. Later, you copy the contents of `Acc` to `h_out`, two arrays of different dimensions, neither of which will accomodate `thetaMax - 1`. – M Oehm Feb 09 '23 at 20:42

0 Answers0