Question

so i have this image processing program where i am using a linear regression algorithm to find a plane that best fits all of the points (x,y,z: z being the pixel color intensity (0-255)

Simply speaking i have this picture of ? x ? dimension. I run this algorithm and i get these A, B, C values. (3 float values)

then i go every pixel in the program and minus the pixel value with mod_val where

mod_val = (-A * x -B * y ) / C

A,B,C are constants while x,y is the pixel location in a x,y plane.

When the dimension of the picture is divisible by 100 its perfect but when its not the picture fractures. The picture itself is the same as the original but there is a diagonal line with color contrast that goes across the picture. The program is supposed to make the pixel color uniform from the center.

I tried running the picture where mod_val = 0 for not divisble by 100 dimension pictures and it copies a new picture perfectly. So i doubt there is a problem with storing and writing the read data in terms of alignment. (fyi this picture is a grey scale 8 bit.bmp)

I have tried changing the A,B,C values but the diagonal remains the same. The color of the image fragments within the diagonals change.

when i run 1400 x 1100 picture it works perfectly with the mod_val equation written above which is the most baffling part.

I spent a lot of time looking for rounding errors. They are virtually all floats. The dimension i used for breaking picture is 1490 x 1170.

here is a gragment of the code where i think a error is occuring:

    int img_row = row_length;
    int img_col = col_length;
    int i = 0;
    float *pfAmultX = new float[img_row];
    for (int x = 0; x < img_row; x++)
    {
        pfAmultX[x] = (A * x)/C;
    }
    for (int y = 0; y < img_col; y++)
    {
        float BmultY = B*y/C;
        for (int x = 0; x < img_row; x++, i++)
        {
            modify_val = pfAmultX[x] + BmultY;
            int temp = (int) data.data[i];
            data.data[i] += (unsigned char) modify_val; 
            if(temp >= 250){
                data.data[i] = 255;
            }
            else if(temp < 0){
                data.data[i] = 0;
            }
        }
    }
    delete[] pfAmultX;

The img_row, img_col is correct according to VS debugger mode

Any help would be greatly appreciated. I've been trying to find this bug for many hours now and my boss is telling me that i can't go back home until i find this bug.....

before algorithm (1400 x 1100, works) imgur.com/pBCBzdH after imgur.com/KiG6Jx2

before (1490 x 1170, demonstrates the problem) imgur.com/zSRUdJi after imgur.com/eXvef88

UPDATE: well i have boiled down the problem as something with the x coordinate after extensive testing.

This is because when i use large A or B values or both (C value is always ~.999) for 1400x1100 it does not create diagonals.

However, for the other image, large B values do not create diagonals but a fairly small - avg A value creates diagonals.

Whats even more, when i test a picture where x is disivible by 100 but y is divisible by 10. the answer is correct.

Was it helpful?

Solution

well in the end i found the solution. It was a problem due to the padding the the bitmap. When the dimension on the x was not divisible by 4 it would use padding which would throw off all of the x coordinates. This also meant that the row_value i received from the bmp header was the same as the dimension but not really the same in reality. I had to make a edit where i had to do: 4 * (row_value_from_bmp_header + 3)/ 4.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top