聚焦图像

Focusing Of An Image

我需要做一些图像处理来模糊整个图像,除了该图像的中心 30%。我已经完成了模糊和中央对焦部分。但是当前的聚焦区域是一个矩形,我认为这不是最好的输出,因为大多数图像处理工具都使用圆形代替。 但我怎样才能做到这一点?我尝试使用 width / 2 和 height / 2 找到图像的中点坐标。并使用 newX = centralX + radius * cos D, newY 找到将成为圆周的其余像素的坐标= centralY + radius * sin D,其中 D 是度数/角度。

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;
}

如果我理解正确,在你的嵌套循环中,你有一个 (x,y) 坐标,你想确定它是否在某个半径的圆内,比如 fRadius,从中心点,比如 (nCX, nCY).

如果是这样,您需要确定 (x,y) 像素距中心的距离。所以首先从你的 (x,y) 中减去中心点以获得从中心到你的点的向量:

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

现在求出该向量的长度:

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

那么,你的模糊条件就是:

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