Swing/ColorConvertOp のバックアップ差分(No.22)
- バックアップ一覧
- 現在との差分 を表示
- 現在との差分 - Visual を表示
- ソース を表示
- バックアップ を表示
- Swing/ColorConvertOp へ行く。
- 1 (2006-01-18 (水) 20:15:07)
- 2 (2006-02-27 (月) 15:34:23)
- 3 (2006-04-12 (水) 19:38:08)
- 4 (2006-05-18 (木) 17:10:13)
- 5 (2006-05-18 (木) 18:10:45)
- 6 (2007-02-23 (金) 17:20:54)
- 7 (2007-07-27 (金) 13:15:14)
- 8 (2007-08-21 (火) 12:49:19)
- 9 (2007-11-15 (木) 18:41:52)
- 10 (2008-01-10 (木) 10:39:27)
- 11 (2008-01-10 (木) 14:27:28)
- 12 (2009-10-09 (金) 20:51:35)
- 13 (2010-02-27 (土) 02:54:58)
- 14 (2010-10-14 (木) 12:54:42)
- 15 (2012-01-17 (火) 17:50:50)
- 16 (2013-02-26 (火) 19:50:44)
- 17 (2013-03-21 (木) 16:04:18)
- 18 (2013-08-01 (木) 14:03:38)
- 19 (2013-09-04 (水) 00:09:41)
- 20 (2014-04-24 (木) 15:54:59)
- 21 (2014-11-05 (水) 04:49:45)
- 22 (2014-11-25 (火) 03:03:31)
- 23 (2015-12-02 (水) 00:48:32)
- 24 (2016-06-01 (水) 19:07:19)
- 25 (2016-06-03 (金) 13:26:41)
- 26 (2016-09-01 (木) 21:38:24)
- 27 (2017-10-12 (木) 13:42:29)
- 28 (2018-12-19 (水) 21:24:39)
- 29 (2019-05-22 (水) 19:35:38)
- 30 (2020-11-15 (日) 01:57:07)
- 31 (2022-12-09 (金) 12:45:40)
- 追加された行はこの色です。
- 削除された行はこの色です。
---
title: ColorConvertOpで画像をグレースケールに変換
tags: [ColorConvertOp, BufferedImage, RGBImageFilter, ImageIcon]
author: aterai
pubdate: 2005-11-21
description: ColorConvertOpを使って画像をグレースケールに変換します。
---
* 概要 [#t5e7fcf3]
`ColorConvertOp`を使って画像をグレースケールに変換します。
#download(https://lh6.googleusercontent.com/_9Z4BYR88imo/TQTJmEhFayI/AAAAAAAAAUo/x4JGGk_f08c/s800/ColorConvertOp.png)
* サンプルコード [#a2a42159]
#code(link){{
Image img = icon1.getImage();
BufferedImage source = new BufferedImage(
img.getWidth(this), img.getHeight(this),
BufferedImage.TYPE_INT_ARGB);
Graphics g = source.createGraphics();
g.drawImage(img, 0, 0, this);
g.dispose();
ColorConvertOp colorConvert = new ColorConvertOp(
ColorSpace.getInstance(ColorSpace.CS_GRAY), null);
BufferedImage destination = colorConvert.filter(source, null);
icon2 = new ImageIcon(destination);
}}
* 解説 [#s9c1238a]
用意したアイコンから、`BufferedImage`を作成し、これを`ColorConvertOp#filter`メソッドを使ってグレースケールに変換しています。
上記のサンプルでは、ラベルをクリックすると元画像とグレースケール画像とが切り替わるようになっています。
----
以下のように`GrayFilter.createDisabledImage`を使った場合よりきれいに変換できるようです。
#code{{
icon2 = new ImageIcon(GrayFilter.createDisabledImage(img));
}}
----
`GrayFilter`の代わりに、以下のような`RGBImageFilter`を継承したフィルタを使う方法もあります。
#code{{
class MyGrayFilter extends RGBImageFilter {
public int filterRGB(int x, int y, int argb) {
//int a = (argb >> 24) & 0xff;
int r = (argb >> 16) & 0xff;
int g = (argb >> 8) & 0xff;
int b = (argb ) & 0xff;
//http://ofo.jp/osakana/cgtips/grayscale.phtml
int m = (2 * r + 4 * g + b) / 7; //NTSC Coefficients
return (argb & 0xff000000) | (m<<16) | (m<<8) | (m);
}
}
//...
ImageProducer ip = new FilteredImageSource(img.getSource(), new MyGrayFilter());
icon2 = new ImageIcon(Toolkit.getDefaultToolkit().createImage(ip));
}}
----
`BufferedImage.TYPE_BYTE_GRAY`で、`BufferedImage`を作成して複写してもグレースケールに変換できますが、透過色を使用している場合はすこし注意が必要なようです(参考:[https://forums.oracle.com/thread/1373262 Swing - Color to Grayscale to Binary])。
#code{{
BufferedImage bi = new BufferedImage(w, h, BufferedImage.TYPE_BYTE_GRAY);
Graphics g = bi.createGraphics();
//g.setColor(Color.WHITE);
g.fillRect(0, 0, w, h); // pre-fill: alpha
g.drawImage(img, 0, 0, this);
g.dispose();
}}
* 参考リンク [#wb3b736d]
- [http://www.java2s.com/Code/Java/2D-Graphics-GUI/ImageColorGrayEffect.htm Image Color Gray Effect : Java examples (example source code) » 2D Graphics GUI » Image]
- [http://opus-i.biz/ opus-i | シンプル素材 テンプレート 音楽素材]
- %%[http://numata.aquasky.jp/programming/java/graphics/FilteringImage.html フィルタによる半透明な画像の作成]%%
- [http://ofo.jp/osakana/cgtips/grayscale.phtml osakana.factory - グレースケールのひみつ]
--via [http://programamemo2.blogspot.com/2007/08/blog-post_21.html プログラマメモ2: グレースケール]
- [https://forums.oracle.com/thread/1903279 Swing - image manipulation]
* コメント [#i2aa6deb]
#comment
- リンクを参考にして`RGBImageFilter`を使うサンプルを修正しました。 -- &user(aterai); &new{2007-08-21 (火) 13:06:10};
- グレイスケールからグレースケールに変更。 -- &user(aterai); &new{2008-01-10 (木) 14:31:00};
#comment