CS50 Pset4滤镜有问题,模糊效果

时间:2020-08-04 22:04:18

标签: c cs50 blur

#include "helpers.h"
#include <math.h>
 
// Convert image to grayscale
void grayscale(int height, int width, RGBTRIPLE image[height][width])
{
    int total = 0;
    float avg = 0;
 
    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j++)
        {
            total = image[i][j].rgbtRed + image[i][j].rgbtGreen + image[i][j].rgbtBlue;
            avg = total / 3.00;
            image[i][j].rgbtRed = round(avg);
            image[i][j].rgbtGreen = round(avg);
            image[i][j].rgbtBlue = round(avg);
        }
    }
    return;
}
 
// Convert image to sepia
void sepia(int height, int width, RGBTRIPLE image[height][width])
{
    float sepia_red = 0;
    float sepia_green = 0;
    float sepia_blue = 0;
    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j++)
        {
            sepia_red = (.393 * image[i][j].rgbtRed + .769 * image[i][j].rgbtGreen + .189 * image[i][j].rgbtBlue);
            sepia_green = (.349*image[i][j].rgbtRed) + (.686*image[i][j].rgbtGreen) + (.168*image[i][j].rgbtBlue);
            sepia_blue = (.272*image[i][j].rgbtRed) + (.534*image[i][j].rgbtGreen) + (.131*image[i][j].rgbtBlue);
 
            if (sepia_red > 255)
            {
                sepia_red = 255;
            }
            if (sepia_green > 255)
            {
                sepia_green = 255;
            }
            if (sepia_blue > 255)
            {
                sepia_blue = 255;
            }
            image[i][j].rgbtRed = round(sepia_red);
            image[i][j].rgbtGreen = round(sepia_green);
            image[i][j].rgbtBlue = round(sepia_blue);
        }
    }
    return;
}
 
// Reflect image horizontally
void reflect(int height, int width, RGBTRIPLE image[height][width])
{
    int temp[3];
    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width/2; j ++)
        {
            temp[0] = image[i][j].rgbtRed;
            temp[1] = image[i][j].rgbtGreen;
            temp[2] = image[i][j].rgbtBlue;
 
            image[i][j].rgbtRed = image[i][width - j - 1].rgbtRed;
            image[i][j].rgbtGreen = image[i][width - j - 1].rgbtGreen;
            image[i][j].rgbtBlue = image[i][width - j - 1].rgbtBlue;
 
            image[i][width - j - 1].rgbtRed = temp[0];
            image[i][width - j - 1].rgbtGreen = temp[1];
            image[i][width - j - 1].rgbtBlue = temp[2];
        }
    }
    return;
}
 
// Blur image
void blur(int height, int width, RGBTRIPLE image[height][width])
{
    int total = 0;
    float count = 0;
    int elements = 0;
 
    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j++)
        {
            for (int x = i - 1; x <= i + 1; x++)
            {
                if (x >= 0 && x <= height - 1)
                {
                    elements++;
                }
            }
            for (int y = j - 1; y <= j + 1; y++)
            {
                if (y >= 0 && y < width - 1)
                {
                    elements++;
                }
            }
 
            total = image[i][j].rgbtRed + image[i][j].rgbtGreen + image[i][j].rgbtBlue;
 
            count = total / elements;
 
            image[i][j].rgbtRed = round(count);
            image[i][j].rgbtGreen = round(count);
            image[i][j].rgbtBlue = round(count);
        }
    }
    return;
}

在几次reddit发布后,我被告知需要

  • 创建一个临时变量,然后在创建临时变量后将原始像素更改为该临时变量的值
  • 创建一个递增变量(例如(int pixel_used))除以

我不知道该怎么做,除以递增变量会困扰我,因为我认为我必须创建red_countblue_countgreen_count和{{1} }。 我尝试在此处https://pastebin.com/UX1miGtd编写这种形式的代码,因为它类似于Brian在这张图片https://i.stack.imgur.com/K7rVL.png中给我们的示例。我们将红色,绿色和蓝色的总数相加–然后将所选像素分配给这些数字。显然,根据我不得不使用的变量数量来看,它看起来并不好,我认为这是错误的,但是我没有其他想法。

如果有人可以提供帮助,我将不胜感激!

0 个答案:

没有答案