CS50第4周模糊过滤器,图像模糊,但检查50个值错误
我的支票50有问题,我想知道我所做的是不是真的得到了正确的盒子 模糊的目标是获得所有灰色框的颜色并将其平均 对于每个像素,如果像素不在第一行,我知道我可以有顶框。如果像素不在底行,我知道我可以有底框。相同的逻辑与边缘到 但也存在像素位于角点或边缘的情况CS50第4周模糊过滤器,图像模糊,但检查50个值错误,c,cs50,C,Cs50,我的支票50有问题,我想知道我所做的是不是真的得到了正确的盒子 模糊的目标是获得所有灰色框的颜色并将其平均 对于每个像素,如果像素不在第一行,我知道我可以有顶框。如果像素不在底行,我知道我可以有底框。相同的逻辑与边缘到 但也存在像素位于角点或边缘的情况 void blur(int height, int width, RGBTRIPLE image[height][width]) { RGBTRIPLE arrayCopy[height][width]; memcpy(arr
void blur(int height, int width, RGBTRIPLE image[height][width])
{
RGBTRIPLE arrayCopy[height][width];
memcpy(arrayCopy, image, sizeof arrayCopy);
for (int i = 0; i < height; i++)
{
for (int w = 0; w < width; w++)
{
int counter = 0;
int totalRed = 0;
int totalBlue = 0;
int totalGreen = 0;
if (!(w == 0))
{
int leftBoxRed = arrayCopy[i][w-1].rgbtRed;
int leftBoxBlue = arrayCopy[i][w-1].rgbtBlue;
int leftBoxGreen = arrayCopy[i][w-1].rgbtGreen;
totalRed = totalRed + leftBoxRed;
totalBlue = totalBlue + leftBoxBlue;
totalGreen = totalGreen + leftBoxGreen;
counter++;
//printf("%i\n", leftBoxRed);
}
if (!(w == width-1))
{
int rightBoxRed = arrayCopy[i][w+1].rgbtRed;
int rightBoxBlue = arrayCopy[i][w+1].rgbtBlue;
int rightBoxGreen = arrayCopy[i][w+1].rgbtGreen;
totalRed = totalRed + rightBoxRed;
totalBlue = totalBlue + rightBoxBlue;
totalGreen = totalGreen + rightBoxGreen;
counter++;
}
if (!(i == 0))
{
if (!(w == 0))
{
int leftTopBoxRed = arrayCopy[i-1][w-1].rgbtRed;
int leftTopBoxBlue = arrayCopy[i-1][w-1].rgbtBlue;
int leftTopBoxGreen = arrayCopy[i-1][w-1].rgbtGreen;
totalRed = totalRed + leftTopBoxRed;
totalBlue = totalBlue + leftTopBoxBlue;
totalGreen = totalGreen + leftTopBoxGreen;
counter++;
}
if (!(w == width - 1))
{
int rightTopBoxRed = arrayCopy[i-1][w+1].rgbtRed;
int rightTopBoxBlue = arrayCopy[i-1][w+1].rgbtBlue;
int rightTopBoxGreen = arrayCopy[i-1][w+1].rgbtGreen;
totalRed = totalRed + rightTopBoxRed;
totalBlue = totalBlue + rightTopBoxBlue;
totalGreen = totalGreen + rightTopBoxGreen;
counter++;
}
int topBoxRed = arrayCopy[i-1][w].rgbtRed;
int topBoxBlue = arrayCopy[i-1][w].rgbtBlue;
int topBoxGreen = arrayCopy[i-1][w].rgbtGreen;
totalRed = totalRed + topBoxRed;
totalBlue = totalBlue + topBoxBlue;
totalGreen = totalGreen + topBoxGreen;
counter++;
}
if (!(i == height-1))
{
if (!(w == 0))
{
int leftBottomBoxRed = arrayCopy[i+1][w-1].rgbtRed;
int leftBottomBoxBlue = arrayCopy[i+1][w-1].rgbtBlue;
int leftBottomBoxGreen = arrayCopy[i+1][w-1].rgbtGreen;
totalRed = totalRed + leftBottomBoxRed;
totalBlue = totalBlue + leftBottomBoxBlue;
totalGreen = totalGreen + leftBottomBoxGreen;
counter++;
}
if (!(w == width - 1))
{
int rightBottomBoxRed = arrayCopy[i+1][w+1].rgbtRed;
int rightBottomBoxBlue = arrayCopy[i+1][w+1].rgbtBlue;
int rightBottomBoxGreen = arrayCopy[i+1][w+1].rgbtGreen;
totalRed = totalRed + rightBottomBoxRed;
totalBlue = totalBlue + rightBottomBoxBlue;
totalGreen = totalGreen + rightBottomBoxGreen;
counter++;
}
int bottomBoxRed = arrayCopy[i+1][w].rgbtRed;
int bottomBoxBlue = arrayCopy[i+1][w].rgbtBlue;
int bottomBoxGreen = arrayCopy[i+1][w].rgbtGreen;
counter++;
totalRed = totalRed + bottomBoxRed;
totalBlue = totalBlue + bottomBoxBlue;
totalGreen = totalGreen + bottomBoxGreen;
}
float averageRed = totalRed / (float) counter;
float averageBlue = totalBlue / (float) counter;
float averageGreen = totalGreen / (float) counter;
image[i][w].rgbtRed = (int) roundf(averageRed);
image[i][w].rgbtBlue = (int) roundf(averageBlue);
image[i][w].rgbtGreen = (int) roundf(averageGreen);
//printf("%i\n", (int) roundf(averageRed));
}
}
return;
}
void blur(int-height、int-width、rgb三重图像[height][width])
{
RGB三重阵列副本[高度][宽度];
memcpy(阵列副本、图像、阵列副本大小);
对于(int i=0;i
原始图像
我的图像输出
在第一个失败的rest结果中(顺便说一句,请避免在问题中添加文本图像),您应该计算中间像素的红色分量的值127。你算了128 此图像中九个像素的红色分量如下所示:
10 40 70
110 120 130
200 220 240
如果,正如你在问题顶部所说,你应该计算周围八个像素的平均值
for (int x=0; x<width; x++) {
for (int y=0; y<height; y++) {
int counter = 0;
int totalr=0, totalg=0, totalb=0;
for (int dx=-1; dx<=1; dx++) {
if (x+dx < 0 || x+dx >= width) continue;
for (int dy=-1; dy<=1; dy++) {
if (y+dy >=0 && y+dy < height) {
totalr += image[y+dy][x+dx].rgbtRed;
totalg += image[y+dy][x+dx].rgbtGreen;
totalb += image[y+dy][x+dx].rgbtBlue;
counter++;
}
}
}
result[y][x].rgbtRed = roundf(totalr / (float)counter);
result[y][x].rgbtGreen = roundf(totalg / (float)counter);
result[y][x].rgbtBlue = roundf(totalb / (float)counter);
}
}