如何在Java中使用固定大小的块扫描图像?

如何在Java中使用固定大小的块扫描图像?,java,image,image-processing,Java,Image,Image Processing,我正在写一个图像分析程序,它允许用户在图像上选择两个坐标。然后程序根据这两点将图像裁剪成矩形。然后程序将找到图像的直方图分布 我遇到的问题是,我想将剪切的图像与包含该剪切图像的另一个图像(imagetwo)进行比较。我目前的计划是扫描imagetwo,方法是创建一个与裁剪图像大小相同的块,然后在imagetwo上移动,在移动时计算直方图分布。然后将这些块与裁剪后的图像进行比较 我怎样才能做到这一点?我知道这需要很多for循环,但我很难找到逻辑 迄今为止的代码逻辑: //Size of cropp

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

我遇到的问题是,我想将剪切的图像与包含该剪切图像的另一个图像(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);
 }
}
//裁剪图像的大小
int width=crappedimage.getWidth();
int height=crappedimage.getHeight();
图像和图像;
图像二;
//匹配模式

对于(x=0;x听起来你需要三个函数:一个用于获取图像的直方图,另一个用于比较两个直方图,第三个用于裁剪图像。此外,由于直方图仅适用于灰度图像(或单独处理颜色通道),我将假设这就是我们正在处理的灰度图像

让我们假设您有这三个函数:

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.

    }
}
buffereImage场景=…;//要测试的图像
BuffereImage roi=…;//用户选择的裁剪感兴趣区域
int sceneWidth=scene.getWidth();
int sceneHeight=scene.getHeight();
int roiWidth=roi.getWidth();
int roiHeight=roi.getHeight();
int[]histROI=getHistogram(roi);

对于(int y=0;yI,我有点困惑。基本上你是从图像中裁剪出一个矩形。然后在原始图像上为原始图像的每个可能窗口定义一个相同大小的窗口(作为裁剪图像)。这样直方图将在同一位置匹配?有两个相同的图像,但角度不同(即,您拍摄了同一棵树的两张照片)。这些图像共享一些相同的细节,但也显示了另一张照片中没有的细节。我当前允许用户从其中一张图像中取出一小部分,然后检查它是否出现在另一张图像中,如果是,则会根据匹配情况将图像合并在一起。