mirror of
				https://github.com/bspeice/libcvautomation
				synced 2025-11-03 18:00:43 -05:00 
			
		
		
		
	CVAM: Initial detection program
This commit is contained in:
		
							
								
								
									
										1
									
								
								.gitignore
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								.gitignore
									
									
									
									
										vendored
									
									
										Normal file
									
								
							@ -0,0 +1 @@
 | 
			
		||||
*.swp
 | 
			
		||||
							
								
								
									
										8
									
								
								Makefile
									
									
									
									
									
								
							
							
						
						
									
										8
									
								
								Makefile
									
									
									
									
									
								
							@ -1,4 +1,4 @@
 | 
			
		||||
all: cva-match
 | 
			
		||||
all: clean cva-match
 | 
			
		||||
 | 
			
		||||
debug: cva-match-debug
 | 
			
		||||
	gcc cva-match.o `pkg-config --libs opencv` -o cva-match
 | 
			
		||||
@ -7,10 +7,10 @@ cva-match: cva-match.o
 | 
			
		||||
	gcc cva-match.o `pkg-config --libs opencv` -o cva-match
 | 
			
		||||
 | 
			
		||||
cva-match-debug: cva-match-debug.o
 | 
			
		||||
	gcc cva-match.c -DDEBUG `pkg-config --cflags opencv` -c -o cva-match.o
 | 
			
		||||
	gcc src/cva-match.c -DDEBUG `pkg-config --cflags opencv` -c -o cva-match.o
 | 
			
		||||
 | 
			
		||||
cva-match.o:
 | 
			
		||||
	gcc cva-match.c `pkg-config --cflags opencv` -c -o cva-match.o
 | 
			
		||||
	gcc src/cva-match.c `pkg-config --cflags opencv` -c -o cva-match.o
 | 
			
		||||
 | 
			
		||||
clean:
 | 
			
		||||
	rm *.o cva-match
 | 
			
		||||
	rm *.o cva-match -f
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										93
									
								
								cva-match.c
									
									
									
									
									
								
							
							
						
						
									
										93
									
								
								cva-match.c
									
									
									
									
									
								
							@ -1,93 +0,0 @@
 | 
			
		||||
