Java 将黑白图像加载到二进制数组中
我正在建立一个可以识别空中图像的程序 我可以加载图像并将其转换为黑白,但我在尝试将像素值放入数组时遇到了问题,因此我可以使用联合查找链接白色像素和黑色像素的簇 以下是我到目前为止的情况:Java 将黑白图像加载到二进制数组中,java,arrays,image,find,union,Java,Arrays,Image,Find,Union,我正在建立一个可以识别空中图像的程序 我可以加载图像并将其转换为黑白,但我在尝试将像素值放入数组时遇到了问题,因此我可以使用联合查找链接白色像素和黑色像素的簇 以下是我到目前为止的情况: import javafx.stage.FileChooser; import javax.imageio.ImageIO; import javax.swing.*; import java.awt.*; import java.awt.image.BufferedImage;
import javafx.stage.FileChooser;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
public class Image {
public static void main(String args[])
{
new Image();
}
public Image()
{
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception ex) {
}
JFrame frame = new JFrame("Image");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //closes application properly
frame.add(new ImagePane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class ImagePane extends JPanel {
private BufferedImage image;
private BufferedImage bwImage;
public ImagePane() {
try {
FileChooser fileChooser = new FileChooser();
image = ImageIO.read(new File("C:/Users/Connor/Desktop/image.jpg"));
this.image = image;
bwImage = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_BYTE_BINARY);
this.bwImage = bwImage;
Graphics2D g2d = bwImage.createGraphics();
g2d.drawImage(image, 0, 0, this);
g2d.dispose();
} catch (IOException ex) {
ex.printStackTrace();
}
}
public Dimension getPreferredSize() {
Dimension size = super.getPreferredSize();
if (image != null) {
size = new Dimension(image.getWidth() * 2, image.getHeight());
}
return size;
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (image != null) {
int x = (getWidth() - (image.getWidth() * 2)) / 2;
int y = (getHeight() - (image.getHeight()));
g.drawImage(image, x, y, this);
x += image.getWidth();
g.drawImage(bwImage, x, y, this);
}
}
}
}
我希望获得如下输出:
{ 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1 },
{ 0, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0 },
{ 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0 },
{ 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1 },
{ 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0 },
{ 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0 },
{ 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0 }
因此,一种方法是使用
位集
下面是一个小代码示例:
//img is your black and white image of type BufferedImage
// Get the width and height of your image
int width = img.getWidth();
int height = img.getHeight();
// create a BitSet of the correct size
BitSet bits = new BitSet(width * height);
// Iterate through your image's pixels and set the correct bits.
for (int y = 0; y < height; ++y)
{
for (int x = 0; x < width; ++x)
{
// Check against 0xffffffff which is the RGB value of white.
if (img.getRGB(x, y) == 0xffffffff)
{
bits.set(y * width + x);
}
}
}
//img是BuffereImage类型的黑白图像
//获取图像的宽度和高度
int width=img.getWidth();
int height=img.getHeight();
//创建大小正确的位集
位集位=新位集(宽度*高度);
//遍历图像的像素并设置正确的位。
对于(int y=0;y
在此之后,您将得到一个位集,其中一个位表示白色,另一个位表示黑色。您现在可以随意对其进行数学分析。尝试使用
位集。迭代你的图像并设置位(因此1代表白色,0代表黑色),非常感谢!我以前没见过BitSet,但它似乎满足了我的需要。它输出的白色像素的位置,从我可以告诉,只需要找出如何连接他们现在!位集基本上是一个一维位数组(这是保护值为1或0(此处为白色或黑色)的最简单方法)。为了使它成为“二维”,你需要对索引进行一些计算。基本上,它是当前行*列数+当前列
,因此如果您想访问6x6数组中的[2][4],它就是2*6+4
。在Java中,使用一维数组通常比使用二维数组更快(尽管读取和维护变得更加困难,因此在用例中使用二维数组可能更好)