質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Q&A

解決済

1回答

979閲覧

BufferedImageのnoiseを取除く

wangzj

総合スコア53

Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

0グッド

0クリップ

投稿2018/06/13 08:28

編集2018/06/13 10:03

QRコードのnoiseを取除きたいですが、
ぐぐるで検索してみたら、いくつかのメソッドがあったんですが
試してみると、だめでした!
問題はどこかを教えていただけますでしょうか?
ほか、いいメソッドがあれば、ぜひご教授ください。

イメージ説明
メソッド1
※このメソッドはnoise取除きではなく、追加ですね
※調査してわかったことを追記

java

1public static BufferedImage scale(BufferedImage src, int w, int h) 2 { 3 BufferedImage img = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB); 4 int x, y; 5 int ww = src.getWidth(); 6 int hh = src.getHeight(); 7 for (x = 0; x < w; x++) { 8 for (y = 0; y < h; y++) { 9 int col = src.getRGB(x * ww / w, y * hh / h); 10 img.setRGB(x, y, col); 11 } 12 } 13 return img; 14 }

メソッド2
※このメソッドはnoise取除きではなく、追加ですね
※調査してわかったことを追記

java

1 public static BufferedImage impulseNoise(BufferedImage image, BufferedImage output) { 2 output.setData(image.getData()); 3 4 Raster source = image.getRaster(); 5 WritableRaster out = output.getRaster(); 6 double impulseRatio = 0.05; 7 double rand; 8 double halfImpulseRatio = impulseRatio / 2.0; 9 int bands = out.getNumBands(); 10 int width = image.getWidth(); // width of the image 11 int height = image.getHeight(); // height of the image 12 java.util.Random randGen = new java.util.Random(); 13 14 for (int j=0; j<height; j++) { 15 for (int i=0; i<width; i++) { 16 rand = randGen.nextDouble(); 17 if (rand < halfImpulseRatio) { 18 for (int b=0; b<bands; b++) out.setSample(i, j, b, 0); 19 } else if (rand < impulseRatio) { 20 for (int b=0; b<bands; b++) out.setSample(i, j, b, 255); 21 } 22 } 23 } 24 25 return output; 26 }

メソッド3

java

1 protected static BufferedImage gaussianNoise(BufferedImage image, BufferedImage output) { 2 Raster source = image.getRaster(); 3 WritableRaster out = output.getRaster(); 4 5 double stdDev = 10.0; 6 int currVal; // the current value 7 double newVal; // the new "noisy" value 8 double gaussian; // gaussian number 9 int bands = out.getNumBands(); // number of bands 10 int width = image.getWidth(); // width of the image 11 int height = image.getHeight(); // height of the image 12 java.util.Random randGen = new java.util.Random(); 13 14 for (int j=0; j<height; j++) { 15 for (int i=0; i<width; i++) { 16 gaussian = randGen.nextGaussian(); 17 18 for (int b=0; b<bands; b++) { 19 newVal = stdDev * gaussian; 20 currVal = source.getSample(i, j, b); 21 newVal = newVal + currVal; 22 if (newVal < 0) newVal = 0.0; 23 if (newVal > 255) newVal = 255.0; 24 25 out.setSample(i, j, b, (int)(newVal)); 26 } 27 } 28 } 29 30 return output; 31 }

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

汎用的なプログラムではありませんが、どうでしょうか。

java

1static final int= 0xFF000000; 2static final int= 0xFFFFFFFF; 3static final int 中間色 = 0xFF808080; 4static final int[][] 近傍 = { 5 {-1, 0}, // 左 6 {0, -1}, // 上 7 {1, 0}, // 右 8 {0, 1}, // 下 9}; 10static final int 近傍数 = 近傍.length; 11 12public static void removeNoise(File in, File out) throws IOException { 13 BufferedImage image = ImageIO.read(in); 14 int width = image.getWidth(); 15 int height = image.getHeight(); 16 for (int x = 0; x < width; ++x) { 17 for (int y = 0; y < height; ++y) { 18 int black = 0; 19 int white = 0; 20 int org = image.getRGB(x, y); 21 for (int i = 0; i < 近傍数; ++i) { 22 int xi = x + 近傍[i][0], yi = y + 近傍[i][1]; 23 if (xi < 0 || xi >= width) continue; 24 if (yi < 0 || yi >= height) continue; 25 if (image.getRGB(xi, yi) > 中間色) 26 ++white; 27 else 28 ++black; 29 } 30 if (black == white) 31 image.setRGB(x, y, org > 中間色 ?:); 32 else 33 image.setRGB(x, y, black > white ?:); 34 } 35 } 36 ImageIO.write(image, "png", out); 37} 38

結果はこうなります。

処理結果のイメージ

投稿2018/06/13 10:04

編集2018/06/13 13:03
退会済みユーザー

退会済みユーザー

総合スコア0

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

wangzj

2018/06/13 10:13

さすが!!!!!!!!!!!!! QRコードを読み取れました!!! ありがとうございました!!!!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問