如何在Java中找到不同颜色的阴影? [英] How to find different shades of a color in Java?
问题描述
如果我有一个数字的RBG代码,例如 -16777216
(黑色),我如何使用此颜色代码找到其他类似的黑色阴影? p>
我试图将所有不是 -16777216
的像素标记为白色,将图像转换为单色。然而,通常有不同的黑色阴影被发现,但他们失去了,因为他们不是一个完全匹配。
编辑:我有一点麻烦。当我尝试使用这种颜色来寻找阴影的黑色,所以我可以忽略他们,而将其他像素转换为白色,这是我的结果:
资料来源: p>
结果:
代码:
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);
文件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; //白色;
颜色fg =新颜色(前景);
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 (gP- tolerance< = gT)& (bT <= bP +耐受性)和(bT <= bP +耐受性)之间的差异(gT <= gP +耐受性)和&
}
}
可以使用这个查找颜色与差异容差方法。
public static boolean isIncluded(Color target,Color pixel,int tolerance ){
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 (gP- tolerance< = gT)& (bT <= bP +耐受性)和(bT-耐受性<= bT)耐受性(gT <= gP +耐受性)和&
}
这里用于获取大纲( -06.jpg
)( motorcycle.jpg
),同时剥离淡灰色叠加层。
motorcycle.jpg
motorcycle-03.png
ImageOutline.java
此代码需要一些耐心(运行时)。请参阅平滑锯齿形路径,了解执行相同操作的代码的速度更快。
import java.awt。*;
import java.awt.image.BufferedImage;
import java.awt.geom.Area;
import javax.imageio.ImageIO;
import java.io.File;
import java.util.Date;
import javax.swing。*;
/ *摩托车图片由ShutterStock提供
http://www.shutterstock.com/pic-13585165/stock-vector-travel-motorcycle-silhouette.html * /
class ImageOutline {
public static Area getOutline(BufferedImage image,Color color,boolean include,int tolerance){
Area area = new Area();
for(int x = 0; x< image.getWidth(); x ++){
for(int y = 0; y< image.getHeight(); y ++){
= new Color(image.getRGB(x,y));
if(include){
if(isIncluded(color,pixel,tolerance)){
Rectangle r = new Rectangle(x,y,1,1)
area.add(new Area(r));
}
} else {
if(!isIncluded(color,pixel,tolerance)){
Rectangle r = new Rectangle(x,y,1,1)
area.add(new Area(r));
}
}
}
}
返回区域;
}
public static boolean isIncluded(Color target,Color pixel,int tolerance){
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 (gP- tolerance< = gT)& (bT <= bP +耐受性)和(bT-耐受性<= bT)耐受性(gT <= gP +耐受性)和&
}
public static BufferedImage drawOutline(int w,int h,Area area){
final BufferedImage result = new BufferedImage(
w,
h,
BufferedImage.TYPE_INT_RGB);
Graphics2D g = result.createGraphics();
g.setColor(Color.white);
g.fillRect(0,0,w,h);
g.setClip(area);
g.setColor(Color.red);
g.fillRect(0,0,w,h);
g.setClip(null);
g.setStroke(new BasicStroke(1));
g.setColor(Color.blue);
g.draw(area);
返回结果;
}
public static BufferedImage createAndWrite(
BufferedImage image,
color color,
boolean include,
int tolerance,
String name)
throws Exception {
int w = image.getWidth();
int h = image.getHeight();
System.out.println(Get Area:+ new Date()+ - + name);
Area area = getOutline(image,color,include,tolerance);
System.out.println(Got Area:+ new Date()+ - + name);
final BufferedImage result = drawOutline(w,h,area);
displayAndWriteImage(result,name);
返回结果;
}
public static void displayAndWriteImage(BufferedImage image,String fileName)throws Exception {
ImageIO.write(image,png,new File(fileName)
JOptionPane.showMessageDialog(null,new JLabel(new ImageIcon(image)));
}
public static void main(String [] args)throws Exception {
final BufferedImage outline = ImageIO.read(new File(motorcycle.jpg));
BufferedImage crop = outline.getSubimage(17,35,420,270);
displayAndWriteImage(crop,motorcycle-01.png);
BufferedImage crude = createAndWrite(crop,Color.white,false,60,motorcycle-02.png);
BufferedImage combo = createAndWrite(crude,Color.red,true,0,motorcycle-03.png);
}
}
在问题中看到的代码,容差为150,我看到了。
If I have the RBG code of a number, such as -16777216
(black), how can I find other similar shades of black using this color code?
I'm trying to convert an image to monochrome by marking all pixels which are not -16777216
to be white. However, often there are varying shades of black which are found, but they are lost because they are not an exact match.
Edit: I'm having a bit of trouble. When I try to use this color to find shades of black, so I can ignore them while converting the other pixels to white, this is my result:
Source:
Result:
Code:
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) );
}
}
You might use this 'look for color with difference tolerance' method.
public static boolean isIncluded(Color target, Color pixel, int tolerance) {
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) );
}
Here it is used to get the outline (motorcycle-03.jpg
) of the motorcycle (motorcycle.jpg
), while stripping out the 'faint gray overlay'.
motorcycle.jpg
motorcycle-03.png
ImageOutline.java
This code requires some patience (when running). See Smoothing a jagged path for code that does the same thing much faster.
import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.geom.Area;
import javax.imageio.ImageIO;
import java.io.File;
import java.util.Date;
import javax.swing.*;
/* Motorcycle image courtesy of ShutterStock
http://www.shutterstock.com/pic-13585165/stock-vector-travel-motorcycle-silhouette.html */
class ImageOutline {
public static Area getOutline(BufferedImage image, Color color, boolean include, int tolerance) {
Area area = new Area();
for (int x=0; x<image.getWidth(); x++) {
for (int y=0; y<image.getHeight(); y++) {
Color pixel = new Color(image.getRGB(x,y));
if (include) {
if (isIncluded(color, pixel, tolerance)) {
Rectangle r = new Rectangle(x,y,1,1);
area.add(new Area(r));
}
} else {
if (!isIncluded(color, pixel, tolerance)) {
Rectangle r = new Rectangle(x,y,1,1);
area.add(new Area(r));
}
}
}
}
return area;
}
public static boolean isIncluded(Color target, Color pixel, int tolerance) {
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) );
}
public static BufferedImage drawOutline(int w, int h, Area area) {
final BufferedImage result = new BufferedImage(
w,
h,
BufferedImage.TYPE_INT_RGB);
Graphics2D g = result.createGraphics();
g.setColor(Color.white);
g.fillRect(0,0,w,h);
g.setClip(area);
g.setColor(Color.red);
g.fillRect(0,0,w,h);
g.setClip(null);
g.setStroke(new BasicStroke(1));
g.setColor(Color.blue);
g.draw(area);
return result;
}
public static BufferedImage createAndWrite(
BufferedImage image,
Color color,
boolean include,
int tolerance,
String name)
throws Exception {
int w = image.getWidth();
int h = image.getHeight();
System.out.println("Get Area: " + new Date() + " - " + name);
Area area = getOutline(image, color, include, tolerance);
System.out.println("Got Area: " + new Date() + " - " + name);
final BufferedImage result = drawOutline(w,h,area);
displayAndWriteImage(result, name);
return result;
}
public static void displayAndWriteImage(BufferedImage image, String fileName) throws Exception {
ImageIO.write(image, "png", new File(fileName));
JOptionPane.showMessageDialog(null, new JLabel(new ImageIcon(image)));
}
public static void main(String[] args) throws Exception {
final BufferedImage outline = ImageIO.read(new File("motorcycle.jpg"));
BufferedImage crop = outline.getSubimage(17,35,420,270);
displayAndWriteImage(crop, "motorcycle-01.png");
BufferedImage crude = createAndWrite(crop, Color.white, false, 60, "motorcycle-02.png");
BufferedImage combo = createAndWrite(crude, Color.red, true, 0, "motorcycle-03.png");
}
}
With the code seen in the question, with a tolerance of 150, I see this.
这篇关于如何在Java中找到不同颜色的阴影?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!