mirror of
https://github.com/bspeice/libcvautomation
synced 2025-07-01 13:56:49 -04:00
Add functions to return the center of a sub-image, rather than top-left corner
Fix up the build process to make sure out-of-tree builds work correctly Fix the example script not finding the cva-match binary correctly
This commit is contained in:
@ -163,7 +163,7 @@ void matchSubImage_a ( IplImage *rootImage, cvautomationList *subImageArray, int
|
||||
|
||||
/*
|
||||
* === FUNCTION ======================================================================
|
||||
* Name: matchSubImage_list_location
|
||||
* Name: matchSubImage_a_location
|
||||
* Description: Match a root image from location, and sub image from
|
||||
* an array of sub-images.
|
||||
* The list contains an element for each sub-image to specify its own
|
||||
@ -204,3 +204,216 @@ void matchSubImage_a_location ( const char *rootImageFileName, cvautomationList
|
||||
cvReleaseImage( &rootImage );
|
||||
}
|
||||
|
||||
/*
|
||||
* === FUNCTION ======================================================================
|
||||
* Name: matchSubImage_center
|
||||
* Returns: CvPoint if a match is found, null if not
|
||||
* The code is mostly the same as matchSubImage, but the difference
|
||||
* here is that it returns the center of the sub-image, rather than
|
||||
* the top-left corner.
|
||||
* =====================================================================================
|
||||
*/
|
||||
|
||||
CvPoint matchSubImage_center ( IplImage *rootImage, IplImage *subImage, int searchMethod, double tolerance )
|
||||
{
|
||||
/* We have the two OpenCV images we want, go ahead and find if there are any matches */
|
||||
IplImage *result;
|
||||
CvPoint minloc, maxloc; /* Location for the match - depending on search algorithm,
|
||||
the result may be in either minloc or maxloc */
|
||||
CvPoint badpoint; /* (-1, -1), used to indicate an error */
|
||||
double minval, maxval;
|
||||
int rootImage_width, rootImage_height;
|
||||
int subImage_width, subImage_height;
|
||||
int result_width, result_height;
|
||||
|
||||
badpoint.x = badpoint.y = -1;
|
||||
|
||||
/* Make sure we have valid images */
|
||||
if ( rootImage == 0 || subImage == 0) {
|
||||
/* Otherwise return invalid */
|
||||
minloc.x = minloc.y = -1;
|
||||
return minloc;
|
||||
}
|
||||
|
||||
/* Set up the parameters for our result image */
|
||||
rootImage_width = rootImage->width;
|
||||
rootImage_height = rootImage->height;
|
||||
subImage_width = subImage->width;
|
||||
subImage_height = subImage->height;
|
||||
result_width = rootImage_width - subImage_width + 1;
|
||||
result_height = rootImage_height - subImage_height + 1;
|
||||
|
||||
/* Initialize our result image */
|
||||
result = cvCreateImage( cvSize( result_width, result_height ), IPL_DEPTH_32F, 1 );
|
||||
|
||||
/* Compute the result image */
|
||||
cvMatchTemplate( rootImage, subImage, result, searchMethod );
|
||||
cvMinMaxLoc( result, &minval, &maxval, &minloc, &maxloc, 0 );
|
||||
|
||||
/* Free memory for the result image
|
||||
* Note that we don't control the root or sub image,
|
||||
* so don't mess with those */
|
||||
cvReleaseImage( &result );
|
||||
|
||||
/* Make sure that we have enough correlation to return a result,
|
||||
* and then return the match location*/
|
||||
/* Return the match location */
|
||||
if ( searchMethod == CV_TM_SQDIFF || searchMethod == CV_TM_SQDIFF_NORMED )
|
||||
{
|
||||
if ( minval < tolerance )
|
||||
{
|
||||
CvPoint resultPoint = minloc;
|
||||
/* Center the image before returning */
|
||||
resultPoint.x += (subImage->width) / 2;
|
||||
resultPoint.y += (subImage->height) / 2;
|
||||
|
||||
return resultPoint;
|
||||
}
|
||||
else
|
||||
return badpoint;
|
||||
}
|
||||
else
|
||||
{
|
||||
if ( maxval > tolerance )
|
||||
{
|
||||
CvPoint resultPoint;
|
||||
resultPoint = maxloc;
|
||||
/* Center the image before returning */
|
||||
resultPoint.x += (subImage->width) / 2;
|
||||
resultPoint.y += (subImage->height) / 2;
|
||||
|
||||
return resultPoint;
|
||||
}
|
||||
else
|
||||
return badpoint;
|
||||
}
|
||||
|
||||
} /* ----- end of function matchSubImage ----- */
|
||||
|
||||
/*
|
||||
* === FUNCTION ======================================================================
|
||||
* Name: matchSubImage_location_center
|
||||
* Description: Match a root image and sub image from filename
|
||||
* Most of the code is the same as matchSubImage_location, but use
|
||||
* matchSubImage_center as the lower-level call to make sure that
|
||||
* we get the center of the sub-image.
|
||||
* =====================================================================================
|
||||
*/
|
||||
CvPoint matchSubImage_location_center ( const char *rootImage_location, const char *subImage_location, int searchMethod, double tolerance )
|
||||
{
|
||||
/* This is basically a wrapper for matchSubImage( IplImage, IplImage )
|
||||
* All we do is load the images from the given filenames, and then
|
||||
* pass off the result to the above-named function */
|
||||
IplImage *rootImage;
|
||||
rootImage = cvLoadImage( rootImage_location, CV_LOAD_IMAGE_COLOR );
|
||||
|
||||
IplImage *subImage;
|
||||
subImage = cvLoadImage( subImage_location, CV_LOAD_IMAGE_COLOR );
|
||||
|
||||
CvPoint return_point;
|
||||
return_point.x = return_point.y = -1;
|
||||
|
||||
/* Make sure we have good images */
|
||||
if ( rootImage == 0 || subImage == 0 )
|
||||
{
|
||||
/* Return error */
|
||||
return return_point;
|
||||
}
|
||||
|
||||
return_point = matchSubImage_center( rootImage, subImage, searchMethod, tolerance );
|
||||
|
||||
/* Free up the memory we created */
|
||||
cvReleaseImage( &rootImage );
|
||||
cvReleaseImage( &subImage );
|
||||
|
||||
/* Our return_point will already be NULL if there's no match */
|
||||
return return_point;
|
||||
|
||||
} /* ----- end of function matchSubImage ----- */
|
||||
|
||||
/*
|
||||
* === FUNCTION ======================================================================
|
||||
* Name: matchSubImage_a_center
|
||||
* Description: Match a root image and sub image from an array of sub-images.
|
||||
* The list contains an element for each sub-image to specify its own
|
||||
* searchMethod and threshold value.
|
||||
* Most of the code is the same as matchSubImage_a, but we use
|
||||
* matchSubImage_center as the underlying function to make sure we
|
||||
* get the center of the sub-image, rather than the top-left corner
|
||||
* =====================================================================================
|
||||
*/
|
||||
void matchSubImage_a_center ( IplImage *rootImage, cvautomationList *subImageArray, int listSize )
|
||||
{
|
||||
/* This is also a higher-end wrapper for matchSubImage, but is mostly aimed
|
||||
* at making python support for multiple images very easy. */
|
||||
|
||||
CvPoint resultPoint;
|
||||
cvautomationList curr;
|
||||
|
||||
int x = 0;
|
||||
for ( x = 0; x < listSize; x++ )
|
||||
{
|
||||
curr = subImageArray[x];
|
||||
if ( subImageArray[x].cvaImage != 0 )
|
||||
resultPoint = matchSubImage ( rootImage, curr.cvaImage, curr.searchMethod, curr.tolerance );
|
||||
else
|
||||
{
|
||||
/* We have a sub-image filename, go ahead and create the actual image. */
|
||||
IplImage *subImage;
|
||||
subImage = cvLoadImage( curr.fileName, CV_LOAD_IMAGE_COLOR );
|
||||
|
||||
resultPoint = matchSubImage_center ( rootImage, subImage, curr.searchMethod, curr.tolerance );
|
||||
|
||||
cvReleaseImage( &subImage );
|
||||
}
|
||||
|
||||
curr.resultPoint = resultPoint;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* === FUNCTION ======================================================================
|
||||
* Name: matchSubImage_a_location_center
|
||||
* Description: Match a root image from location, and sub image from
|
||||
* an array of sub-images.
|
||||
* The list contains an element for each sub-image to specify its own
|
||||
* searchMethod and threshold value.
|
||||
* Most of the code is the same as matchSubImage_a_location, but we
|
||||
* use matchSubImage_center in order to return the center of the sub-
|
||||
* image, rather than the top-left corner.
|
||||
* =====================================================================================
|
||||
*/
|
||||
void matchSubImage_a_location_center ( const char *rootImageFileName, cvautomationList *subImageArray, int listSize )
|
||||
{
|
||||
/* This is also a higher-end wrapper for matchSubImage, but is mostly aimed
|
||||
* at making python support for multiple images very easy. */
|
||||
|
||||
CvPoint resultPoint;
|
||||
cvautomationList curr;
|
||||
|
||||
IplImage *rootImage;
|
||||
rootImage = cvLoadImage( rootImageFileName, CV_LOAD_IMAGE_COLOR );
|
||||
|
||||
int x = 0;
|
||||
for ( x = 0; x < listSize; x++ )
|
||||
{
|
||||
curr = subImageArray[x];
|
||||
if ( subImageArray[x].cvaImage != 0 )
|
||||
resultPoint = matchSubImage ( rootImage, curr.cvaImage, curr.searchMethod, curr.tolerance );
|
||||
else
|
||||
{
|
||||
/* We have a sub-image filename, go ahead and create the actual image. */
|
||||
IplImage *subImage;
|
||||
subImage = cvLoadImage( curr.fileName, CV_LOAD_IMAGE_COLOR );
|
||||
|
||||
resultPoint = matchSubImage_center ( rootImage, subImage, curr.searchMethod, curr.tolerance );
|
||||
|
||||
cvReleaseImage( &subImage );
|
||||
}
|
||||
|
||||
curr.resultPoint = resultPoint;
|
||||
}
|
||||
|
||||
cvReleaseImage( &rootImage );
|
||||
}
|
||||
|
||||
|
@ -143,7 +143,8 @@ CvPoint matchSubImage_X11( char *displayLocation, IplImage *subImage, int search
|
||||
/* Slow alternative for non-24-bit-depth images */
|
||||
/* I don't know how this works either. */
|
||||
/* TODO: Figure out how this code works and document it */
|
||||
Colormap colormap = DefaultColormap( &display, DefaultScreen ( &display ));
|
||||
Colormap colormap;
|
||||
colormap = DefaultColormap( display, DefaultScreen ( display ));
|
||||
for ( x = 0; x < rootImage->width; x++ ) {
|
||||
for ( y = 0; y < rootImage->height; y++ ) {
|
||||
color.pixel = XGetPixel ( rootImage, x, y );
|
||||
@ -173,7 +174,7 @@ CvPoint matchSubImage_X11( char *displayLocation, IplImage *subImage, int search
|
||||
|
||||
/*
|
||||
* === FUNCTION ======================================================================
|
||||
* Name: matchSubImage_X11
|
||||
* Name: matchSubImage_X11_location
|
||||
* Description: Match a sub image using the X11 root window as root, from filename
|
||||
* =====================================================================================
|
||||
*/
|
||||
@ -187,6 +188,8 @@ CvPoint matchSubImage_X11_location( char *displayLocation, const char *subImage_
|
||||
subImage = cvLoadImage( subImage_location, CV_LOAD_IMAGE_COLOR );
|
||||
|
||||
CvPoint return_point;
|
||||
return_point.x = -1;
|
||||
return_point.y = -1;
|
||||
|
||||
/* Make sure we have a good image */
|
||||
if ( subImage == 0 )
|
||||
@ -205,3 +208,197 @@ CvPoint matchSubImage_X11_location( char *displayLocation, const char *subImage_
|
||||
return return_point;
|
||||
|
||||
} /* ----- end of function matchSubImage_X11_location ----- */
|
||||
|
||||
/*
|
||||
* === FUNCTION ======================================================================
|
||||
* Name: matchSubImage_X11_center
|
||||
* Description: Match a sub image using the X11 root window as root
|
||||
* The code is mostly the same as matchSubImage_X11, but use
|
||||
* matchSubImage_center to get the center of the result, rather than
|
||||
* the top-left corner.
|
||||
* =====================================================================================
|
||||
*/
|
||||
CvPoint matchSubImage_X11_center( char *displayLocation, IplImage *subImage, int searchMethod, int tolerance )
|
||||
{
|
||||
/* First things first, grab the root X window and convert it to
|
||||
* the IplImage format.
|
||||
* Much of this code was ripped lovingly from:
|
||||
* http://opencv.willowgarage.com/wiki/ximage2opencvimage */
|
||||
IplImage *X_IPL;
|
||||
CvSize imageSize;
|
||||
CvPoint resultPoint;
|
||||
|
||||
XImage *rootImage;
|
||||
XColor color;
|
||||
Display *display;
|
||||
Screen *screen;
|
||||
unsigned long rmask, gmask, bmask;
|
||||
unsigned long rshift, rbits,
|
||||
gshift, gbits,
|
||||
bshift, bbits;
|
||||
unsigned char colorChannel[3];
|
||||
|
||||
int startX = 0, startY = 0;
|
||||
unsigned int width, height;
|
||||
|
||||
/* Setting up the X screengrab is the first order of business */
|
||||
display = XOpenDisplay(displayLocation);
|
||||
screen = DefaultScreenOfDisplay(display);
|
||||
|
||||
width = screen->width;
|
||||
height = screen->height;
|
||||
|
||||
rootImage = XGetImage( display, DefaultRootWindow(display),
|
||||
startX, startY, width, height,
|
||||
AllPlanes, ZPixmap );
|
||||
|
||||
/* Make sure that we're good to go before going any farther */
|
||||
if ( rootImage == NULL || display == NULL || screen == NULL )
|
||||
{
|
||||
fprintf( stderr, "Couldn't grab the root window!" );
|
||||
resultPoint.x = -1;
|
||||
resultPoint.y = -1;
|
||||
return resultPoint;
|
||||
}
|
||||
|
||||
/* Set up the OpenCV Image */
|
||||
imageSize.width = rootImage->width;
|
||||
imageSize.height = rootImage->height;
|
||||
X_IPL = cvCreateImage( imageSize, IPL_DEPTH_8U, 3 ); /* 3 channels - RGB */
|
||||
|
||||
/* This if block converts the X root window to an IPL image. See you on the other side! */
|
||||
unsigned int x, y; /* To be used later */
|
||||
|
||||
if ( screen->depths->depth == 24 )
|
||||
{
|
||||
/* Actually convert the XImage to Ipl */
|
||||
rmask = screen->root_visual->red_mask;
|
||||
gmask = screen->root_visual->green_mask;
|
||||
bmask = screen->root_visual->blue_mask;
|
||||
|
||||
/* I honestly have no clue how most of the below code works */
|
||||
/* TODO: Figure out how this code works and document it */
|
||||
rshift = 0; rbits = 0;
|
||||
while ( !(rmask & 1) ){
|
||||
rshift++;
|
||||
rmask >>= 1; /* Right bitshift by 1 */
|
||||
}
|
||||
while (rmask & 1) {
|
||||
rbits++;
|
||||
rmask >>= 1; /* Right bitshift by 1 */
|
||||
}
|
||||
if (rbits > 8) {
|
||||
rshift += rbits - 8;
|
||||
rbits = 8;
|
||||
}
|
||||
|
||||
gshift = 0; gbits = 0;
|
||||
while ( !(gmask & 1) ){
|
||||
gshift++;
|
||||
gmask >>= 1; /* Right bitshift by 1 */
|
||||
}
|
||||
while (gmask & 1) {
|
||||
gbits++;
|
||||
gmask >>= 1; /* Right bitshift by 1 */
|
||||
}
|
||||
if (gbits > 8) {
|
||||
gshift += gbits - 8;
|
||||
gbits = 8;
|
||||
}
|
||||
|
||||
bshift = 0; bbits = 0;
|
||||
while ( !(bmask & 1) ){
|
||||
bshift++;
|
||||
bmask >>= 1; /* Right bitshift by 1 */
|
||||
}
|
||||
while (bmask & 1) {
|
||||
bbits++;
|
||||
bmask >>= 1; /* Right bitshift by 1 */
|
||||
}
|
||||
if (bbits > 8) {
|
||||
bshift += bbits - 8;
|
||||
bbits = 8;
|
||||
}
|
||||
|
||||
for ( x = 0; x < rootImage->width; x++) {
|
||||
for ( y = 0; y < rootImage->height; y++) {
|
||||
color.pixel = XGetPixel ( rootImage, x, y );
|
||||
colorChannel[0] = ((color.pixel >> bshift) & ((1 << bbits) - 1)) << (8 - bbits);
|
||||
colorChannel[1] = ((color.pixel >> gshift) & ((1 << gbits) - 1)) << (8 - gbits);
|
||||
colorChannel[2] = ((color.pixel >> rshift) & ((1 << rbits) - 1)) << (8 - rbits);
|
||||
CV_IMAGE_ELEM(X_IPL, uchar, y, x * X_IPL->nChannels) = colorChannel[0];
|
||||
CV_IMAGE_ELEM(X_IPL, uchar, y, x * X_IPL->nChannels + 1) = colorChannel[1];
|
||||
CV_IMAGE_ELEM(X_IPL, uchar, y, x * X_IPL->nChannels + 2) = colorChannel[2];
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
/* Slow alternative for non-24-bit-depth images */
|
||||
/* I don't know how this works either. */
|
||||
/* TODO: Figure out how this code works and document it */
|
||||
Colormap colormap;
|
||||
colormap = DefaultColormap( display, DefaultScreen ( display ));
|
||||
for ( x = 0; x < rootImage->width; x++ ) {
|
||||
for ( y = 0; y < rootImage->height; y++ ) {
|
||||
color.pixel = XGetPixel ( rootImage, x, y );
|
||||
XQueryColor( display, colormap, &color );
|
||||
CV_IMAGE_ELEM(X_IPL, uchar, y, x * X_IPL->nChannels) = color.blue;
|
||||
CV_IMAGE_ELEM(X_IPL, uchar, y, x * X_IPL->nChannels + 1) = color.green;
|
||||
CV_IMAGE_ELEM(X_IPL, uchar, y, x * X_IPL->nChannels + 2) = color.red;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Now that we've got the image we want as X_IPL, time to actually do the comparison.
|
||||
* However, we don't want to do any more work than we have to - send our images off
|
||||
* to matchSubImage in libopencvautomation-opencv. */
|
||||
|
||||
resultPoint = matchSubImage_center ( X_IPL, subImage, searchMethod, tolerance );
|
||||
|
||||
/* Clean up the CV image we created, as well as all X resources */
|
||||
XDestroyImage( rootImage );
|
||||
XCloseDisplay( display );
|
||||
cvReleaseImage( &X_IPL );
|
||||
|
||||
/* Return and be done */
|
||||
return resultPoint;
|
||||
|
||||
} /* ----- end of function matchSubImage_X11_center ----- */
|
||||
|
||||
/*
|
||||
* === FUNCTION ======================================================================
|
||||
* Name: matchSubImage_X11_location_center
|
||||
* Description: Match a sub image using the X11 root window as root, from filename
|
||||
* =====================================================================================
|
||||
*/
|
||||
CvPoint matchSubImage_X11_location_center( char *displayLocation, const char *subImage_location, int searchMethod, int tolerance )
|
||||
{
|
||||
/* This is basically a wrapper for matchSubImage_X11( char *display, IplImage )
|
||||
* All we do is load the sub-image from the given filename, and then
|
||||
* pass off the result to the above-named function */
|
||||
|
||||
IplImage *subImage;
|
||||
subImage = cvLoadImage( subImage_location, CV_LOAD_IMAGE_COLOR );
|
||||
|
||||
CvPoint return_point;
|
||||
return_point.x = -1;
|
||||
return_point.y = -1;
|
||||
|
||||
/* Make sure we have a good image */
|
||||
if ( subImage == 0 )
|
||||
{
|
||||
/* Return error */
|
||||
return return_point;
|
||||
}
|
||||
|
||||
return_point = matchSubImage_X11_center( displayLocation, subImage, searchMethod, tolerance );
|
||||
|
||||
/* Free up the memory we created */
|
||||
cvReleaseImage( &subImage );
|
||||
|
||||
/* Our return_point will already be bad if there's no match,
|
||||
* we don't need to worry about setting it. */
|
||||
return return_point;
|
||||
|
||||
} /* ----- end of function matchSubImage_X11_location_center ----- */
|
||||
|
Reference in New Issue
Block a user