Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Image Manipulation - is this a for-loop issue?

I have an image that I'm trying to blur out, but it ends up looking very weird indeed:

enter image description here

What I DO is this: I take each pixel, and I average its value on a color-by-color basis with the values of all adjacent pixels.

Or so I think. But there's clearly an error in there, and I suspect there's a problem with my for-loops but for the life of me I cannot find out what is actually going wrong.

In particular, step five (the output step) shows that the image is still ordered - if I output the left image on the right side, rather than the blur-masked image, the pixels are still in the correct order.

    try
{
    // STEP ONE: MAKE MEMORY AVAILABLE FOR IMAGE
    int ***image;
    image = new int**[m_nSizeX];
    for(int i = 0; i < m_nSizeX; ++i)
    {
        image[i] = new int*[m_nSizeY];
        for(int j = 0; j < m_nSizeY; ++j)
        {
            image[i][j] = new int[nrPixels];// image[x][y][z] is now a pointer to an int

        }
    }

    // STEP TWO: MAKE MEMORY AVAILABLE FOR IMAGE MASK
    int ***mask;
    mask = new int**[m_nSizeX];
    for(int i = 0; i < m_nSizeX; ++i)
    {
        mask[i] = new int*[m_nSizeY];
        for(int j = 0; j < m_nSizeY; ++j)
        {
            mask[i][j] = new int[nrPixels];// mask[x][y][z] is now a pointer to an int

        }
    }

    //STEP THREE: COPY IMAGE INTO MEMORY

    unsigned long lOffset = 0;
    for(long i=0; i<m_nSizeX ; i++)
    {
        for(long j=0; j<m_nSizeY ; j++)
        {
            for(int k=0; k<(nrPixels) ; k++)
            {
                image[i][j][k] = *(reinterpret_cast<unsigned char*>(m_pcMemOrg + lOffset) );
                lOffset++;
            }
        }
    }

    // STEP FOUR: BLUR IMAGE

    for(long i=0; i<m_nSizeX ; i++)
    {
        for(long j=0; j<m_nSizeY ; j++)
        {
            for(int k=0; k<(nrPixels) ; k++)
            {
                // INSERT BLURRING FUNCTION HERE (New value = Old value averaged with adjacent pixels)

                if(k != 2) // 0 = blue, 1 = green, 2 = red;
                {
                    mask[i][j][k] = 0;
                }
                else


                if(i==0 && j==0)// (0,0) Corner Pixel
                {
                    mask[i][j][k] = (image[i][j][k]+image[i+1][j][k]+image[i][j+1][k]+image[i+1][j+1][k])/4;
                }
                else if(i==0 && j==(m_nSizeY-1))// (0,yMax) Corner Pixel
                {
                    mask[i][j][k] = (image[i][j][k]+image[i+1][j][k]+image[i][j-1][k]+image[i+1][j-1][k])/4;
                }
                else if(i==(m_nSizeX-1) && j==0)// (xMax,0) Corner Pixel
                {
                    mask[i][j][k] = (image[i][j][k]+image[i-1][j][k]+image[i][j+1][k]+image[i-1][j+1][k])/4;
                }
                else if(i==(m_nSizeX-1) && j==(m_nSizeY-1))// (xMax,yMax) Corner Pixel
                {
                    mask[i][j][k] = (image[i][j][k]+image[i-1][j][k]+image[i][j-1][k]+image[i-1][j-1][k])/4;
                }
                else if(i==0)// (0,---) Edge Pixels
                {
                    mask[i][j][k] = (image[i][j][k]+image[i][j+1][k]+image[i+1][j+1][k]+image[i+1][j][k]+image[i+1][j-1][k]+image[i][j-1][k])/6;
                }
                else if(j==0)// (---,0) Edge Pixels
                {
                    mask[i][j][k] = (image[i][j][k]+image[i-1][j][k]+image[i-1][j+1][k]+image[i][j+1][k]+image[i+1][j+1][k]+image[i+1][j][k])/6;                    
                }
                else if(i==(m_nSizeX-1))// (xMax,---) Edge Pixels
                {
                    mask[i][j][k] = (image[i][j][k]+image[i][j-1][k]+image[i-1][j-1][k]+image[i-1][j][k]+image[i-1][j+1][k]+image[i][j+1][k])/6;
                }
                else if(j==(m_nSizeY-1))// (---,yMax) Edge Pixels
                {
                    mask[i][j][k] = (image[i][j][k]+image[i+1][j][k]+image[i+1][j-1][k]+image[i][j-1][k]+image[i-1][j-1][k]+image[i-1][j][k])/6;
                }
                else // Mid-Image Pixels
                {
                    mask[i][j][k] = (image[i][j][k]+image[i][j+1][k]+image[i+1][j+1][k]+image[i+1][j][k]+image[i+1][j-1][k]+image[i][j-1][k]+image[i-1][j-1][k]+image[i-1][j][k]+image[i-1][j+1][k])/9;
                }
            }
        }
    }

    //STEP FIVE: OUTPUT BLURRED IMAGE
    lOffset = 0;

    for(long i=0; i<m_nSizeX ; i++)
    {
        for(long j=0; j<m_nSizeY ; j++)
        {
            for(int k=0; k<(nrPixels) ; k++)
            {
                *(reinterpret_cast<unsigned char*>(m_pcMemInv + lOffset) ) = mask[i][j][k];
                //*(reinterpret_cast<unsigned char*>(m_pcMemInv + lOffset) ) = image[i][j][k];
                lOffset++;
            }
        }
    }


    // STOP USING IMAGE MEMORY NOW

    for (int i = 0; i < m_nSizeX; ++i) {
        for (int j = 0; j < m_nSizeY; ++j)
            delete [] image[i][j];

        delete [] image[i];
    }
    delete [] image;

    // STOP USING MASK MEMORY NOW
    for (int i = 0; i < m_nSizeX; ++i) {
        for (int j = 0; j < m_nSizeY; ++j)
            delete [] mask[i][j];

        delete [] mask[i];
    }
    delete [] mask;
}
catch( ... )
{

}
like image 803
shieldfoss Avatar asked Dec 02 '25 02:12

shieldfoss


1 Answers

When using multi-dimensional indexing, usually the first index is y, the second x and the third red/green/blue - you use a nonstandard transposed layout with i, j and k, where i seems to mean a horizontal index (seeing that you compare it to m_nSizeX).

I guess your image is getting transposed when you copy it the first time, transformed in a mysterious way, and transposed back when you copy it the second time; i cannot guess the details, but it's enough to advise you to just get the dimensions right (swap i and j).

By the way, calling coordinates the normal names x and y (instead of i and j, or maybe j and i?) helps.

like image 94
anatolyg Avatar answered Dec 03 '25 19:12

anatolyg



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!