CVAM: First functioning code and libcvautomation

This commit is contained in:
Bradlee Speice 2012-06-21 12:19:55 -04:00
parent 978ba8567f
commit 7818d188e8
7 changed files with 407 additions and 96 deletions

108
Makefile
View File

@ -1,16 +1,104 @@
all: clean cva-match
#===============================================================================
#
# File: Makefile
# Description:
#
# Usage: make (generate executable(s) )
# make clean (remove objects, executables, prerequisits )
# make tarball (generate compressed archive )
# make zip (generate compressed archive )
#
# Author: Bradlee Speice
# Email: bspeice@uncc.edu
# Created: 6/21/2012
#
#===============================================================================
debug: cva-match-debug
gcc cva-match.o `pkg-config --libs opencv` -o cva-match
cva-match: cva-match.o
gcc cva-match.o `pkg-config --libs opencv` -o cva-match
CC = gcc
CCP = g++
CFLAGS = -c -g -Wall -fPIC
LDFLAGS = -g
SYS_LIBS =
TARBALL_EXCLUDE = "*.{o,gz,zip}"
ZIP_EXCLUDE = *.o *.gz *.zip
cva-match-debug: cva-match-debug.o
gcc src/cva-match.c -DDEBUG `pkg-config --cflags opencv` -c -o cva-match.o
SRC_DIR = src
OBJ_DIR = obj
BIN_DIR = bin
LIB_DIR = lib
cva-match.o:
gcc src/cva-match.c `pkg-config --cflags opencv` -c -o cva-match.o
CV_FLAGS = `pkg-config --cflags opencv`
CV_LIBS = `pkg-config --libs opencv`
X11_FLAGS = `pkg-config --cflags x11`
X11_LIBS = `pkg-config --libs x11`
EXECUTABLE = $(BIN_DIR)/cva-match
LIBRARIES = $(LIB_DIR)/libcvautomation.so
SOURCES = $(SRC_DIR)/cva-match.c $(SRC_DIR)/libcvautomation-x11.c $(SRC_DIR)/libcvautomation-opencv.c
CVAM_OBJECTS = $(OBJ_DIR)/cva-match.o
CVAM_SOURCES = $(SRC_DIR)/cva-match.c
LIBCVAM_OBJECTS = $(OBJ_DIR)/libcvautomation-opencv.o $(OBJ_DIR)/libcvautomation-x11.o
LIBCVAM_SOURCES = $(SRC_DIR)/libcvautomation-opencv.c $(SRC_DIR)/libcvautomation-x11.c
TARGETS = $(OBJECTS) $(LIBRARIES) $(EXECUTABLE)
#---------- targets --------------------------------------
all: .dirs $(TARGETS)
#---------- installation directories ---------------------
.dirs:
mkdir -p $(SRC_DIR)
mkdir -p $(OBJ_DIR)
mkdir -p $(BIN_DIR)
mkdir -p $(LIB_DIR)
touch .dirs
#---------- link libcvautomation -------------------------
$(LIB_DIR)/libcvautomation.so: $(LIBCVAM_OBJECTS)
$(CC) $(LDFLAGS) -o $@ $(LIBCVAM_OBJECTS) $(SYS_LIBS) $(CV_LIBS) $(X11_LIBS) -shared
#---------- link cva-match program -----------------------
$(BIN_DIR)/cva-match: $(CVAM_OBJECTS)
$(CC) $(LDFLAGS) -o $@ $(CVAM_OBJECTS) $(SYS_LIBS) -L$(LIB_DIR) -lcvautomation
#---------- compile libcvautomation-opencv.c ------------
$(OBJ_DIR)/libcvautomation-opencv.o: src/libcvautomation-opencv.c
$(CC) $(CFLAGS) -o $@ src/libcvautomation-opencv.c $(CV_FLAGS)
#---------- compile libcvautomation-x11.c ----------------
$(OBJ_DIR)/libcvautomation-x11.o: src/libcvautomation-x11.c
$(CC) $(CFLAGS) -o $@ src/libcvautomation-x11.c $(X11_FLAGS)
#---------- compile cva-match ----------------------------
$(OBJ_DIR)/cva-match.o: $(CVAM_SOURCES)
$(CC) $(CFLAGS) -o $@ $(CVAM_SOURCES)
#---------- tarball --------------------------------------
tarball:
localdir=`pwd`; localdir=$${localdir##*/}; \
rm --force $$localdir.tar.gz; \
tar --exclude=$(TARBALL_EXCLUDE) \
--create \
--gzip \
--verbose \
--file $$localdir.tar.gz *
#---------- zip ------------------------------------------
zip:
localdir=`pwd`; localdir=$${localdir##*/}; \
zip -r $$localdir.zip * -x $(ZIP_EXCLUDE)
#---------- clear up -------------------------------------
clean:
rm *.o cva-match -f
rm -f $(OBJ_DIR)/*
rm -f $(BIN_DIR)/*
rm -f $(LIB_DIR)/*
distclean:
rm -rf $(OBJ_DIR)
rm -rf $(BIN_DIR)
rm -rf $(LIB_DIR)
rm .dirs

View File

@ -19,55 +19,23 @@
#include <stdio.h>
#include <unistd.h>
#include <getopt.h>
#include <limits.h>
#include "cv.h"
#include "highgui.h"
#include "libcvautomation-opencv.h"
/*
* === FUNCTION ======================================================================
* Name: usage
* Description:
* =====================================================================================
*/
void usage ( )
{
fprintf( stderr,
"cva-match -r <root_image> -s <sub_image> \n\
\n\
This program uses OpenCV in order to recognize an image within an image.\n\
The return code is how many matches were found - return 0 for no matches,\n\
1 for one match, etc.\n\
\n\
Usage: \n\
\n\
\t-h, --help:\tDisplay this usage message.\n\
\t-u, --usage:\tDisplay this usage message.\n\
\t-r, --root-image:\tLocation of the root image to compare against.\n\
\t-s, --sub-image:\tLocation of the sub-image to find in root.\n\
\t-p, --separator:\tSeparator of the X and Y coordinates.\n\
\n\
If you have any questions, comments, concerns, email bspeice@uncc.edu\n" );
exit (0);
} /* ----- end of function usage ----- */
#include "cva-match.h"
int main( int argc, char** argv )
{
IplImage *root; /* The root image */
IplImage *sub; /* The sub-image */
IplImage *res;
CvPoint minloc, maxloc; /* The location of our match */
double minval, maxval;
int root_width, root_height;
int sub_width, sub_height;
int res_width, res_height;
CvPoint result_point;
result_point.x = result_point.y = 0;
/* Set up everything for getopt */
char *separator = ",";
char *root_location = "root.png";
char *sub_location = "sub.png";
int threshold = INT_MAX;
int search_method = CV_TM_SQDIFF;
/* Start getopt */
while (1)
@ -79,13 +47,15 @@ int main( int argc, char** argv )
{"root-image", required_argument, 0, 'r'},
{"sub-image", required_argument, 0, 's'},
{"separator", required_argument, 0, 'p'},
{"search-method",required_argument, 0, 'm'},
{"threshold", required_argument, 0, 't'},
{0, 0, 0, 0}
};
int option_index = 0;
opterr = 0;
int c = getopt_long (argc, argv, "hur:s:p:",
int c = getopt_long (argc, argv, "hur:s:p:m:t:",
long_options, &option_index);
/* We're done with parsing options */
@ -117,6 +87,14 @@ int main( int argc, char** argv )
separator = optarg;
break;
case 'm':
search_method = atoi(optarg);
break;
case 't':
threshold = atoi(optarg);
break;
case '?':
/* Error routine */
break;
@ -127,54 +105,53 @@ int main( int argc, char** argv )
};
}
/* Use INT_MAX for the threshold, due to the way CV_TM_SQDIFF is calculated */
result_point = matchSubImage_location( root_location, sub_location, search_method, threshold );
/* load reference image */
root = cvLoadImage( root_location, CV_LOAD_IMAGE_COLOR );
/* always check */
if( root == 0 ) {
fprintf( stderr, "Cannot load the root image %s!\n", root_location );
return 0;
}
/* load sub image */
sub = cvLoadImage( sub_location, CV_LOAD_IMAGE_COLOR );
/* always check */
if( sub == 0 ) {
fprintf( stderr, "Cannot load the sub-image %s!\n", sub_location );
return 0;
}
/* get image's properties */
root_width = root->width;
root_height = root->height;
sub_width = sub->width;
sub_height = sub->height;
res_width = root_width - sub_width + 1;
res_height = root_height - sub_height + 1;
/* create new image for template matching computation */
res = cvCreateImage( cvSize( res_width, res_height ), IPL_DEPTH_32F, 1 );
/* choose template matching method to be used */
cvMatchTemplate( root, sub, res, CV_TM_SQDIFF );
/*cvMatchTemplate( root, sub, res, CV_TM_SQDIFF_NORMED );
cvMatchTemplate( root, sub, res, CV_TM_CCORR );
cvMatchTemplate( root, sub, res, CV_TM_CCORR_NORMED );
cvMatchTemplate( root, sub, res, CV_TM_CCOEFF );
cvMatchTemplate( root, sub, res, CV_TM_CCOEFF_NORMED );*/
cvMinMaxLoc( res, &minval, &maxval, &minloc, &maxloc, 0 );
/* Output the match location */
printf ("%i%s%i\n", minloc.x, separator, minloc.y );
/* free memory */
cvReleaseImage( &root );
cvReleaseImage( &sub );
cvReleaseImage( &res );
if ( result_point.x != -1 && result_point.y != -1 )
/* Output the match location */
printf ("%i%s%i\n", result_point.x, separator, result_point.y );
return 0;
}
/*
* === FUNCTION ======================================================================
* Name: usage
* Description: I really shouldn't need to write this
* =====================================================================================
*/
void usage ( )
{
fprintf( stderr, "\n\
cva-match -r <root_image> -s <sub_image> \n\
\n\
This program uses OpenCV in order to recognize an image within an image.\n\
The return code is how many matches were found - return 0 for no matches,\n\
1 for one match, etc.\n\
\n\
Usage: \n\
\n\
\t-h, --help:\t\tDisplay this usage message.\n\
\t-u, --usage:\t\tDisplay this usage message.\n\
\t-r, --root-image:\tLocation of the root image to compare against.\n\
\t-s, --sub-image:\tLocation of the sub-image to find in root.\n\
\t-p, --separator:\tSeparator of the X and Y coordinates.\n\
\t-t, --threshold:\tSet how strict the match is - 50 is recommended lowest value.\n\
\t\t\t\tNote: When using CCORR or CCOEFF threshold works in opposite direction,\n\
\t\t\t\tso -50 is recommended highest value.\n\
\t-m, --search-method:\tSet which method is used to search for sub-images.\n\
\t\t\t\tMethods:\n\
\t\t\t\t\tCV_TM_SQDIFF = 0\n\
\t\t\t\t\tCV_TM_SQDIFF_NORMED = 1\n\
\t\t\t\t\tCV_TM_CCORR = 2\n\
\t\t\t\t\tCV_TM_CCORR_NORMED = 3\n\
\t\t\t\t\tCV_TM_CCOEFF = 4\n\
\t\t\t\t\tCV_TM_COEFF_NORMED = 5\n\
\n\
If you have any questions, comments, concerns, email bspeice@uncc.edu\n" );
exit (0);
} /* ----- end of function usage ----- */