/*
 | 
			
		||||
 * TemplateMatching 1.0
 | 
			
		||||
 *
 | 
			
		||||
 * @author 	  Nashruddin Amin <me@nashruddin.com>
 | 
			
		||||
 * @version   1.0
 | 
			
		||||
 * @website	  http://www.nashruddin.com
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
#include <stdio.h>
 | 
			
		||||
#include "cv.h"
 | 
			
		||||
#include "highgui.h"
 | 
			
		||||
 | 
			
		||||
int main( int argc, char** argv )
 | 
			
		||||
{
 | 
			
		||||
	IplImage	*img;
 | 
			
		||||
	IplImage	*tpl;
 | 
			
		||||
	IplImage	*res;
 | 
			
		||||
	CvPoint		minloc, maxloc;
 | 
			
		||||
	double		minval, maxval;
 | 
			
		||||
	int			img_width, img_height;
 | 
			
		||||
	int			tpl_width, tpl_height;
 | 
			
		||||
	int			res_width, res_height;
 | 
			
		||||
 | 
			
		||||
	/* check for arguments */
 | 
			
		||||
	if( argc < 3 ) {
 | 
			
		||||
		fprintf( stderr, "Usage: template_match <reference> <template>\n" );
 | 
			
		||||
		return 1;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	/* load reference image */
 | 
			
		||||
	img = cvLoadImage( argv[1], CV_LOAD_IMAGE_COLOR );
 | 
			
		||||
 | 
			
		||||
	/* always check */
 | 
			
		||||
	if( img == 0 ) {
 | 
			
		||||
		fprintf( stderr, "Cannot load file %s!\n", argv[1] );
 | 
			
		||||
		return 1; 
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	/* load template image */
 | 
			
		||||
	tpl = cvLoadImage( argv[2], CV_LOAD_IMAGE_COLOR );
 | 
			
		||||
 | 
			
		||||
	/* always check */
 | 
			
		||||
	if( tpl == 0 ) {
 | 
			
		||||
		fprintf( stderr, "Cannot load file %s!\n", argv[2] );
 | 
			
		||||
		return 1;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	/* get image's properties */
 | 
			
		||||
	img_width  = img->width;
 | 
			
		||||
	img_height = img->height;
 | 
			
		||||
	tpl_width  = tpl->width;
 | 
			
		||||
	tpl_height = tpl->height;
 | 
			
		||||
	res_width  = img_width - tpl_width + 1;
 | 
			
		||||
	res_height = img_height - tpl_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( img, tpl, res, CV_TM_SQDIFF );
 | 
			
		||||
	/*cvMatchTemplate( img, tpl, res, CV_TM_SQDIFF_NORMED );
 | 
			
		||||
	cvMatchTemplate( img, tpl, res, CV_TM_CCORR );
 | 
			
		||||
	cvMatchTemplate( img, tpl, res, CV_TM_CCORR_NORMED );
 | 
			
		||||
	cvMatchTemplate( img, tpl, res, CV_TM_CCOEFF );
 | 
			
		||||
	cvMatchTemplate( img, tpl, res, CV_TM_CCOEFF_NORMED );*/
 | 
			
		||||
 | 
			
		||||
	cvMinMaxLoc( res, &minval, &maxval, &minloc, &maxloc, 0 );
 | 
			
		||||
 | 
			
		||||
	/* draw red rectangle */
 | 
			
		||||
	cvRectangle( img, 
 | 
			
		||||
				 cvPoint( minloc.x, minloc.y ), 
 | 
			
		||||
				 cvPoint( minloc.x + tpl_width, minloc.y + tpl_height ),
 | 
			
		||||
				 cvScalar( 0, 0, 255, 0 ), 1, 0, 0 );	
 | 
			
		||||
 | 
			
		||||
	/* display images */
 | 
			
		||||
	cvNamedWindow( "reference", CV_WINDOW_AUTOSIZE );
 | 
			
		||||
	cvNamedWindow( "template", CV_WINDOW_AUTOSIZE );
 | 
			
		||||
	cvShowImage( "reference", img );
 | 
			
		||||
	cvShowImage( "template", tpl );
 | 
			
		||||
 | 
			
		||||
	/* wait until user press a key to exit */
 | 
			
		||||
	cvWaitKey( 0 );
 | 
			
		||||
 | 
			
		||||
	/* free memory */
 | 
			
		||||
	cvDestroyWindow( "reference" );
 | 
			
		||||
	cvDestroyWindow( "template" );
 | 
			
		||||
	cvReleaseImage( &img );
 | 
			
		||||
	cvReleaseImage( &tpl );
 | 
			
		||||
	cvReleaseImage( &res );
 | 
			
		||||
 | 
			
		||||
	return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										180
									
								
								src/cva-match.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										180
									
								
								src/cva-match.c
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,180 @@
 | 
			
		||||
/*
 | 
			
		||||
 * =====================================================================================
 | 
			
		||||
 *
 | 
			
		||||
 *       Filename:  cva-match.c
 | 
			
		||||
 *
 | 
			
		||||
 *    Description:  Match an image to its parent image, and return the coordinates
 | 
			
		||||
 *
 | 
			
		||||
 *        Version:  0.1
 | 
			
		||||
 *        Created:  06/20/2012
 | 
			
		||||
 *       Revision:  none
 | 
			
		||||
 *       Compiler:  gcc
 | 
			
		||||
 *
 | 
			
		||||
 *         Author:  Bradlee Speice, bspeice@uncc.edu
 | 
			
		||||
 *   Organization:  MOSAIC at University of North Carolina at Charlotte
 | 
			
		||||
 *
 | 
			
		||||
 * =====================================================================================
 | 
			
		||||
 */
 | 
			
		||||
#include <stdlib.h>
 | 
			
		||||
#include <stdio.h>
 | 
			
		||||
#include <unistd.h>
 | 
			
		||||
#include <getopt.h>
 | 
			
		||||
 | 
			
		||||
#include "cv.h"
 | 
			
		||||
#include "highgui.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  ----- */
 | 
			
		||||
 | 
			
		||||
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;
 | 
			
		||||
 | 
			
		||||
	/* Set up everything for getopt */
 | 
			
		||||
	char *separator = ",";
 | 
			
		||||
	char *root_location = "root.png";
 | 
			
		||||
	char *sub_location = "sub.png";
 | 
			
		||||
 | 
			
		||||
	/* Start getopt */
 | 
			
		||||
	while (1)
 | 
			
		||||
	{
 | 
			
		||||
		static struct option long_options[] =
 | 
			
		||||
		{
 | 
			
		||||
				{"help",		no_argument,		0,	'h'},
 | 
			
		||||
				{"usage",		no_argument,		0,	'u'},
 | 
			
		||||
				{"root-image",	required_argument,	0,	'r'},
 | 
			
		||||
				{"sub-image",	required_argument,	0,	's'},
 | 
			
		||||
				{"separator",	required_argument,	0,	'p'},
 | 
			
		||||
				{0, 0, 0, 0}
 | 
			
		||||
		};
 | 
			
		||||
 | 
			
		||||
		int option_index = 0;
 | 
			
		||||
		opterr = 0;
 | 
			
		||||
 | 
			
		||||
		int c = getopt_long (argc, argv, "hur:s:p:",
 | 
			
		||||
							long_options, &option_index);
 | 
			
		||||
 | 
			
		||||
		/* We're done with parsing options */
 | 
			
		||||
		if (c == -1)
 | 
			
		||||
			break;
 | 
			
		||||
 | 
			
		||||
		switch (c)
 | 
			
		||||
		{
 | 
			
		||||
			case 0:
 | 
			
		||||
				break;
 | 
			
		||||
 | 
			
		||||
			case 'h':
 | 
			
		||||
				usage();
 | 
			
		||||
				break;
 | 
			
		||||
 | 
			
		||||
			case 'u':
 | 
			
		||||
				usage();
 | 
			
		||||
				break;
 | 
			
		||||
 | 
			
		||||
			case 'r':
 | 
			
		||||
				root_location = optarg;
 | 
			
		||||
				break;
 | 
			
		||||
 | 
			
		||||
			case 's':
 | 
			
		||||
				sub_location = optarg;
 | 
			
		||||
				break;
 | 
			
		||||
 | 
			
		||||
			case 'p':
 | 
			
		||||
				separator = optarg;
 | 
			
		||||
				break;
 | 
			
		||||
 | 
			
		||||
			case '?':
 | 
			
		||||
				/* Error routine */
 | 
			
		||||
				break;
 | 
			
		||||
 | 
			
		||||
			default:
 | 
			
		||||
				fprintf( stderr, "Unknown option..." );
 | 
			
		||||
				exit(0);
 | 
			
		||||
		};
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
	/* 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 );
 | 
			
		||||
 | 
			
		||||
	return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user