(黒)などの数値の RBG コードを持っている場合、-16777216
このカラー コードを使用して他の同様の黒の色合いを見つけるにはどうすればよいですか?
白ではないすべてのピクセルをマークして、画像をモノクロに変換しようとしてい-16777216
ます。ただし、さまざまな色合いの黒が見つかることがよくありますが、それらは正確に一致しないため失われます。
編集:ちょっと困っています。この色を使用して黒の色合いを見つけようとすると、他のピクセルを白に変換するときにそれらを無視できるようになります。これが私の結果です。
ソース:
結果:
コード:
package test;
import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.File;
import java.net.URL;
import javax.imageio.ImageIO;
public class Test
{
public static void main(String[] args)
{
try
{
BufferedImage source = ImageIO.read( new URL("http://i.imgur.com/UgdqfUY.png"));
//-16777216 = black:
BufferedImage dest = makeMonoChromeFast(source, -16777216);
File result = new File("D:/result.png");
ImageIO.write(dest, "png", result);
}
catch (Exception e)
{
e.printStackTrace();;
}
}
public static BufferedImage makeMonoChromeFast(BufferedImage source, int foreground)
{
int background = -1; //white;
Color fg = new Color(foreground);
int color = 0;
for (int y = 0; y < source.getHeight(); y++)
{
for (int x = 0; x < source.getWidth(); x++)
{
color = source.getRGB(x, y);
if ( color == foreground )
continue;
if (! isIncluded(fg, color, 50))
source.setRGB(x, y, background);;
}
}
return source;
}
public static boolean isIncluded(Color target, int pixelColor, int tolerance)
{
Color pixel = new Color(pixelColor);
int rT = target.getRed();
int gT = target.getGreen();
int bT = target.getBlue();
int rP = pixel.getRed();
int gP = pixel.getGreen();
int bP = pixel.getBlue();
return(
(rP-tolerance<=rT) && (rT<=rP+tolerance) &&
(gP-tolerance<=gT) && (gT<=gP+tolerance) &&
(bP-tolerance<=bT) && (bT<=bP+tolerance) );
}
}