gpt4 book ai didi

c - 用 C 模糊图像

转载 作者:行者123 更新时间:2023-12-05 01:14:16 24 4
gpt4 key购买 nike

我正在尝试根据我正在遵循的教程模糊图像。这是说明。

Blur There are a number of ways to create the effect of blurring or softening an image. For this problem, we’ll use the “box blur,” which works by taking each pixel and, for each color value, giving it a new value by averaging the color values of neighboring pixels.

Consider the following grid of pixels, where we’ve numbered each pixel.

a grid of pixels

The new value of each pixel would be the average of the values of all of the pixels that are within 1 row and column of the original pixel (forming a 3x3 box). For example, each of the color values for pixel 6 would be obtained by averaging the original color values of pixels 1, 2, 3, 5, 6, 7, 9, 10, and 11 (note that pixel 6 itself is included in the average). Likewise, the color values for pixel 11 would be be obtained by averaging the color values of pixels 6, 7, 8, 10, 11, 12, 14, 15 and 16.

enter image description here

For a pixel along the edge or corner, like pixel 15, we would still look for all pixels within 1 row and column: in this case, pixels 10, 11, 12, 14, 15, and 16.```

所以为了解决这个问题,我想出了这个解决方案。

for each row
for each column
add the pixel if upper left exists
add the pixel if directly above exists
add the pixel if upper right exists
add the pixel if right exists
add the pixel if directly below exists
add the pixel if left exists
divide by times added

我已经输入了代码 -

void blur(int height, int width, RGBTRIPLE image[height][width])
{
int red;
int green;
int blue;
int counter = 0;

for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
if (i + 1 && j - 1)
{
red = image[i][j].rgbtRed + image[i + 1][j - 1].rgbtRed;
green = image[i][j].rgbtGreen + image[i + 1][j - 1].rgbtGreen;
blue = image[i][j].rgbtBlue + image[i + 1][j - 1].rgbtBlue;
counter++;
}
if (j + 1)
{
red = image[i][j].rgbtRed + image[i][j + 1].rgbtRed;
green = image[i][j].rgbtGreen + image[i][j + 1].rgbtGreen;
blue = image[i][j].rgbtBlue + image[i][j + 1].rgbtBlue;
counter++;
}
if (i + 1 && j + 1)
{
red = image[i][j].rgbtRed + image[i + 1][j + 1].rgbtRed;
green = image[i][j].rgbtGreen + image[i + 1][j + 1].rgbtGreen;
blue = image[i][j].rgbtBlue + image[i + 1][j + 1].rgbtBlue;
counter++;
}
if (i + 1)
{
red = image[i][j].rgbtRed + image[i + 1][j].rgbtRed;
green = image[i][j].rgbtGreen + image[i + 1][j].rgbtGreen;
blue = image[i][j].rgbtBlue + image[i + 1][j].rgbtBlue;
counter++;
}
if (j - 1)
{
red = image[i][j].rgbtRed + image[i][j - 1].rgbtRed;
green = image[i][j].rgbtGreen + image[i][j - 1].rgbtGreen;
blue = image[i][j].rgbtBlue + image[i][j - 1].rgbtBlue;
counter++;
}
if (i - 1)
{
red = image[i][j].rgbtRed + image[i - 1][j].rgbtRed;
green = image[i][j].rgbtGreen + image[i - 1][j].rgbtGreen;
blue = image[i][j].rgbtBlue + image[i - 1][j].rgbtBlue;
counter++;
}
image[i][j].rgbtRed = red/counter;
image[i][j].rgbtGreen = green/counter;
image[i][j].rgbtBlue = blue/counter;
}
}
return;
}

似乎我收到错误提示

~/pset4/filter/ $ ./filter -b images/courtyard.bmp  test.bmp                                                                                                                                                        
helpers.c:84:45: runtime error: index -1 out of bounds for type 'RGBTRIPLE [width]'
helpers.c:85:49: runtime error: index -1 out of bounds for type 'RGBTRIPLE [width]'
helpers.c:86:47: runtime error: index -1 out of bounds for type 'RGBTRIPLE [width]'
helpers.c:112:45: runtime error: index -1 out of bounds for type 'RGBTRIPLE [width]'
helpers.c:113:49: runtime error: index -1 out of bounds for type 'RGBTRIPLE [width]'
helpers.c:114:47: runtime error: index -1 out of bounds for type 'RGBTRIPLE [width]'
UndefinedBehaviorSanitizer:DEADLYSIGNAL
==4657==ERROR: UndefinedBehaviorSanitizer: SEGV on unknown address 0x7fd1249a490a (pc 0x00000042b7ea bp 0x7ffd589ca4f0 sp 0x7ffd589c90f0 T4657)
==4657==The signal is caused by a READ memory access.
#0 0x42b7e9 (/home/ubuntu/pset4/filter/filter+0x42b7e9)
#1 0x422ed2 (/home/ubuntu/pset4/filter/filter+0x422ed2)
#2 0x7fd123897b96 (/lib/x86_64-linux-gnu/libc.so.6+0x21b96)
#3 0x402d69 (/home/ubuntu/pset4/filter/filter+0x402d69)

对我来说,代码似乎并没有真正解决问题。它在第一次迭代后就失败了。因为 ij 值变为负数,因此并没有真正服务于检查附近像素的存在。

您能否分享一两点关于这里应该做什么以更好地了解程序应该如何检测周围像素?

谢谢!

最佳答案

在 C 中,非零被认为是真,j=0 处的 j-1 是 -1 真。

void blur(int height, int width, RGBTRIPLE image[height][width])

{

int red;

int green;
int blue;
int counter = 0;
RGBTRIPLE **new_image = (RGBTRIPLE**) malloc(sizeof(RGBTRIPLE*)*height);

for(int i=0;i<height;i++)
new_image[i]=((RGBTRIPLE*) malloc(sizeof(RGBTRIPLE)*width);

for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
red=green=blue=0;
counter=0;
if (i + 1 <height && j - 1 >=0)
{
red += image[i][j].rgbtRed + image[i + 1][j - 1].rgbtRed;
green += image[i][j].rgbtGreen + image[i + 1][j - 1].rgbtGreen;
blue += image[i][j].rgbtBlue + image[i + 1][j - 1].rgbtBlue;
counter++;
}
if (j + 1<width)
{
red += image[i][j].rgbtRed + image[i][j + 1].rgbtRed;
green += image[i][j].rgbtGreen + image[i][j + 1].rgbtGreen;
blue += image[i][j].rgbtBlue + image[i][j + 1].rgbtBlue;
counter++;
}
if (i + 1 <height && j + 1 <width)
{
red += image[i][j].rgbtRed + image[i + 1][j + 1].rgbtRed;
green += image[i][j].rgbtGreen + image[i + 1][j + 1].rgbtGreen;
blue += image[i][j].rgbtBlue + image[i + 1][j + 1].rgbtBlue;
counter++;
}
if (i + 1<height)
{
red += image[i][j].rgbtRed + image[i + 1][j].rgbtRed;
green += image[i][j].rgbtGreen + image[i + 1][j].rgbtGreen;
blue += image[i][j].rgbtBlue + image[i + 1][j].rgbtBlue;
counter++;
}
if (j - 1>=0)
{
red += image[i][j].rgbtRed + image[i][j - 1].rgbtRed;
green += image[i][j].rgbtGreen + image[i][j - 1].rgbtGreen;
blue += image[i][j].rgbtBlue + image[i][j - 1].rgbtBlue;
counter++;
}
if (i - 1>=0)
{
red += image[i][j].rgbtRed + image[i - 1][j].rgbtRed;
green += image[i][j].rgbtGreen + image[i - 1][j].rgbtGreen;
blue += image[i][j].rgbtBlue + image[i - 1][j].rgbtBlue;
counter++;
}
new_image[i][j].rgbtRed = red/counter;
new_image[i][j].rgbtGreen = green/counter;
new_image[i][j].rgbtBlue = blue/counter;
}
}
return;
}

关于c - 用 C 模糊图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58926559/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com