聚焦图像

时间:2015-09-09 04:53:24

标签: c image-processing geometry sdl pixel

我需要做一些图像处理来模糊整个图像,除了该图像的中心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;
}

1 个答案:

答案 0 :(得分:2)

如果我理解正确,在你的嵌套循环中,你有一个(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
}