如何比较一个图像是另一个图像的一部分 [英] how to compare one image is part of another image

查看:83
本文介绍了如何比较一个图像是另一个图像的一部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

hi
我想比较一个图像是另一个图像的一部分不是

请给我任何想法或算法r代码

hi I want to compare a image is a part of another image is not
pls send me any idea or algorithm r code

推荐答案

一个天真的解决方案是迭代外部图像的像素,并将它们与内部图像的像素进行比较,如果它们匹配则表示您已经找到了图片。



一个非常简单的实现可以是这样的;



A naive solution would be to iterate over the pixels of the "outer" image and compare them to the pixels of the "inner" image, and if they match then that means you''ve found the part of the image.

A very simple implementation of this can look like this;

package com.bornander.imagetest;

import java.awt.Point;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;

public class Program {
	
  private static Point findImage(String outerFilename, String innerFilename) throws Exception {
    BufferedImage outer = ImageIO.read(new File(outerFilename));
    BufferedImage inner = ImageIO.read(new File(innerFilename));
		
    int startPixel = inner.getRGB(0, 0);
    for(int ox = 0; ox < outer.getWidth(); ++ox) {
      for(int oy = 0; oy < outer.getHeight(); ++oy) {
        if (outer.getRGB(ox, ox) == startPixel) {
	  int px = ox;
	  int py = oy;
	  boolean isMatch = true;

	  for(int ix = 0; ix < inner.getWidth() && (px + ix) < outer.getWidth() && isMatch; ++ix) {
	    for(int iy = 0; iy < inner.getHeight() && (py + iy) < outer.getHeight() && isMatch; ++iy) {
  	      if (inner.getRGB(ix, iy) != outer.getRGB(px +ix,  py + iy)) {
                isMatch = false;
              }
            }
          }

          if (isMatch) 
            return new Point(px, py);
        }
      }
    }
    return null;
  }

  public static void main(String[] args) throws Exception {
    Point point = findImage("C:\\Temp\\outer.png", "C:\\Temp\\innerpart.png");
    System.out.println("Found inner in outer at " + point.x + ", " + point.y);
  }
}





再次这有点天真,完整的实现可能应该寻找像素相似而且不仅仅是完全匹配。



希望这会有所帮助,

Fredrik



Again this is a bit naive and a full implementation should probably look for pixels that are similar and not only exact matches.

Hope this helps,
Fredrik


这篇关于如何比较一个图像是另一个图像的一部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