Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented Image.Find(image) function #113

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions Source/Image.cc
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,49 @@ void Image::Destroy (void)

////////////////////////////////////////////////////////////////////////////////

// Find source from target (this=target, image=source)
// If found, returns relative coordinates(Point) from target image
// If not found, returns {-1,-1}
Point Image::Find(const Image & image)
{
if (!IsValid() || !image.IsValid())
return{ -1, -1 };

const auto targetWidth = GetWidth();
const auto targetHeight = GetHeight();
const auto sourceWidth = image.GetWidth();
const auto sourceHeight = image.GetHeight();

if (targetWidth >= sourceWidth && targetHeight >= sourceHeight)
{
bool mismatch;
for (int y = 0; y < targetHeight - sourceHeight; y++)
{
for (int x = 0; x < targetWidth - sourceWidth; x++)
{
mismatch = false;
for (int y2 = 0; !mismatch && y2 < sourceHeight; y2++)
{
for (int x2 = 0; !mismatch && x2 < sourceWidth; x2++)
{
auto nCol1 = GetPixel(x + x2, y + y2);
auto nCol2 = image.GetPixel(x2, y2);
if (nCol1 != nCol2)
mismatch = true;
}
}

if (mismatch == false) // == found
return{ x, y };
}
}
}

return{ -1, -1 };
}

////////////////////////////////////////////////////////////////////////////////

Color Image::GetPixel (const Point& point) const
{
return GetPixel (point.X, point.Y);
Expand Down
2 changes: 2 additions & 0 deletions Source/Image.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ class ROBOT_EXPORT Image
bool Create (uint16 w, uint16 h);
void Destroy (void);

Point Find(const Image &image);

uint16 GetWidth (void) const { return mWidth; }
uint16 GetHeight (void) const { return mHeight; }
uint32 GetLength (void) const { return mLength; }
Expand Down