CS50X PSET4 BLUR-提取原始价值的问题
在过去的几天里,我一直在努力实施模糊功能,并且处于崩溃的边缘。
我认为问题可能在于指针和地址的使用情况,但我不确定。由于某种原因,每当我试图获得循环在上一个循环中发生变化的像素时,我都会得到完全不同的东西。
CS50检查器的输出:
第一行:(10,20,30),(40,50,60),(70,80,90)
第二行:(110,130,140),(120,140,150),(130,150,160)
第三行:(200,210,220),(220,230,240),(240,250,255)
块00
R:10 G:20 B:30(检查中间主块)
R:110 g:130 B:140(检查南块)
R:120 G:140 B:150(检查东南块)
R:40 g:50 B:60(检查东块)
块01
R:40 G:50 B:60(检查中间主块)
R:120 g:140 B:150(检查南块)
R:110 G:130 B:140(检查西南块)
R:130 G:150 B:160(检查东南块)
r:49 g:247 B:0 (假设是西部街区(10,20,30) - 不确定这里发生了什么)
R:70 g:80 B:90(检查东块)
块0 2
R:70G:80B:90
R:130G:150B:160
R:120G:140B:150
r:0g:0b:...
我在下面封装了我的代码:
// Blur image
void blur(int height, int width, RGBTRIPLE image[height][width])
{
RGBTRIPLE temp[height][width];
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
temp[i][j] = image[i][j];
}
}
for (int h = 0; h < height; h++)
{
for (int w = 0; w < width; w++)
{
printf("Block %i %i", h, w);
int totalRed = 0;
int totalGreen = 0;
int totalBlue = 0;
float cellCounter = 0.0;
//adds rgb for designated cell
rgbAdd(&temp[h][w], &totalRed, &totalGreen, &totalBlue);
cellCounter += 1.0;
//north row has to be at least 0
if ((h - 1) >= 0)
{
//tally up north cell
rgbAdd(&temp[h - 1][w], &totalRed, &totalGreen, &totalBlue);
cellCounter += 1.0;
//east col has to be at least 0 for northeast corner to exist
if ((w - 1) >= 0)
{
//tally up northeast corner
rgbAdd(&temp[h - 1][w - 1], &totalRed, &totalGreen, &totalBlue);
cellCounter += 1.0;
}
//west col has to be (width - 1) or less for northwest corner to exist
if ((w + 1) <= (width - 1))
{
//tally up northwest corner
rgbAdd(&temp[h - 1][w + 1], &totalRed, &totalGreen, &totalBlue);
cellCounter += 1.0;
}
}
//south row has to be (height -1) or less
if ((h + 1) <= height - 1)
{
//tally up south cell
rgbAdd(&temp[h + 1][w], &totalRed, &totalGreen, &totalBlue);
cellCounter += 1.0;
//east col has to be at least 0 for southeast corner to exist
if ((w - 1) >= 0)
{
//tally up northeast corner
rgbAdd(&temp[h + 1][w - 1], &totalRed, &totalGreen, &totalBlue);
cellCounter += 1.0;
}
//west col has to be less than (width - 1) for southwest corner to exist
if ((w + 1) <= (width - 1))
{
//tally up northwest corner
rgbAdd(&temp[h + 1][w + 1], &totalRed, &totalGreen, &totalBlue);
cellCounter += 1.0;
}
}
//west col has to be at least 0
if ((w - 1) >= 0)
{
//tally up west point
rgbAdd(&temp[h - 1][w - 1], &totalRed, &totalGreen, &totalBlue);
cellCounter += 1.0;
}
//east col has to be equal to or less than (width - 1)
if ((w + 1) <= (width - 1))
{
//tally up east point
rgbAdd(&temp[h][w + 1], &totalRed, &totalGreen, &totalBlue);
cellCounter += 1.0;
}
image[h][w].rgbtRed = round(totalRed / cellCounter);
image[h][w].rgbtGreen = round(totalGreen / cellCounter);
image[h][w].rgbtBlue = round(totalBlue / cellCounter);
}
}
return;
}
//total the red, green, blue values of the original and surrounding cells
void rgbAdd(RGBTRIPLE *p, int *totalRed, int *totalGreen, int *totalBlue)
{
RGBTRIPLE pixel = *p;
BYTE pRed = pixel.rgbtRed;
printf("R: %i", pRed);
BYTE pGreen = pixel.rgbtGreen;
printf("G: %i", pGreen);
BYTE pBlue = pixel.rgbtBlue;
printf("B: %i", pBlue);
*totalRed += pRed;
*totalGreen += pGreen;
*totalBlue += pBlue;
}
I have been struggling with implementing the blur function these past days and am on the verge of a meltdown.
I think the issue might lie in the usage of the pointer and address, but I am unsure. For some reason, whenever I try to get the pixel that the loop had changed in the previous loop, I end up with something completely different.
Output from CS50 checker:
first row: (10, 20, 30), (40, 50, 60), (70, 80, 90)
second row: (110, 130, 140), (120, 140, 150), (130, 150, 160)
third row: (200, 210, 220), (220, 230, 240), (240, 250, 255)
Block 00
R: 10 G: 20 B: 30 (check middle main block)
R: 110 G: 130 B: 140 (check south block)
R: 120 G: 140 B: 150 (check southeast block)
R: 40 G: 50 B: 60 (check east block)
Block 01
R: 40 G: 50 B: 60 (check middle main block)
R: 120 G: 140 B: 150 (check south block)
R: 110 G: 130 B: 140 (check southwest block)
R: 130 G: 150 B: 160 (check southeast block)
R: 49 G: 247 B: 0 (suppose to be west block (10, 20, 30) - not sure what happened here)
R: 70 G: 80 B: 90 (check east block)
Block 0 2
R: 70G: 80B: 90
R: 130G: 150B: 160
R: 120G: 140B: 150
R: 0G: 0B:...
I have enclosed my code below:
// Blur image
void blur(int height, int width, RGBTRIPLE image[height][width])
{
RGBTRIPLE temp[height][width];
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
temp[i][j] = image[i][j];
}
}
for (int h = 0; h < height; h++)
{
for (int w = 0; w < width; w++)
{
printf("Block %i %i", h, w);
int totalRed = 0;
int totalGreen = 0;
int totalBlue = 0;
float cellCounter = 0.0;
//adds rgb for designated cell
rgbAdd(&temp[h][w], &totalRed, &totalGreen, &totalBlue);
cellCounter += 1.0;
//north row has to be at least 0
if ((h - 1) >= 0)
{
//tally up north cell
rgbAdd(&temp[h - 1][w], &totalRed, &totalGreen, &totalBlue);
cellCounter += 1.0;
//east col has to be at least 0 for northeast corner to exist
if ((w - 1) >= 0)
{
//tally up northeast corner
rgbAdd(&temp[h - 1][w - 1], &totalRed, &totalGreen, &totalBlue);
cellCounter += 1.0;
}
//west col has to be (width - 1) or less for northwest corner to exist
if ((w + 1) <= (width - 1))
{
//tally up northwest corner
rgbAdd(&temp[h - 1][w + 1], &totalRed, &totalGreen, &totalBlue);
cellCounter += 1.0;
}
}
//south row has to be (height -1) or less
if ((h + 1) <= height - 1)
{
//tally up south cell
rgbAdd(&temp[h + 1][w], &totalRed, &totalGreen, &totalBlue);
cellCounter += 1.0;
//east col has to be at least 0 for southeast corner to exist
if ((w - 1) >= 0)
{
//tally up northeast corner
rgbAdd(&temp[h + 1][w - 1], &totalRed, &totalGreen, &totalBlue);
cellCounter += 1.0;
}
//west col has to be less than (width - 1) for southwest corner to exist
if ((w + 1) <= (width - 1))
{
//tally up northwest corner
rgbAdd(&temp[h + 1][w + 1], &totalRed, &totalGreen, &totalBlue);
cellCounter += 1.0;
}
}
//west col has to be at least 0
if ((w - 1) >= 0)
{
//tally up west point
rgbAdd(&temp[h - 1][w - 1], &totalRed, &totalGreen, &totalBlue);
cellCounter += 1.0;
}
//east col has to be equal to or less than (width - 1)
if ((w + 1) <= (width - 1))
{
//tally up east point
rgbAdd(&temp[h][w + 1], &totalRed, &totalGreen, &totalBlue);
cellCounter += 1.0;
}
image[h][w].rgbtRed = round(totalRed / cellCounter);
image[h][w].rgbtGreen = round(totalGreen / cellCounter);
image[h][w].rgbtBlue = round(totalBlue / cellCounter);
}
}
return;
}
//total the red, green, blue values of the original and surrounding cells
void rgbAdd(RGBTRIPLE *p, int *totalRed, int *totalGreen, int *totalBlue)
{
RGBTRIPLE pixel = *p;
BYTE pRed = pixel.rgbtRed;
printf("R: %i", pRed);
BYTE pGreen = pixel.rgbtGreen;
printf("G: %i", pGreen);
BYTE pBlue = pixel.rgbtBlue;
printf("B: %i", pBlue);
*totalRed += pRed;
*totalGreen += pGreen;
*totalBlue += pBlue;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
west 点的坐标存在错误:您写了
rgbadd(&amp; temp [h -1] [w -1] [w -1],&amp; totalred,&amp; totalgreen,&amp; totalBlue);
而不是:凝视明显而不看的非常令人沮丧,但是所有程序员都会发生……
为了使错误更难隐藏在宾至如归中,我建议简化代码沿着这些想法:
rgbavgadd()
对像素的一致顺序上的像素,以botton的顶部为顶,从rgbavgend中左右
这是修改的代码:
There is an error in the coordinates of the west point: you wrote
rgbAdd(&temp[h - 1][w - 1], &totalRed, &totalGreen, &totalBlue);
instead of:It is very frustrating to stare at the obvious and not see it, but it happens to all programmers...
To make it harder for bugs to hide in plain sight, I recommend simplifying the code along these ideas:
rgbAvgAdd()
for pixels in a consistent order top to botton, left to rightrgbAvgEnd()
Here is the modified code: