【问题标题】:How to scan an image using a fixed size block in Java?如何在 Java 中使用固定大小的块扫描图像?
【发布时间】:2014-02-09 22:37:40
【问题描述】:

我正在编写一个图像分析程序,它允许用户在图像上选择两个坐标。然后程序根据这两个点将图像裁剪为一个矩形。然后程序会找到图像的直方图分布。

我遇到的问题是我想将裁剪后的图像与包含该裁剪图像的另一个图像 (imagetwo) 进行比较。我目前的计划是通过创建一个与裁剪图像大小相同的块然后在 imagetwo 上移动来扫描 imagetwo,并在它移动时计算直方图分布。然后将块与裁剪后的图像进行比较。

我怎样才能做到这一点?我知道这需要一些 for 循环,但我很难弄清楚逻辑。

到目前为止的代码逻辑:

//Size of cropped image
int width = croppedimage.getWidth();
int height = croppedimage.getHeight();

Image tempImage;
Image imageTwo;

//Match pattern
for(x=0; x<width; x++){
 for(y=0; y<height; y++){
  tempImage.addPixel(imageTwo.get(x,y);
 }
}

这将检索图像 2 中具有裁剪图像大小的第一个块。但是我想一次一个像素地沿着 imageTwo 移动块。

【问题讨论】:

  • 我有点困惑。基本上,您正在从图像中裁剪出一个矩形。然后在原始图像上为原始图像的每个可能窗口定义一个大小相同的窗口(与裁剪后的图像相同)。这样直方图会在同一位置匹配吗?
  • 同一事物有两张图像,但角度不同(即,您为同一棵树拍摄两张照片)。这些图像共享一些相同的细节,但也显示了其他图片中没有的细节。我目前允许用户取出图像的一小部分,然后检查它是否在另一张图像上,如果是,那么它将根据匹配将图像合并在一起。

标签: java image image-processing


【解决方案1】:

听起来您将需要三个函数:一个用于获取图像的直方图,另一个用于比较两个直方图,第三个用于裁剪图像。此外,由于直方图仅适用于灰度图像(或单独的印刷色通道),我将假设这就是我们正在使用的 - 灰度图像。

假设你有这三个功能:

public int[] getHistogram(BufferedImage image){ ... }
public float getRelativeCorrelation(int[] hist1, int[] hist2){ ... }
public BufferedImage cropImage(BufferedImage image, int x, int y, int width, int height){ ... }

然后查看所有比较的循环是:

BufferedImage scene = ... ;// The image that you want to test
BufferedImage roi = ... ; //The cropped region of interest selected by your user


int sceneWidth  = scene.getWidth();
int sceneHeight = scene.getHeight();
int roiWidth    = roi.getWidth();
int roiHeight   = roi.getHeight();

int[] histROI = getHistogram(roi);

for (int y=0;y<sceneHeight-roiHeight+1;y++){
    for (int x=0;x<sceneWidth-roiWidth+1;x++){

          BufferedImage sceneROI = cropImage(scene, x,y, roiWidth, roiHeight);
          int[] histSceneROI = getHistogram(sceneROI);

          float comparison  = getRelativeCorrelation(histROI, histSceneROI);

          //todo: something with the comparison.

    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-09-20
    • 1970-01-01
    • 2011-07-19
    • 2013-04-14
    • 2020-01-26
    • 1970-01-01
    • 1970-01-01
    • 2015-10-12
    相关资源
    最近更新 更多