이미지의 이미지 인식 (haystack) 이미지 (바늘)를 찾으려면 여기를 클릭하십시오.C#
일을 간단하게하기 위해 내 데스크톱의 스크린 샷 두 장을 가져옵니다. 하나의 풀 사이즈 (건초 더미)와 작은 하나 (바늘). 그런 다음 건초 더미 이미지를 반복하여 바늘 이미지를 찾습니다.
- 캡처 니들 건초 스크린 샷
- 루프 덤 통해 덤 대해 찾아 [I] == 바늘의 제 1 픽셀
- [2 사실 인 경우]의 마지막 픽셀에 2 내지 루프 바늘과 haystack [i]을 비교하십시오.
예상 결과 : 바늘 이미지가 올바른 위치에 있습니다.
이미 일부 좌표/너비/높이 (A)에서 작동하고 있습니다.
그러나 때로는 비트가 "꺼져있는 것"이므로 은 일치가 발견되지 않습니다. (B).
내가 잘못 할 수 있습니까? 어떤 제안이라도 환영합니다. 감사.
var needle_height = 25;
var needle_width = 25;
var haystack_height = 400;
var haystack_width = 500;
A. 예 입력 - 매치
var needle = screenshot(5, 3, needle_width, needle_height);
var haystack = screenshot(0, 0, haystack_width, haystack_height);
var result = findmatch(haystack, needle);
B. 예의 입력 - NO 매치
var needle = screenshot(5, 5, needle_width, needle_height);
var haystack = screenshot(0, 0, haystack_width, haystack_height);
var result = findmatch(haystack, needle);
1. 캡처 바늘과 건초 더미 이미지
private int[] screenshot(int x, int y, int width, int height)
{
Bitmap bmp = new Bitmap(width, height, PixelFormat.Format32bppArgb);
Graphics.FromImage(bmp).CopyFromScreen(x, y, 0, 0, bmp.Size);
var bmd = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height),
ImageLockMode.ReadOnly, bmp.PixelFormat);
var ptr = bmd.Scan0;
var bytes = bmd.Stride * bmp.Height/4;
var result = new int[bytes];
Marshal.Copy(ptr, result, 0, bytes);
bmp.UnlockBits(bmd);
return result;
}
2. 시도 일치를 찾을 수
public Point findmatch(int[] haystack, int[] needle)
{
var firstpixel = needle[0];
for (int i = 0; i < haystack.Length; i++)
{
if (haystack[i] == firstpixel)
{
var y = i/haystack_height;
var x = i % haystack_width;
var matched = checkmatch(haystack, needle, x, y);
if (matched)
return (new Point(x,y));
}
}
return new Point();
}
3. 확인 전체 경기
public bool checkmatch(int[] haystack, int[] needle, int startx, int starty)
{
for (int y = starty; y < starty + needle_height; y++)
{
for (int x = startx; x < startx + needle_width; x++)
{
int haystack_index = y * haystack_width + x;
int needle_index = (y - starty) * needle_width + x - startx;
if (haystack[haystack_index] != needle[needle_index])
return false;
}
}
return true;
}
그건 속임수 였어. 너 정말 천재 야. 코딩 개선 방법에 대한 귀하의 도움과 노력에 진심으로 감사드립니다. – fanti