- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试创建一个 java.awt.geom.Area(或任何使用 Shape 接口(interface)的对象)来描述一组创建复杂形状的点。这是一个例子:
我正在尝试创建一个 Area 对象(或多个 Area 对象)来描述黄色区域。我有一种获取黄色像素坐标的简单方法,但我不知道如何创建包含所有黄色点的 Area 对象(或多个 Area 对象)。
这个 stackoverflow 问题似乎非常相关 Create closed polygon from boundary points ) 但正在使用 Matlab。我认为其他人在这里问了一个非常相似的问题 ( Convert a list java.awt.geom.Point2D to a java.awt.geom.Area ),但答案中提供的链接已失效。
最佳答案
例如
import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.geom.Area;
import javax.imageio.ImageIO;
import java.io.File;
import java.net.URL;
import java.util.Date;
import javax.swing.*;
/* See also http://stackoverflow.com/q/7052422/418556 */
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 {
URL url = new URL("http://i.stack.imgur.com/aGBuT.png");
final BufferedImage outline = ImageIO.read(url);
displayAndWriteImage(outline, "motorcycle-01.png");
createAndWrite(
outline, Color.white, false, 60, "YellowBlobOutline.png");
}
}
关于java - 从点创建 java.awt.geom.Area,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18591749/
在下面的查询中,为什么我们使用As foo (geom) 而不是As geom? foo() 做什么? SQL 查询 SELECT ST_SRID(geom) AS srid, ST_SRID(ST_
我有一组点,我想在 Java 中将其变成闭合的多边形。我目前正在尝试使用 java.awt.geom.Point2D 和 java.awt.geom.Area,但不知道如何将一组点变成一个区域。 我想
我正在编写一个偏移函数来测试移动物体是否接触空间中的其他可移动和固定物体。 为了测试这一点,我需要查看它们的边界矩形是否相交。 我想测试两个可移动物体是否像这样接触: table 车 - id:整数(
我尝试使用 ggplotly 函数将 ggplot geom-tile 图表转换为 plotly。然而,我意识到结果是不同的。请引用以下链接查看差异。除此之外,ggplotly 图表还缺少颜色条。请帮
在 ggplot2 – issue with overlay of lines and errorbars ,我提出了一个使用 lapply(.) 的答案以特定顺序生成几何图形组,以便每个级别的点/线
我试图在图中绘制两个“geom_vline()”。 下面的代码适用于一条垂直线: x=1:7 y=1:7 df1 = data.frame(x=x,y=y) vertical.lines <- c(2
我一直在阅读 vignette on extending ggplot2 ,但我对如何制作可以向绘图添加多个几何图形的单个几何图形有些困惑。 ggplot2 geoms 中已经存在多个几何图形,例如,
我在普林斯顿各个地点的交通流量数据框。 # dput(count) structure(list(intersection = structure(c(11L, 9L, 10L, 12L, 6L, 3
我在 postgres 数据库中有 geom。当我使用这个查询时 SELECT ST_AsGeoJSON(st_simplify(geom,0.01))::json As geometry FR
本文整理了Java中org.geolatte.geom.codec.WktDecoder类的一些代码示例,展示了WktDecoder类的具体用法。这些代码示例主要来源于Github/Stackover
本文整理了Java中org.geolatte.geom.codec.WkbEncoder类的一些代码示例,展示了WkbEncoder类的具体用法。这些代码示例主要来源于Github/Stackover
sf包似乎是一种比 sp 更易于用户使用的处理空间数据的方法。 .例如,如果我有一组纬度/经度坐标,我可以使用 ggplot2 的开发版本轻松绘图。 : library(sf) devtools::i
我想在使用stat_boxplot(geom = "errorbar")时更改晶须的线宽: set.seed(42) df <- data.frame(cond = factor( rep(c("A"
我有如下地理数据(但数据量大得多): library(tidyverse) library(gganimate) n % sort data % arrange(longitude) %>% muta
Hi I want to initialize an array of Point2D (Point2D.Double []) Somehow I fail, I get the following
根据 D3 文档,可以为 hull 方法分配自定义访问器来获取 x 和 y 坐标。 Hull documentation 我想使用这些自定义访问器,但我无法弄清楚语法。这就是我所做的,这基本上是一个不
我想使用 Java 的 Area 类 (java.awt.geom.Area) 对各种多边形进行减法和求交操作。 在许多这些情况下,减法操作可能会将源区域分成两部分。在这些情况下,我需要返回两个 Ar
我正在尝试在 R 中制作一个多面绘图线图,无论有多少组可用于分面,它都会生成一个图。当运行下面的代码时,我得到了一个理想的绘图,如下图所示 # R 3.6.0 # most of these pack
与其他 ggplot geom 相比,geom_vline 似乎在色彩美学方面“表现不佳”。我想弄清楚我是否对 geom_vline 有误解,或者这是否是对 geom_vline 设计的疏忽。 # F
我首先给你我的示例代码: x <- runif(1000,0, 5) y <- c(runif(500, 0, 2), runif(500, 3,5)) A <- data.frame("X"=x,"
我是一名优秀的程序员,十分优秀!