Filter: 課題
予め用意された写真に対して、フィルタ加工をするためのフィルタを作るというのが今回の課題。フィルターの種類は、全部で4種類(1)グレイスケール、(2)セピア、(3)反射、(4)ボケ。
https://cs50.harvard.edu/x/2021/psets/4/filter/less/
Filter: 回答例
#include "helpers.h"
#include "math.h"
// Convert image to grayscale
void grayscale(int height, int width, RGBTRIPLE image[height][width])
{
// Average RGB and round to nearest integer
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
// get into the 2D array, obtain value of each colour
int red = image[i][j].rgbtRed;
int blue = image[i][j].rgbtBlue;
int green = image[i][j].rgbtGreen;
// calculate the average value of each pixel, rounded
int avg = round(((float)red + (float)blue + (float)green)/3);
// set the calculated average to be the new value of each pixel
image[i][j].rgbtRed = image[i][j].rgbtBlue = image[i][j].rgbtGreen = avg;
}
}
return;
}
// Convert image to sepia
void sepia(int height, int width, RGBTRIPLE image[height][width])
{
// For loop for row and column
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
// get into the 2D array, obtain value of each colour
int red = image[i][j].rgbtRed;
int blue = image[i][j].rgbtBlue;
int green = image[i][j].rgbtGreen;
// apply sepia algorithm, rounded
// Sepia Red
int sepiaRed = round(0.393 * red + 0.769 * green + 0.189 * blue);
if (sepiaRed > 255)
{
image[i][j].rgbtRed = 255;
}
else
{
image[i][j].rgbtRed = sepiaRed;
}
// Sepia Blue
int sepiaGreen = round(0.349 * red + 0.686 * green + 0.168 * blue);
if (sepiaGreen > 255)
{
image[i][j].rgbtGreen = 255;
}
else
{
image[i][j].rgbtGreen = sepiaGreen;
}
// Sepia Green
int sepiaBlue = round(0.272 * red + 0.534 * green + 0.131 * blue);
if (sepiaBlue > 255)
{
image[i][j].rgbtBlue = 255;
}
else
{
image[i][j].rgbtBlue = sepiaBlue;
}
}
}
return;
}
// Reflect image horizontally
void reflect(int height, int width, RGBTRIPLE image[height][width])
{
// For loop for row and column (until mid point)
for (int i = 0; i < height; i++)
{
// until mid-point because pixels are swapped already
for (int j = 0; j < width/2; j++)
{
RGBTRIPLE temp = image[i][j];
//just like how [0][9]...[4][5], [i] will swap with [total-1-i]
image[i][j] = image[i][width - (j + 1)];
image[i][width - (j + 1)] = temp;
}
}
return;
}
// Blur image
void blur(int height, int width, RGBTRIPLE image[height][width])
{
//create a temporary image to be blurred
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 i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
int sum_blue;
int sum_green;
int sum_red;
float counter;
sum_blue = sum_green = sum_red = counter = 0;
// If pixel is a corner pixel -
//corner pixel on bottom right
if (i >= 0 && j >= 0)
{
sum_red += temp[i][j].rgbtRed;
sum_green += temp[i][j].rgbtGreen;
sum_blue += temp[i][j].rgbtBlue;
counter++;
}
//corner pixel on bottom left
if (i >= 0 && j - 1 >= 0)
{
sum_red += temp[i][j-1].rgbtRed;
sum_green += temp[i][j-1].rgbtGreen;
sum_blue += temp[i][j-1].rgbtBlue;
counter++;
}
//corner pixel on top left
if (i - 1 >= 0 && j >= 0)
{
sum_red += temp[i-1][j].rgbtRed;
sum_green += temp[i-1][j].rgbtGreen;
sum_blue += temp[i-1][j].rgbtBlue;
counter++;
}
//corner pixel on top right
if (i - 1 >= 0 && j - 1 >= 0)
{
sum_red += temp[i-1][j-1].rgbtRed;
sum_green += temp[i-1][j-1].rgbtGreen;
sum_blue += temp[i-1][j-1].rgbtBlue;
counter++;
}
// If pixel is located on the edges -
//pixels on bottom edge
if ((i >= 0 && j + 1 >= 0) && (i >= 0 && j + 1 < width))
{
sum_red += temp[i][j+1].rgbtRed;
sum_green += temp[i][j+1].rgbtGreen;
sum_blue += temp[i][j+1].rgbtBlue;
counter++;
}
//pixels on top edge
if ((i - 1 >= 0 && j + 1 >= 0) && (i - 1 >= 0 && j + 1 < width))
{
sum_red += temp[i-1][j+1].rgbtRed;
sum_green += temp[i-1][j+1].rgbtGreen;
sum_blue += temp[i-1][j+1].rgbtBlue;
counter++;
}
//pixels on left edge
if ((i + 1 >= 0 && j >= 0) && (i + 1 < height && j >= 0))
{
sum_red += temp[i+1][j].rgbtRed;
sum_green += temp[i+1][j].rgbtGreen;
sum_blue += temp[i+1][j].rgbtBlue;
counter++;
}
//pixels on right edge
if ((i + 1 >= 0 && j - 1 >= 0) && (i + 1 < height && j - 1 >= 0))
{
sum_red += temp[i+1][j-1].rgbtRed;
sum_green += temp[i+1][j-1].rgbtGreen;
sum_blue += temp[i+1][j-1].rgbtBlue;
counter++;
}
// If pixel is in the middle -
//middle pixels
if ((i + 1 >= 0 && j + 1 >= 0) && (i + 1 < height && j + 1 < width))
{
sum_red += temp[i+1][j+1].rgbtRed;
sum_green += temp[i+1][j+1].rgbtGreen;
sum_blue += temp[i+1][j+1].rgbtBlue;
counter++;
}
//find average colour value
image[i][j].rgbtRed = round(sum_red / counter);
image[i][j].rgbtGreen = round(sum_green / counter);
image[i][j].rgbtBlue = round(sum_blue / counter);
}
}
return;
}
未希 諒
Related posts
About media
Ramps Blog は、ノマドによるノマドのためのブログです。日本、香港、シンガポール、タイ、台湾のノマド生活を通じて見えた景色、特に食、カルチャー、テクノロジー、ビジネス、アート、デザインなどについて発信しています。
Recent Posts
安曇野放牧豚を豪快にバンズに挟んだハンバーガーはスーパーナイス!|三六五+二(367)〈松本十帖〉 https://ramps.jp/restaurant/matsumoto-jujo-367-lunch-super-nice-burger/
絶品。
#ハンバーガー #旅行好きな人と繋がりたい #写真好きな人と繋がりたい #ランチ
「INUA」部門シェフによる宿泊客限定の朝食|三六五+二(367)〈松本十帖〉 https://ramps.jp/restaurant/matsumoto-jujo-367-breakfast/
朝食のコンセプトは「ヘルシー&コンシャス」。『ALPS BAKERY』で焼き上げたパンの他、野菜をたっぷりの朝食を堪能。
#旅行好きな人と繋がりたい
#旅行
#写真撮ってる人と繋がりたい
#グルメ旅
「INUA」の部門シェフによる薪火ダイニングのローカルガストロノミー|三六五+二(367)〈松本十帖〉
http://ramps.jp/restaurant/mat…
このレストランを目的に旅をする価値がある。
#ローカルガストロノミー
#noma
#発酵
#ホテルレストラン
#グルメ旅
#ホテル
【ホテル宿泊記】松本十帖|地域活性化の拠点を担うホテルのスイートルーム〈松本・浅間温泉〉 http://ramps.jp/hotel/matsumot…
松本十帖のスイートルームの全貌を紹介。ただ一言、最高だった!
#旅行好きな人と繋がりたい
#写真好きな人と繋がりたい
#写真で伝える私の世界
#ホテル
#旅行記