0

I've read so many of the posts on this and also tried to use the codes mentioned on this forum but they all give incorrect values for the edge filter. I'm quite stumped so I decided to post on this. I hope someone could point out to me where i can improve

{
    int Gx[height][width][3], Gy[height][width][3];
    for(int i=0;i<height;i++) {
        for(int j=0;j<width;j++) {//loop through pixels
            for(int x=0;x<3;x++) {
                for(int y=0;y<3;y+=2) {//loops through adjacent pixels
                    if((i+x-1)>=0 && (i+x-1)<height && (j+y-1)>=0 && (j+y-1)<width) {//checks if the adjacent pixels are within the picture
                        if(abs(x-1)==1) {//checks if pixels are on top or below
                            Gx[i][j][0]+=(y-1)*image[i+x-1][j+y-1].rgbtRed;
                            Gx[i][j][1]+=(y-1)*image[i+x-1][j+y-1].rgbtGreen;
                            Gx[i][j][2]+=(y-1)*image[i+x-1][j+y-1].rgbtBlue;
                        } else {//horizontally aligned with pixel in question
                            Gx[i][j][0]+=2*(y-1)*image[i+x-1][j+y-1].rgbtRed;
                            Gx[i][j][1]+=2*(y-1)*image[i+x-1][j+y-1].rgbtGreen;
                            Gx[i][j][2]+=2*(y-1)*image[i+x-1][j+y-1].rgbtBlue;
                        }//adds red green and blue to new value Gx
                    }
                }
            }
        }
    }

    for(int i=0;i<height;i++) {
        for(int j=0;j<width;j++) {
            for(int x=0;x<3;x+=2) {
                for(int y=0;y<3;y++) {
                    if((i+x-1)>=0 && (i+x-1)<height && (j+y-1)>=0 && (j+y-1)<width) {
                        if(abs(y-1)==1) {
                            Gy[i][j][0]+=(x-1)*image[i+x-1][j+y-1].rgbtRed;
                            Gy[i][j][1]+=(x-1)*image[i+x-1][j+y-1].rgbtGreen;
                            Gy[i][j][2]+=(x-1)*image[i+x-1][j+y-1].rgbtBlue;
                        } else {
                            Gy[i][j][0]+=2*(x-1)*image[i+x-1][j+y-1].rgbtRed;
                            Gy[i][j][1]+=2*(x-1)*image[i+x-1][j+y-1].rgbtGreen;
                            Gy[i][j][2]+=2*(x-1)*image[i+x-1][j+y-1].rgbtBlue;
                        }
                    }
                }
            }
        }
    }

    for(int i=0;i<height;i++) {
        for(int j=0;j<width;j++) {
            int Red=round(sqrt(pow(Gx[i][j][0],2)+pow(Gy[i][j][0],2)));
            if(Red>255)
            Red=255;
            int Green=round(sqrt(pow(Gx[i][j][1],2)+pow(Gy[i][j][1],2)));
            if(Green>255)
            Green=255;
            int Blue=round(sqrt(pow(Gx[i][j][2],2)+pow(Gy[i][j][2],2)));
            if(Blue>255)
            Blue=255;
            image[i][j].rgbtRed=Red;//replaces pixel with new value
            image[i][j].rgbtGreen=Green;
            image[i][j].rgbtBlue=Blue;
        }
    }
    return;
}

You must log in to answer this question.

Browse other questions tagged .