1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【画像処理100本ノックに挑戦】Q.39. JPEG圧縮 (Step.3)YCbCr表色系

Last updated at Posted at 2020-01-23

使用したライブラリ

【画像処理100本ノック】独自の画像入出力クラスを作る

Q.39. JPEG圧縮 (Step.3)YCbCr表色系

YCbCr表色形において、Yを0.7倍してコントラストを暗くせよ。

【画像処理100本ノックに挑戦】Q.5. HSV変換のコードを流用しました。

struct YCbCr
{
	double Y, Cb, Cr;
};
struct RGB
{
	RGB(int r, int g, int b)
	{
		this->r = r;
		this->g = g;
		this->b = b;
	}
	int r, g, b;
};

YCbCr RGB2YCbCr(const RGB& rgb)
{
	YCbCr ycbcr;
	int R = rgb.r;
	int G = rgb.g;
	int B = rgb.b;

	ycbcr.Y = 0.299 * R + 0.5870 * G + 0.114 * B;
	ycbcr.Cb = -0.1687 * R - 0.3313 * G + 0.5 * B + 128;
	ycbcr.Cr = 0.5 * R - 0.4187 * G - 0.0813 * B + 128;

	return ycbcr;
}
RGB YCbCr2RGB(const YCbCr& ycbcr)
{
	double R, G, B;
	double Y = ycbcr.Y;
	double Cb = ycbcr.Cb;
	double Cr = ycbcr.Cr;
	R = Y + (Cr - 128) * 1.402;
	G = Y - (Cb - 128) * 0.3441 - (Cr - 128) * 0.7139;
	B = Y + (Cb - 128) * 1.7718;
	return RGB(R,G,B);
}

int main()
{
	PPM ppm("imori.pnm");
	int width = ppm.Get_width();
	int height = ppm.Get_height();
	PPM ppm2(width, height);

	for (int j = 0; j < height; j++)
		for (int i = 0; i < width; i++)
		{
			int r = ppm(i, j, 'r');
			int g = ppm(i, j, 'g');
			int b = ppm(i, j, 'b');

			YCbCr ycbcr = RGB2YCbCr(RGB(r, g, b));
			ycbcr.Y *= 0.7;

			RGB rgb = YCbCr2RGB(ycbcr);
			ppm2(i, j, 'r') = rgb.r;
			ppm2(i, j, 'g') = rgb.g;
			ppm2(i, j, 'b') = rgb.b;
		}

	ppm2.Flush("out.ppm");
	return 0;
}

imori.png out.png

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?