【问题标题】:Reflect pset4 CS50反映 pset4 CS50
【发布时间】:2020-04-11 08:58:26
【问题描述】:

向所有参与 CS50 的人致敬,

目前,我正在做 pset4 过滤、反映和努力编写我编写的代码。它编译得很好,但输出图片看起来像我附加的那个。我有什么线索可以解决这个问题吗? (如果可能没有解决方案;提示完全可以:))

#include "helpers.h"
#include <stdio.h>
#include <math.h>

// Convert image to grayscale
void grayscale(int height, int width, RGBTRIPLE image[height][width])
{
   for (int i = 0; i < (height - 1); i++)
   {
       for (int j = 0; j < (width - 1); j++)
       {
           //get the average
           float average = 0;
           average = (image[i][j].rgbtRed + image[i][j].rgbtGreen + image[i][j].rgbtBlue) / 3;

           //set color channels with the average value
           image[i][j].rgbtRed = round(average);
           image[i][j].rgbtGreen = round(average);
           image[i][j].rgbtBlue = round(average);
       }
   }
   return;
}

// Convert image to sepia
void sepia(int height, int width, RGBTRIPLE image[height][width])
{
   for (int i = 0; i < (height - 1); i++)
   {
       for (int j = 0; j < (width - 1); j++)
       {
           //calculate the new values
           float red = 0;
           float green = 0;
           float blue = 0;


           red = .393 * image[i][j].rgbtRed + .769 * image[i][j].rgbtGreen + .189 * image[i][j].rgbtBlue;
           if (round(red) > 255)
           {
               image[i][j].rgbtRed = 255;
           }
           else if (round(red <= 255))
           {
               image[i][j].rgbtRed = red;
           }

           green = .349 * image[i][j].rgbtRed + .686 * image[i][j].rgbtGreen + .168 * image[i][j].rgbtBlue;
           if (round(green) > 255)
           {
               image[i][j].rgbtGreen = 255;
           }
           else if (round(green) <= 255)
           {
               image[i][j].rgbtGreen = green;
           }

           blue = .272 * image[i][j].rgbtRed + .534 * image[i][j].rgbtGreen + .131 * image[i][j].rgbtBlue;
           if (round(blue) > 255)
           {
               image[i][j].rgbtBlue = 255;
           }
           else if (round(blue) <= 255)
           {
               image[i][j].rgbtBlue = blue;
           }
       }
   }
   return;
}

// Reflect image horizontally
void reflect(int height, int width, RGBTRIPLE image[height][width])
{
   for (int i = 0; i < (height - 1); i++)
   {
       //RGBTRIPLE

       int a[width - 1 /2];
       int b[width - 1 /2];
       int c[width - 1 /2];

       for (int j = 0; j < ((width/2) - 1); j++)
       {
           a[j] = image[i][(width - 1) - j].rgbtRed;
           b[j] = image[i][(width - 1) - j].rgbtGreen;
           c[j] = image[i][(width - 1) - j].rgbtBlue;
       }

       // GERADE / UNGERADE ZAHLEN VERARBEITEN

       for (int n = 0; (n + (width - 1) /2) < (width - 1); n++)
       {
           image[i][((width - 1)/2) - n].rgbtRed = image[i][((width - 1)/2) - n].rgbtRed;
           image[i][((width - 1)/2) - n].rgbtGreen = image[i][((width - 1)/2) - n].rgbtGreen;
           image[i][((width - 1)/2) - n].rgbtBlue = image[i][((width - 1)/2) - n].rgbtBlue;
       }

       for (int m = 0; m < ((width/2) - 1); m++)
       {
           image[i][m].rgbtRed = a[m];
           image[i][m].rgbtRed = b[m];
           image[i][m].rgbtRed = c[m];
       }
   }
   return;
}

// Blur image
void blur(int height, int width, RGBTRIPLE image[height][width])
{
   return;
}

输入:

./filter -r stadium.bmp outfile.bmp 

(stadium.bmp只是我选择的一个文件,你可以选择任何文件作为输入)

输出:编译成功,见附图enter image description here

【问题讨论】:

  • 所以你成功地编写了一个程序来创建一个看起来很酷的图片。我不明白你具体的编程相关问题是什么。
  • 哈哈谢谢。垂直反映图片的主要意图。但是,如您所见,输出并未反映出来。相反,我得到了这个输出。

标签: c cs50


【解决方案1】:

您的代码太复杂了。您不需要将图像划分为 RGB 值。您需要从左到右交换图像值,反之亦然以实现反射。

为此,您可能需要创建一个与“图像”相同的临时数据结构。

RGBTRIPLE temp[width];

使用temp,您需要存储来自image 的值,并交换它们。以下是您可以使用的设计模式。

//Pseudo code
1. //Loop though the row of an image (start from row = 0). We need to iterate every row.
    2. // Loop through the height of an image to save current row in an image to the temp.
    3. // Swap the values in image by using the temp. 

如果你想过滤下面的这个一维图像。

val1, val2, valn, val3, val4

反射滤镜后会是这个样子。

val4, val3, valn, val2, val1

【讨论】:

  • 谢谢!在您的伪代码中的交换部分之前,我理解了一切。你能详细说明一下吗?
  • 我对交换部分的理解是这样的: 1. 遍历每一行 2. 同时将每个图像值存储在 temp ( temp[j] = image[height][width];) 3.通过高度进行另一个循环,然后再次遍历行,但是这次是从右到左(for (int j = 0; width - 1 - j
  • 嗨 Nisanth,您的第二条评论正是您需要做的!我用一个例子改进了我的答案。
  • 我确实实现了它。图片正在反射,但是我在图片中间确实有黑色垂直线。第一行和最后一列似乎也已更改。你知道错误在哪里吗?
  • 很好!一步一步来。实际上,如果行中的元素数量是偶数,那么你需要考虑中间元素会发生什么。中间元素必须保持不变!因为它们在中心,在镜像点没有镜像! :) 如果第一行和最后一列不正确,请仔细检查循环条件。你可能在那里做错了什么。不过应该不难修复吧!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多