c - 需要cs50滤镜灰度的一些初始方向
问题描述
我正在尝试解决过滤器灰度的问题,并且在开始之前我正在苦苦挣扎。
对于灰度,我们给出的公式是平均 RGB 的值,然后将该值用作每个像素的新 RGB 值。平均后如何更新像素中的值?
我在想我可以创建一个 Replace 的布尔值并将其设置为 false,然后在替换后将其设置为 true,但现在我认为我忽略了一些更简单的东西。
当我不知道除了'height -1'行之外还有多少行时,我如何将图像视为一个数组。
作业视频将示例像素格式描述为:
image[2][3].rgtbBlue
image[2][3].rgbtGreen
image[2][3].rgbtRed
那么这是否意味着
((BYTE.rgbtBlue + BYTE.rgbtGreen + BYTE.rgbtRed) /3)
无法获得平均值?
这行得通吗?
for (int i = 0; i < height -1; i ++)
{
for (int j =0; j < height; j++)
{
image[i][j].rgbtBlue = round(((BYTE.rgbtBlue+ BYTE.rgbtGreen + BYTE.rgbtRed) /3));
image[i][j].rgbtGreen = round(((BYTE.rgbtBlue+ BYTE.rgbtGreen + BYTE.rgbtRed) /3));
image[i][j].rgbtRed = round(((BYTE.rgbtBlue+ BYTE.rgbtGreen + BYTE.rgbtRed) /3));
}
}
解决方案
void grayscale(int height, int width, RGBTRIPLE image[height][width])
{
// Locate the pixel
for (int i = 0; i < width; i++)
{
for (int j = 0; j < height; j++)
{
// Find the average pixel color
float avgpixel = round((image[j][i].rgbtBlue + image[j][i].rgbtGreen + image[j][i].rgbtRed) / 3.00);
// Replace the grayscale pixel to all colors
image[j][i].rgbtBlue = avgpixel;
image[j][i].rgbtGreen = avgpixel;
image[j][i].rgbtRed = avgpixel;
}
}
return;
}
推荐阅读
- c - 在 C 编程中需要生成 4 个不重复的随机数。1到4
- python - 如何知道 python 中的程序是否在 spyder 中运行?
- omnet++ - 动脉(VEINS 扩展)- LTE 和 IEEE 802.11p 结合
- python - 从 python 类中的另一个方法访问在方法内部创建的变量
- javascript - 使用对象数组展平对象中的数据
- java - 如何在原生 android 和 java 中查找嵌套元素
- tsql - TSQL:尝试简化/优化我的存储过程
- r - 是否有像 ggplot 或 plot 之类的 R 函数以按 id 和不同颜色在同一图中显示我所有的时间序列曲线
- c# - 获取 XML 正文 - 调用肥皂网络服务时出现反序列化错误
- apache-spark - 使用 Spark 从 S3 复制到 HDFS