24
src/cva-match.h Normal file
View File

@ -0,0 +1,24 @@
/*
* =====================================================================================
*
* Filename: cva-match.h
*
* Description:
*
* Version: 1.0
* Created: 06/21/2012 08:34:21 AM
* Revision: none
* Compiler: gcc
*
* Author: Bradlee Speice, bspeice@uncc.edu
* Organization: MOSAIC at University of North Carolina at Charlotte
*
* =====================================================================================
*/
#ifndef CVA_MATCH_H
#define CVA_MATCH_H
void usage ();
int main(int, char**);
#endif /* CVA_MATCH_H */

View File

@ -0,0 +1,125 @@
/*
* =====================================================================================
*
* Filename: libcvautomation-opencv.c
*
* Description:
*
* Version: 1.0
* Created: 06/21/2012 08:34:21 AM
* Revision: none
* Compiler: gcc
*
* Author: Bradlee Speice, bspeice@uncc.edu
* Organization: MOSAIC at University of North Carolina at Charlotte
*
* =====================================================================================
*/
#include "libcvautomation-opencv.h"
/*
* === FUNCTION ======================================================================
* Name: matchSubImage
* Returns: CvPoint if a match is found, null if not
* =====================================================================================
*/
CvPoint matchSubImage ( IplImage *rootImage, IplImage *subImage, int searchMethod, double threshold )
{
/* 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 < threshold )
return minloc;
else
return badpoint;
}
else
{
if ( maxval > threshold )
return maxloc;
else
return badpoint;
}
} /* ----- end of function matchSubImage ----- */
/*
* === FUNCTION ======================================================================
* Name: matchSubImage_location
* Description: Match a root image and sub image from filename
* =====================================================================================
*/
CvPoint matchSubImage_location ( char *rootImage_location, char *subImage_location, int searchMethod, double threshold )
{
/* 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( rootImage, subImage, searchMethod, threshold );
/* 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 ----- */

View File

@ -0,0 +1,39 @@
/*
* =====================================================================================
*
* Filename: libcvautomation-opencv.h
*
* Description: Function definitions for opencv functionality
*
* Version: 1.0
* Created: 06/21/2012 08:34:21 AM
* Revision: none
* Compiler: gcc
*
* Author: Bradlee Speice, bspeice@uncc.edu
* Organization: MOSAIC at University of North Carolina at Charlotte
*
* =====================================================================================
*/
#ifndef LIBCVAUTOMATION_OPENCV_H
#define LIBCVAUTOMATION_OPENCV_H
#include <opencv/cv.h>
#include <opencv/highgui.h>
/* It should be noted that the following are the macros for template matching:
* CV_TM_SQDIFF (default)
* CV_TM_SQDIFF_NORMED
* CV_TM_CCORR
* CV_TM_CCORR_NORMED
* CV_TM_CCOEFF
* CV_TM_CCOEFF_NORMED
*/
/* Match a root image and sub image */
CvPoint matchSubImage ( IplImage *rootImage, IplImage *subImage, int searchMethod, double threshold );
/* Match a root image and sub image from filename */
CvPoint matchSubImage_location ( char *rootImage_location, char *subImage_location, int searchMethod, double threshold );
#endif /* LIBCVAUTOMATION_OPENCV_H */

19
src/libcvautomation-x11.c Normal file
View File

@ -0,0 +1,19 @@
/*
* =====================================================================================
*
* Filename: libcvautomation-x11.c
*
* Description:
*
* Version: 1.0
* Created: 06/21/2012 08:34:21 AM
* Revision: none
* Compiler: gcc
*
* Author: Bradlee Speice, bspeice@uncc.edu
* Organization: MOSAIC at University of North Carolina at Charlotte
*
* =====================================================================================
*/
#include <stdlib.h>

39
src/libcvautomation-x11.h Normal file
View File

@ -0,0 +1,39 @@
/*
* =====================================================================================
*
* Filename: libcvautomation-x11.h
*
* Description: Function definitions for X11 operations
*
* Version: 1.0
* Created: 06/21/2012 08:34:21 AM
* Revision: none
* Compiler: gcc
*
* Author: Bradlee Speice, bspeice@uncc.edu
* Organization: MOSAIC at University of North Carolina at Charlotte
*
* =====================================================================================
*/
#ifndef LIBCVAUTOMATION_X11_H
#define LIBCVAUTOMATION_X11_H
#include <opencv/cv.h>
#include <opencv/highgui.h>
#include <X11/Xlib.h>
/* It should be noted that the following are the macros for template matching:
* CV_TM_SQDIFF (default)
* CV_TM_SQDIFF_NORMED
* CV_TM_CCORR
* CV_TM_CCORR_NORMED
* CV_TM_CCOEFF
* CV_TM_CCOEFF_NORMED
*/
cvPoint matchSubImage_X11(IplImage); /* Match a sub image using the X11 root window as root */
cvPoint matchSubImage_X11(char *, int=CV_TM_SQDIFF ); /* Match a sub image using X11 as root, from filename */
#endif /* LIBCVAUTOMATION_X11_H */