Search code examples
cimage-processinggeometrysdlpixel

Focusing Of An Image


I need to do some image processing to blur the entire image except the central 30% of that image. I've done the blurring and central focusing part. But the current focusing area is a rectangle which I dont think it is the best possible output since majority of the image processing tools use a circle instead. But how can I achieve that? I tried finding the mid point coordinate of the image by using width / 2 and height / 2. And to find the coordinate of the rest of pixel that will be the circumference of the circle by using newX = centralX + radius * cos D, newY = centralY + radius * sin D, with D being the degree / angle.

SDL_Surface *centralFocus(SDL_Surface * surface)
{
SDL_Surface *tmp = surface;

int width = tmp->w;
int height = tmp->h;
int x, y, radius;
int circumferenceCoordinate[8][2], midpoint[2];
float avg_r = 0, avg_g = 0, avg_b = 0;

Uint32 cur_pixel;

//Initializing Array, Radius & Midpoint, 0 = X-axis, 1 = Y-axis

midpoint[0] = width / 2;
midpoint[1] = height / 2;

radius = sqrt((width * height * 0.3) / 3.142);

for (int i = 0; i < 8; i++){
    for (int j = 0; j < 2; j++){
        circumferenceCoordinate[i][j] = 0;
    }
}

if (SDL_MUSTLOCK(tmp))
    SDL_LockSurface(tmp);

for (x = 0; x < height; x++) {
    for (y = 0; y < width; y++){
        if ((x < midpoint[1] - radius || x > midpoint[1] + radius) || (y < midpoint[0] - radius || y > midpoint[0] + radius))
        {
            //Extracting 9 Pixels

            Uint32 p1 = get_pixel32(tmp, y, x);
            Uint32 p2 = get_pixel32(tmp, y, x + 1);
            Uint32 p3 = get_pixel32(tmp, y, x + 2);

            Uint32 p4 = get_pixel32(tmp, y + 1, x);
            Uint32 p5 = get_pixel32(tmp, y + 1, x + 1);
            Uint32 p6 = get_pixel32(tmp, y + 1, x + 2);

            Uint32 p7 = get_pixel32(tmp, y + 2, x);
            Uint32 p8 = get_pixel32(tmp, y + 2, x + 1);
            Uint32 p9 = get_pixel32(tmp, y + 2, x + 2);

            //Calculating Average For Each Channel

            calculateAvg(avg_r, avg_g, avg_b, p1, p2, p3, p4, p5, p6, p7, p8, p9);

            //Converting RGB Into Pixel

            cur_pixel = SDL_MapRGB(tmp->format, (Uint8)avg_r, (Uint8)avg_g, (Uint8)avg_b);

            //Placing Average Pixel Value

            put_pixel32(tmp, y, x, cur_pixel);
        }
    }
}

if (SDL_MUSTLOCK(tmp))
    SDL_UnlockSurface(tmp);

return tmp;
}

Solution

  • If I understand correctly, within your nested loop, You have an (x,y) coordinate, and you want to determine whether or not this is inside some circle of radius, say fRadius, from a centre point, say (nCX, nCY).

    If so, you need to determine how far the (x,y) pixel is from the centre. So first subtract the centre point from your (x,y) to get the vector from the centre to your point:

    float fDX = (float)(x - nCX);
    float fDY = (float)(y - nCY);
    

    Now find the length of that vector:

    float fLen = sqrt((fDX * fDX) + (fDY * fDY));
    

    Then, your condition for blurring is just:

    if (fLen >= fRadius) 
    {
        // ... blur at this pixel
    }