From ed7009231ae0516a41accb4eb32891905e8e7493 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=8F=E7=A4=92=20=E8=80=80=E5=B9=B3?= Date: Mon, 11 Jul 2022 13:03:42 +0900 Subject: [PATCH 1/4] =?UTF-8?q?B110=20=E8=A7=A3=E5=83=8F=E5=BA=A6=E3=81=AE?= =?UTF-8?q?=E5=90=91=E4=B8=8A=E3=80=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ykoiso/src/B110_Resolution.java | 132 +++++++++++++++++++++++++++ ykoiso/test/B110_ResolutionTest.java | 44 +++++++++ 2 files changed, 176 insertions(+) create mode 100644 ykoiso/src/B110_Resolution.java create mode 100644 ykoiso/test/B110_ResolutionTest.java diff --git a/ykoiso/src/B110_Resolution.java b/ykoiso/src/B110_Resolution.java new file mode 100644 index 0000000..7421bac --- /dev/null +++ b/ykoiso/src/B110_Resolution.java @@ -0,0 +1,132 @@ +import java.util.Scanner; + +public class B110_Resolution { + // 重ねる際ずらす量 + final int SHIFT_HEIGHT = 1; + final int SHIFT_WIDTH = 1; + // 分割倍率 + final int SPLIT_MAG = 2; + + public static void main(String[] args) { + Scanner scan = new Scanner(System.in); + // 定数読み込み + final int height = scan.nextInt(); + final int width = scan.nextInt(); + // 画像インスタンス生成 + int[][] img1 = new int[height][width]; + int[][] img2 = new int[height][width]; + for (int H = 0; H < height; H++) { + for (int W = 0; W < width; W++) { + img1[H][W] = scan.nextInt(); + } + } + for (int H = 0; H < height; H++) { + for (int W = 0; W < width; W++) { + img2[H][W] = scan.nextInt(); + } + } + B110_Resolution re = new B110_Resolution(); + // 分割画像の生成 + SplitImage splitImg1 = re.new SplitImage(height, width); + SplitImage splitImg2 = re.new SplitImage(height, width); + splitImg1.makeImage(img1, height, width); + splitImg2.makeImage(img2, height, width); + // 高解像度画像の生成 + NewImage newImage = re.new NewImage(height, width); + newImage.makeImage(splitImg1, splitImg2); + // 画像の出力 + newImage.showImage(); + scan.close(); + } + + // 分割画像 + class SplitImage { + final int height; + final int width; + int[][] img; + + // コンストラクタ + SplitImage(final int height, final int width) { + this.height = height * 2; + this.width = width * 2; + img = new int[this.height][this.width]; + } + + // テスト用 + public void setImg(int[][] img) { + this.img = img; + } + + // 元画像から分割画像の生成 + public void makeImage(int[][] img, int height, int width) { + for (int H = 0; H < height; H++) { + for (int W = 0; W < width; W++) { + int newH = H * SPLIT_MAG; + int newW = W * SPLIT_MAG; + for (int i = 0; i < 2; i++) { + for (int j = 0; j < 2; j++) { + this.img[newH + i][newW + j] = img[H][W]; + } + } + } + } + } + + public int getHeight() { + return height; + } + + public int getWidth() { + return width; + } + + public int getImgPixel(int H, int W) { + return img[H][W]; + } + } + // 高解像度画像 + class NewImage { + final int height; + final int width; + int[][] img; + + NewImage(final int height, final int width) { + this.height = height * 2 - SHIFT_HEIGHT; + this.width = width * 2 - SHIFT_WIDTH; + img = new int[this.height][this.width]; + } + + // 2つの画像から高解像度の画像の生成 + // 画像をずらして重ねて各画素の値の平均を取る + public void makeImage(SplitImage img1, SplitImage img2) { + int splitHeight = img1.getHeight(); + int splitWidth = img1.getWidth(); + for (int img1H = 1; img1H < splitHeight; img1H++) { + for (int img1W = 1; img1W < splitWidth; img1W++) { + int img2H = img1H - SHIFT_HEIGHT; + int img2W = img1W - SHIFT_WIDTH; + img[img2H][img2W] = + (img1.getImgPixel(img1H, img1W) + img2.getImgPixel(img2H, img2W)) / 2; + } + } + } + + // 画像の出力 + public void showImage() { + for (int H = 0; H < height; H++) { + for (int W = 0; W < width; W++) { + System.out.print(img[H][W]); + if (W != width - 1) + System.out.print(" "); + } + System.out.println(); + } + } + + // テスト用 + public int[][] getImg() { + return img; + } + } + +} diff --git a/ykoiso/test/B110_ResolutionTest.java b/ykoiso/test/B110_ResolutionTest.java new file mode 100644 index 0000000..d289849 --- /dev/null +++ b/ykoiso/test/B110_ResolutionTest.java @@ -0,0 +1,44 @@ +import static org.junit.Assert.assertThat; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import org.junit.Test; + +import static org.hamcrest.CoreMatchers.*; + +public class B110_ResolutionTest { + B110_Resolution re = new B110_Resolution(); + B110_Resolution.NewImage sut; + B110_Resolution.SplitImage img1 = re.new SplitImage(2, 2); + B110_Resolution.SplitImage img2 = re.new SplitImage(2, 2); + + @Test + public void 同じ画素の値がかぶっていないとき() { + int[][] originalImg1 = + {{0, 0, 255, 255}, {0, 0, 255, 255}, {0, 0, 255, 255}, {0, 0, 255, 255}}; + img1.setImg(originalImg1); + int[][] originalImg2 = + {{255, 0, 0, 255}, {255, 0, 0, 255}, {255, 0, 0, 255}, {255, 0, 0, 255}}; + img2.setImg(originalImg2); + sut = re.new NewImage(2, 2); + sut.makeImage(img1, img2); + int[][] actual = sut.getImg(); + int[][] expected = {{127, 127, 127}, {127, 127, 127}, {127, 127, 127}}; + assertThat(actual, is(expected)); + } + + @Test + public void 同じ画素の値がかぶっているとき() { + int[][] originalImg1 = + {{0, 255, 255, 255}, {0, 255, 255, 255}, {0, 255, 255, 255}, {0, 255, 255, 255}}; + img1.setImg(originalImg1); + int[][] originalImg2 = {{55, 255, 255, 255}, {155, 255, 255, 255}, {55, 255, 255, 255}, + {155, 255, 255, 255}}; + img2.setImg(originalImg2); + sut = re.new NewImage(2, 2); + sut.makeImage(img1, img2); + int[][] actual = sut.getImg(); + int[][] expected = {{155, 255, 255}, {205, 255, 255}, {155, 255, 255}}; + assertThat(actual, is(expected)); + } +} -- GitLab From 4556f7ac6a85389845856bfd683fde8ed1929a31 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=8F=E7=A4=92=20=E8=80=80=E5=B9=B3?= Date: Mon, 11 Jul 2022 14:53:13 +0900 Subject: [PATCH 2/4] =?UTF-8?q?B110=20=E8=A7=A3=E5=83=8F=E5=BA=A6=E3=81=AE?= =?UTF-8?q?=E5=90=91=E4=B8=8A=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ykoiso/src/B110_Resolution.java | 33 +++++++++++++++++++-------------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/ykoiso/src/B110_Resolution.java b/ykoiso/src/B110_Resolution.java index 7421bac..2f2938d 100644 --- a/ykoiso/src/B110_Resolution.java +++ b/ykoiso/src/B110_Resolution.java @@ -32,7 +32,7 @@ public class B110_Resolution { splitImg1.makeImage(img1, height, width); splitImg2.makeImage(img2, height, width); // 高解像度画像の生成 - NewImage newImage = re.new NewImage(height, width); + HighResoImage newImage = re.new HighResoImage(height, width); newImage.makeImage(splitImg1, splitImg2); // 画像の出力 newImage.showImage(); @@ -53,21 +53,26 @@ public class B110_Resolution { } // テスト用 - public void setImg(int[][] img) { + public void setImg(final int[][] img) { this.img = img; } // 元画像から分割画像の生成 - public void makeImage(int[][] img, int height, int width) { + public void makeImage(final int[][] img, final int height, final int width) { for (int H = 0; H < height; H++) { for (int W = 0; W < width; W++) { - int newH = H * SPLIT_MAG; - int newW = W * SPLIT_MAG; - for (int i = 0; i < 2; i++) { - for (int j = 0; j < 2; j++) { - this.img[newH + i][newW + j] = img[H][W]; - } - } + splitPixel(img, H, W); + } + } + } + + // 元画像の画素に対して分割した画素を生成する + public void splitPixel(final int[][] img, int originalH, int originalW) { + int splitH = originalH * SPLIT_MAG; + int splitW = originalW * SPLIT_MAG; + for (int i = 0; i < 2; i++) { + for (int j = 0; j < 2; j++) { + this.img[splitH + i][splitW + j] = img[originalH][originalW]; } } } @@ -80,17 +85,17 @@ public class B110_Resolution { return width; } - public int getImgPixel(int H, int W) { + public int getImgPixel(final int H, final int W) { return img[H][W]; } } // 高解像度画像 - class NewImage { + class HighResoImage { final int height; final int width; int[][] img; - NewImage(final int height, final int width) { + HighResoImage(final int height, final int width) { this.height = height * 2 - SHIFT_HEIGHT; this.width = width * 2 - SHIFT_WIDTH; img = new int[this.height][this.width]; @@ -98,7 +103,7 @@ public class B110_Resolution { // 2つの画像から高解像度の画像の生成 // 画像をずらして重ねて各画素の値の平均を取る - public void makeImage(SplitImage img1, SplitImage img2) { + public void makeImage(final SplitImage img1, final SplitImage img2) { int splitHeight = img1.getHeight(); int splitWidth = img1.getWidth(); for (int img1H = 1; img1H < splitHeight; img1H++) { -- GitLab From fd5ef1db370ab21e281ae1a519220ec2bcbea14b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=8F=E7=A4=92=20=E8=80=80=E5=B9=B3?= Date: Mon, 11 Jul 2022 14:54:57 +0900 Subject: [PATCH 3/4] =?UTF-8?q?B110=20=E8=A7=A3=E5=83=8F=E5=BA=A6=E3=81=AE?= =?UTF-8?q?=E5=90=91=E4=B8=8A=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ykoiso/test/B110_ResolutionTest.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/ykoiso/test/B110_ResolutionTest.java b/ykoiso/test/B110_ResolutionTest.java index d289849..8d1d0d8 100644 --- a/ykoiso/test/B110_ResolutionTest.java +++ b/ykoiso/test/B110_ResolutionTest.java @@ -8,7 +8,7 @@ import static org.hamcrest.CoreMatchers.*; public class B110_ResolutionTest { B110_Resolution re = new B110_Resolution(); - B110_Resolution.NewImage sut; + B110_Resolution.HighResoImage sut; B110_Resolution.SplitImage img1 = re.new SplitImage(2, 2); B110_Resolution.SplitImage img2 = re.new SplitImage(2, 2); @@ -20,7 +20,7 @@ public class B110_ResolutionTest { int[][] originalImg2 = {{255, 0, 0, 255}, {255, 0, 0, 255}, {255, 0, 0, 255}, {255, 0, 0, 255}}; img2.setImg(originalImg2); - sut = re.new NewImage(2, 2); + sut = re.new HighResoImage(2, 2); sut.makeImage(img1, img2); int[][] actual = sut.getImg(); int[][] expected = {{127, 127, 127}, {127, 127, 127}, {127, 127, 127}}; @@ -35,7 +35,7 @@ public class B110_ResolutionTest { int[][] originalImg2 = {{55, 255, 255, 255}, {155, 255, 255, 255}, {55, 255, 255, 255}, {155, 255, 255, 255}}; img2.setImg(originalImg2); - sut = re.new NewImage(2, 2); + sut = re.new HighResoImage(2, 2); sut.makeImage(img1, img2); int[][] actual = sut.getImg(); int[][] expected = {{155, 255, 255}, {205, 255, 255}, {155, 255, 255}}; -- GitLab From df0e766609a6aa40ae86ac4be256d33e941fdcc8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=8F=E7=A4=92=20=E8=80=80=E5=B9=B3?= Date: Mon, 11 Jul 2022 14:57:33 +0900 Subject: [PATCH 4/4] =?UTF-8?q?B110=20=E8=A7=A3=E5=83=8F=E5=BA=A6=E3=81=AE?= =?UTF-8?q?=E5=90=91=E4=B8=8A=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ykoiso/src/B110_Resolution.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ykoiso/src/B110_Resolution.java b/ykoiso/src/B110_Resolution.java index 2f2938d..4823f1c 100644 --- a/ykoiso/src/B110_Resolution.java +++ b/ykoiso/src/B110_Resolution.java @@ -67,7 +67,7 @@ public class B110_Resolution { } // 元画像の画素に対して分割した画素を生成する - public void splitPixel(final int[][] img, int originalH, int originalW) { + public void splitPixel(final int[][] img, final int originalH, final int originalW) { int splitH = originalH * SPLIT_MAG; int splitW = originalW * SPLIT_MAG; for (int i = 0; i < 2; i++) { -- GitLab