- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试实现在图像中查找图像的各种方法。我从严格匹配开始。让我们用 image
来表示我们要搜索的图像,用 bigImage
来表示我们要在中搜索的图像。
/** Finds image in bigImage by exact pixel match (all pixels must be exactly the same color).
*
* @param image the smaller image you want to find
* @param bigImage the big image you're searching in
* @return Rect object describing the location where the small image was found. Returns null if nothing was found.
*/
public static Rect findByExactMatch(BufferedImage image, BufferedImage bigImage) {
//I marked these final so that I don't accidentally change them later
final int iw = image.getWidth();
final int ih = image.getHeight();
final int bw = bigImage.getWidth();
final int bh = bigImage.getHeight();
//Loop from 0 to big image width/height MINUS the small image width/height
//The MINUS there is, because once you are at the end, the small image overlaps to undefined area
for(int rect_x=0, mrx=bw-iw; rect_x<mrx; rect_x++) {
for(int rect_y=0, mry=bh-ih; rect_y<mry; rect_y++) {
//This is where pixel looping begins
int x = 0;
int y = 0;
for (; x < iw; x++) {
for (; y < ih; y++) {
//Get RGB returns 0x00rrggbb
if(image.getRGB(x, y)!=
bigImage.getRGB(x+rect_x, y+rect_y)) {
//If the color does not match, break back to the rectangular search
//WITHOUT -1 THE VALUE OVERFLOWS ON NEXT ITERATION (damnit, debuged this like an idiot!!!)
y = x = Integer.MAX_VALUE-1;
break;
}
}
}
//This statement asks if the loop ended normally
// - otherwise, the x and y are MAX_INT and greater than iw
if(x==iw) {
return Rect.byWidthHeight(rect_x, rect_y, iw, ih);
}
}
}
//Nothing found - return null
return null;
}
正如你所看到的,函数非常简单,大部分代码都是注释。前两个循环移动我们正在比较的位置的帧,而内部循环则比较当前偏移处的小图像和大图像。
用法如下:
public static void main(String[] args)
{
//The small image to search for
BufferedImage thing = loadFromPath("thing.png");
//The big image to search in
BufferedImage screenshot = loadFromPath("screenshot.png");
if(thing!=null && screenshot!=null) {
Rect pos = autoclick.ScreenWatcher.findByExactMatch(thing, screenshot);
if(pos!=null) {
System.out.println("Found object: "+pos);
//Draw rectangle on discovered position
Graphics2D graph = screenshot.createGraphics();
graph.setColor(Color.RED);
graph.drawRect(pos.top, pos.left, pos.width, pos.height);
graph.dispose();
//Save the file for review
try {
ImageIO.write(screenshot, "png", new File("output.png"));
} catch (IOException ex) {
Logger.getLogger("wtf goes here?").log(Level.SEVERE, null, ex);
}
}
}
}
不幸的是,它似乎不起作用(红框显示程序识别的位置):
然而,当我向我的 friend 提示并想向他表明邪恶代码不起作用时,它突然起作用了:
需要注意的重要一点是,如果图像的第一列不是白色,它就可以工作。或者在我看来是这样。
我创建了一个测试项目,其中包含所有必需的可用文件:https://gist.github.com/Darker/f08b2fbf1795af9ebbe2 。默认情况下,它期望“thing.png”作为image
,“screenshot.png”作为“bigImage”。
最佳答案
您的内部匹配循环(x 和 y)编码错误;仔细看看。您在循环之前初始化两个循环变量,这意味着当第一列中没有不匹配时,y 已递增到 ih 的值,从而在 x 的所有后续传递中绕过 y 循环-循环。
解决此问题的一种方法是将 x 和 y 初始化移动到各自的 for 语句中(它们所属的位置),并在 for(rect_y...)
语句上放置一个标签。如果像素不匹配,请使用 Continueecty
中止 x 和 y 循环,而不是中断。这避免了操作 x/y 以及之后的人工检查的需要。
for (rect_x ...) {
label: for (rect_y...) {
for (x = 0; ...) {
for (y = 0; ....) {
if (mismatch) {
continue label;
}
}
}
return "match";
}
}
return null;
为了让事情变得不那么复杂,您还可以将内部两个循环重构为单独的方法,并在发生不匹配时简单地使用 return
来中断循环。
关于java - 为什么我在图像函数中查找图像匹配不完全匹配?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28175816/
我有两张来自同一场景的立体相机的图像,但几乎没有不同的视角(imgLeft 和 imgRight)。现在,我想在左侧图像中找到右侧图像的 ROI(下图中的红色矩形)。我需要非常快地执行此操作,因为我正
我正在尝试检测照片是否代表预定义的充满数据的公式模板。 我是图像处理和 OpenCV 的新手,但我的第一次尝试是使用 FlannBasedMatcher 并比较检测到的关键点的数量。 有更好的方法吗?
我的问题在这里显示:problem statement。我有一个模板图像,必须在相机图像中检测到。在检测到它之后,我必须使用仿射变换来规范化摄像机图像。我的工作目标是确定十字架。我尝试将SURF功能用
我正在尝试使用 ORB 检测器/描述符和 Flann 或蛮力匹配器在更大的目标图像(allimg.jpg 包含 3 张面孔)中识别源图像(c1.jpg- 一张脸)。 c1.jpg 是通过裁剪/复制从
我是一名优秀的程序员,十分优秀!