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:
Bradlee Speice 2012-06-22 15:38:41 -04:00
parent df26d0a95c
commit dab4d28932
7 changed files with 457 additions and 13 deletions

View File

@ -1,6 +1,6 @@
#Need to include the -Ilibcvautomation so that libcvautomation.h can find everything that it needs
AM_CFLAGS = -Wall -O2 -I$(top_srcdir)/include -I$(top_srcdir)/libcvautomation
AM_LDFLAGS = -L$(top_srcdir)/libcvautomation/.libs -lcvautomation `pkg-config --libs x11` `pkg-config --libs opencv`
AM_CFLAGS = -Wall -O2 -I$(top_srcdir)/include #-I$(top_srcdir)/include/libcvautomation
AM_LDFLAGS = -L../libcvautomation/.libs -lcvautomation `pkg-config --libs x11` `pkg-config --libs opencv`
#Build only if we're building the examples
bin_PROGRAMS = cva-match

View File

@ -44,8 +44,12 @@ int main( int argc, char** argv )
int tolerance = INT_MAX;
int search_method = CV_TM_SQDIFF;
int useX = 0; /* bool useX = false; */
int useCenter = 0;
char *xDisplay;
/* Set the default display */
xDisplay = "";
/* Set up the linked list for slave images */
basic_list *list_head = NULL, *list_curr = NULL, *list_prev = NULL;
@ -62,13 +66,14 @@ int main( int argc, char** argv )
{"search-method",required_argument, 0, 'm'},
{"tolerance", required_argument, 0, 't'},
{"x-root", optional_argument, 0, 'x'},
{"center", no_argument, 0, 'c'},
{0, 0, 0, 0}
};
int option_index = 0;
opterr = 0;
int c = getopt_long (argc, argv, "hur:s:p:m:t:x::",
int c = getopt_long (argc, argv, "hur:s:p:m:t:x::c",
long_options, &option_index);
/* We're done with parsing options */
@ -125,6 +130,8 @@ int main( int argc, char** argv )
}
break;
case 'c':
useCenter = 1;
case '?':
/* Error routine */
@ -147,9 +154,13 @@ int main( int argc, char** argv )
{
sub_location = list_curr->fileName;
if (useX)
if (useX && useCenter)
result_point = matchSubImage_X11_location_center( xDisplay, sub_location, search_method, tolerance );
else if (useX && !useCenter)
result_point = matchSubImage_X11_location( xDisplay, sub_location, search_method, tolerance );
else
else if (!useX && useCenter)
result_point = matchSubImage_location_center( root_location, sub_location, search_method, tolerance );
else /* if (!useX && !useCenter) */
result_point = matchSubImage_location( root_location, sub_location, search_method, tolerance );
if ( result_point.x != -1 && result_point.y != -1 )
@ -211,6 +222,8 @@ Usage: \n\
\t\t\t\tto use the default display (not specifically :0.0)\n\
\t\t\t\tPlease note also that the '-x' must come at the end\n\
\t\t\t\tsince it is an optional argument.\n\
\t-c, --center:\t\tThe output points should be centered on the sub-image, rather\n\
\t\t\t\tthan the top-left corner.\n\
\n\
If you have any questions, comments, concerns, email bspeice@uncc.edu\n" );

View File

@ -32,13 +32,25 @@
/* Match a root image and sub image */
CvPoint matchSubImage ( IplImage *rootImage, IplImage *subImage, int searchMethod, double tolerance );
/* Match a root image and sub image, return the center */
CvPoint matchSubImage_center ( IplImage *rootImage, IplImage *subImage, int searchMethod, double tolerance );
/* Match a root image and sub image from filename */
CvPoint matchSubImage_location ( const char *rootImage_location, const char *subImage_location, int searchMethod, double tolerance );
/* Match a root image and sub image from filename, return the center */
CvPoint matchSubImage_location_center ( const char *rootImage_location, const char *subImage_location, int searchMethod, double tolerance );
/* Match a root image and sub images from an array of images */
void matchSubImage_a ( IplImage *rootImage, cvautomationList *subImageArray, int listSize );
/* Match a root image and sub images from an array of images, return the center */
void matchSubImage_a_center ( IplImage *rootImage, cvautomationList *subImageArray, int listSize );
/* Match a root image and sub images from an array of images */
void matchSubImage_a_location ( const char *rootImageFileName, cvautomationList *subImageArray, int listSize );
/* Match a root image and sub images from an array of images, return the center */
void matchSubImage_a_location_center ( const char *rootImageFileName, cvautomationList *subImageArray, int listSize );
#endif /* LIBCVAUTOMATION_OPENCV_H */

View File

@ -30,7 +30,16 @@
* CV_TM_CCOEFF_NORMED
*/
CvPoint matchSubImage_X11( char *displayLocation, IplImage *subImage, int searchMethod, int tolerance ); /* Match a sub image using the X11 root window as root */
CvPoint matchSubImage_X11_location( char *displayLocation, const char *rootImage_location, int search_method, int tolerance ); /* Match a sub image using X11 as root, from filename */
/* Match a sub image using the X11 root window as root */
CvPoint matchSubImage_X11( char *displayLocation, IplImage *subImage, int searchMethod, int tolerance );
/* Match a sub image using the X11 root window as root, return the center */
CvPoint matchSubImage_X11_center( char *displayLocation, IplImage *subImage, int searchMethod, int tolerance );
/* Match a sub image using X11 as root, from filename */
CvPoint matchSubImage_X11_location( char *displayLocation, const char *subImage_location, int search_method, int tolerance );
/* Match a sub image using X11 as root, from filename, return the center */
CvPoint matchSubImage_X11_location_center( char *displayLocation, const char *subImage_location, int search_method, int tolerance );
#endif /* LIBCVAUTOMATION_X11_H */

View File

@ -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 );
}

View File

@ -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 ----- */

View File

@ -19,10 +19,10 @@
# REVISION: ---
#===============================================================================
CVAMATCH="`dirname $0`/../bin/cva-match"
CVAMATCH=`which cva-match`
if [ ! -x $CVAMATCH ]; then
echo "The cva-match binary was not found or is not executable. Please fix this!"
if [ ! -x "$CVA_MATCH" ]; then
echo "Could not find the cva-match binary! Can't continue..."
exit 255
fi