我需要进行一些图像处理来模糊整个图像(除了图像的中央 30%)。我已经完成了模糊和中央对焦部分。但当前的聚焦区域是一个矩形,我认为这不是最好的输出,因为大多数图像处理工具都使用圆形。 但我怎样才能做到这一点呢?我尝试使用 width/2 和 height/2 查找图像的中点坐标。并使用 newX =centralX + radius * cos D, newY 查找将成为圆的周长的其余像素的坐标= centerY + 半径 * 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) 中减去中心点,以获得从中心到您的点的 vector :
float fDX = (float)(x - nCX);
float fDY = (float)(y - nCY);
现在找到该 vector 的长度:
float fLen = sqrt((fDX * fDX) + (fDY * fDY));
那么,你的模糊条件就是:
if (fLen >= fRadius)
{
// ... blur at this pixel
}
关于c - 图像的聚焦,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32471151/