1. Trang chủ
  2. » Công Nghệ Thông Tin

O’Reilly Learning OpenCV phần 5 pdf

57 414 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 57
Dung lượng 712,55 KB

Nội dung

214 | Chapter 7: Histograms and Matching Template Matching Template matching v ia cvMatchTemplate() is not based on histograms; rather, the func- tion matches an actual image patch against an input image by “sliding” the patch over the input image using one of the matching methods described in this section. If, as in Figure 7-10, we have an image patch containing a face, then we can slide that face over an input image looking for strong matches that would indicate another face is present.  e function call is similar to that of cvCalcBackProjectPatch(): void cvMatchTemplate( const CvArr* image, const CvArr* templ, CvArr* result, int method ); Instead of the array of input image planes that we saw in cvCalcBackProjectPatch(), here we have a single 8-bit or  oating-point plane or color image as input.  e match- ing model in templ is just a patch from a similar image containing the object for which Figure 7-9. Using cvCalcBackProjectPatch() to locate an object (here, a co ee cup) whose size ap- proximately matches the patch size (white box in upper right panel): the sought object is modeled by a hue-saturation histogram (upper le ), which can be compared with an HS histogram for the image as a whole (lower le ); the result of cvCalcBackProjectPatch() (lower right) is that the object is easily picked out from the scene by virtue of its color 07-R4886-AT1.indd 21407-R4886-AT1.indd 214 9/15/08 4:21:57 PM9/15/08 4:21:57 PM Some More Complicated Stuff | 215 you are searching.  e output object image will be put in the result image, which is a single-channel byte or  oating-point image of size ( images->width – patch_size.x + 1, rimages->height – patch_size.y + 1 ), as we saw previously in cvCalcBackProjectPatch().  e matching method is somewhat more complex, as we now explain. We use I to denote the input image, T the template, and R the result. Square difference matching method (method = CV_TM_SQDIFF)  ese methods match the squared di erence, so a perfect match will be 0 and bad matches will be large: R xy Txy Ix xy y x y sq_diff (,) [( , ) ( , )] , = ′′ −+ ′ + ′ ′′ 2 ∑∑ Correlation matching methods (method = CV_TM_CCORR)  ese methods multiplicatively match the template against the image, so a perfect match will be large and bad matches will be small or 0. R xy Txy Ix xy y xy ccorr (,) [( , ) ( , )] , = ′′ + ′ + ′ ⋅ ′′ ∑ 2 Figure 7-10. cvMatchTemplate() sweeps a template image patch across another image looking for matches 07-R4886-AT1.indd 21507-R4886-AT1.indd 215 9/15/08 4:21:57 PM9/15/08 4:21:57 PM 216 | Chapter 7: Histograms and Matching Correlation coefficient matching methods (method = CV_TM_CCOEFF)  ese methods match a template relative to its mean against the image relative to its mean, so a perfect match will be 1 and a perfect mismatch will be –1; a value of 0 simply means that there is no correlation (random alignments). R xy Txy Ix xy y x ccoeff (,) [ ( , ) ( , )] , = ′′′ ′ + ′ + ′ ⋅ ′′ 2 yy ∑ ′′′ = ′′ − ′′ ′′ ⋅ ∑ T xy Txy wh Tx y (,) (,) () (,) ′′ ′′ xy, 1 ′ + ′ + ′ =+ ′ + ′ − + ′′ + ⋅ Ix xy y Ix xy y wh Ix x y (,)(,) ()( , 1 ′′′ ∑ y ) ′′ ′′ xy, Normalized methods For each of the three methods just described, there are also normalized versions  rst developed by Galton [Galton] as described by Rodgers [Rodgers88].  e normalized methods are useful because, as mentioned previously, they can help reduce the e ects of lighting di erences between the template and the image. In each case, the normaliza- tion coe cient is the same: ∑∑ 22 )(,)⋅ +Zxy Txy Ix xy x xy xy (, ( , ) ,, = ′′ ′ + ′ ′′ ′′  e values for method that give the normalized computations are listed in Table 7-2. Table 7-2. Values of the method parameter for normalized template matching Value of method parameter Computed result CV_TM_SQDIFF_NORMED Rxy Rxy Zxy sq_di_normed sq_diff (,) (,) (,) = CV_TM_CCORR_NORMED Rxy Rxy Zxy ccor_normed ccor (,) (,) (,) = CV_TM_CCOEFF_NORMED Rxy Rxy Zxy ccoe_normed ccoeff (,) (,) (,) = As usual, we obtain more accurate matches (at the cost of more computations) as we move from simpler measures (square di erence) to the more sophisticated ones (corre- lation coe cient). It’s best to do some test trials of all these settings and then choose the one that best trades o accuracy for speed in your application. 07-R4886-AT1.indd 21607-R4886-AT1.indd 216 9/15/08 4:21:58 PM9/15/08 4:21:58 PM Some More Complicated Stuff | 217 Again, be careful when interpreting your results.  e square-di erence methods show best matches with a minimum, whereas the correlation and correlation-coe cient methods show best matches at maximum points. As in the case of cvCalcBackProjectPatch(), once we use cvMatchTemplate() to obtain a matching result image we can then use cvMinMaxLoc() to  nd the location of the best match. Again, we want to ensure there’s an area of good match around that point in order to avoid random template alignments that just happen to work well. A good match should have good matches nearby, because slight misalignments of the template shouldn’t vary the results too much for real matches. Looking for the best matching “hill” can be done by slightly smoothing the result image before seeking the maximum (for correlation or correlation-coe cient) or minimum (for square-di erence) match- ing methods.  e morphological operators can also be helpful in this context. Example 7-5 should give you a good idea of how the di erent template matching tech- niques behave.  is program  rst reads in a template and image to be matched and then performs the matching via the methods we’ve discussed here. Example 7-5. Template matching // Template matching. // Usage: matchTemplate image template // #include <cv.h> #include <cxcore.h> #include <highgui.h> #include <stdio.h> int main( int argc, char** argv ) { IplImage *src, *templ,*ftmp[6]; //ftmp will hold results int i; if( argc == 3){ //Read in the source image to be searched: if((src=cvLoadImage(argv[1], 1))== 0) { printf(“Error on reading src image %s\n”,argv[i]); return(-1); } //Read in the template to be used for matching: if((templ=cvLoadImage(argv[2], 1))== 0) { printf(“Error on reading template %s\n”,argv[2]); return(-1); } //ALLOCATE OUTPUT IMAGES: int iwidth = src->width - templ->width + 1; int iheight = src->height - templ->height + 1; for(i=0; i<6; ++i){ ftmp[i] = cvCreateImage( cvSize(iwidth,iheight),32,1); } //DO THE MATCHING OF THE TEMPLATE WITH THE IMAGE: 07-R4886-AT1.indd 21707-R4886-AT1.indd 217 9/15/08 4:21:59 PM9/15/08 4:21:59 PM 218 | Chapter 7: Histograms and Matching Example 7-5. Template matching (continued) for(i=0; i<6; ++i){ cvMatchTemplate( src, templ, ftmp[i], i); cvNormalize(ftmp[i],ftmp[i],1,0,CV_MINMAX)*; } //DISPLAY cvNamedWindow( “Template”, 0 ); cvShowImage( “Template”, templ ); cvNamedWindow( “Image”, 0 ); cvShowImage( “Image”, src ); cvNamedWindow( “SQDIFF”, 0 ); cvShowImage( “SQDIFF”, ftmp[0] ); cvNamedWindow( “SQDIFF_NORMED”, 0 ); cvShowImage( “SQDIFF_NORMED”, ftmp[1] ); cvNamedWindow( “CCORR”, 0 ); cvShowImage( “CCORR”, ftmp[2] ); cvNamedWindow( “CCORR_NORMED”, 0 ); cvShowImage( “CCORR_NORMED”, ftmp[3] ); cvNamedWindow( “CCOEFF”, 0 ); cvShowImage( “CCOEFF”, ftmp[4] ); cvNamedWindow( “CCOEFF_NORMED”, 0 ); cvShowImage( “CCOEFF_NORMED”, ftmp[5] ); //LET USER VIEW RESULTS: cvWaitKey(0); } else { printf(“Call should be: ” “matchTemplate image template \n”);} } Note the use of cvNormalize() in this code, which allows us to display the results in a consistent way (recall that some of the matching methods can return negative-valued results. We use the CV_MINMAX  ag when normalizing; this tells the function to shi and scale the  oating-point images so that all returned values are between 0 and 1. Fig ure 7-11 shows the results of sweeping the face template over the source image (shown in Figure 7-10) using each of cvMatchTemplate()’s available matching methods. In outdoor imagery especially, it’s almost always better to use one of the normalized methods. Among those, correlation coe cient gives the most clearly delineated match—but, as expected, at a greater computational cost. For a speci c application, such as automatic parts inspection or tracking features in a video, you should try all the methods and  nd the speed and accuracy trade-o that best serves your needs. * You can o en get more pronounced match results by raising the matches to a power (e.g., cvPow(ftmp[i], ftmp[i], 5); ). In the case of a result which is normalized between 0.0 and 1.0, then you can immediately see that a good match of 0.99 taken to the   h power is not much reduced (0.99 5 =0.95) while a poorer score of 0.20 is reduced substantially (0.50 5 =0.03). 07-R4886-AT1.indd 21807-R4886-AT1.indd 218 9/15/08 4:21:59 PM9/15/08 4:21:59 PM Exercises | 219 Exercises Generate 1,000 random numbers 1. r i between 0 and 1. Decide on a bin size and then take a histogram of 1/r i . Are there similar numbers of entries (i.e., within a factor of ±10) in each histo-a. gram bin? Propose a way of dealing with distributions that are highly nonlinear so that b. each bin has, within a factor of 10, the same amount of data. Take three images of a hand in each of the three lighting conditions discussed in 2. the text. Use cvCalcHist() to make an RGB histogram of the  esh color of one of the hands photographed indoors. Try using just a few large bins (e.g., 2 per dimension), a medium number of bins a. (16 per dimension) and many bins (256 per dimension).  en run a matching routine (using all histogram matching methods) against the other indoor light- ing images of hands. Describe what you  nd. Now add 8 and then 32 bins per dimension and try matching across lighting b. conditions (train on indoor, test on outdoor). Describe the results. As in exercise 2, gather RGB histograms of hand  esh color. Take one of the in-3. door histogram samples as your model and measure EMD (earth mover’s distance) against the second indoor histogram and against the  rst outdoor shaded and  rst outdoor sunlit histograms. Use these measurements to set a distance threshold. Figure 7-11. Match results of six matching methods for the template search depicted in Figure 7-10: the best match for square di erence is 0 and for the other methods it’s the maximum point; thus, matches are indicated by dark areas in the le column and by bright spots in the other two columns 07-R4886-AT1.indd 21907-R4886-AT1.indd 219 9/15/08 4:21:59 PM9/15/08 4:21:59 PM 220 | Chapter 7: Histograms and Matching Using this EMD threshold, see how well you detect the  esh histogram of the a. third indoor histogram, the second outdoor shaded, and the second outdoor sunlit histograms. Report your results. Take histograms of randomly chosen non esh background patches to see how b. well your EMD discriminates. Can it reject the background while matching the true  esh histograms? Using your collection of hand images, design a histogram that can determine un-4. der which of the three lighting conditions a given image was captured. Toward this end, you should create features—perhaps sampling from parts of the whole scene, sampling brightness values, and/or sampling relative brightness (e.g., from top to bottom patches in the frame) or gradients from center to edges. Assemble three histograms of  esh models from each of our three lighting 5. conditions. Use the  rst histograms from indoor, outdoor shaded, and outdoor sunlit as a. your models. Test each one of these against the second images in each respec- tive class to see how well the  esh-matching score works. Report matches. Use the “scene detector” you devised in part a, to create a “switching histo-b. gram” model. First use the scene detector to determine which histogram model to use: indoor, outdoor shaded, or outdoor sunlit.  en use the corresponding  esh model to accept or reject the second  esh patch under all three condi- tions. How well does this switching model work? Create a  esh-region interest (or “attention”) detector.6. Just indoors for now, use several samples of hand and face  esh to create an a. RGB histogram. Use b. cvCalcBackProject() to  nd areas of  esh. Use c. cvErode() from Chapter 5 to clean up noise and then cvFloodFill() (from the same chapter) to  nd large areas of  esh in an image.  ese are your “atten- tion” regions. Try some hand-gesture recognition. Photograph a hand about 2 feet from the cam-7. era, create some (nonmoving) hand gestures: thumb up, thumb le , thumb right. Using your attention detector from exercise 6, take image gradients in the area a. of detected  esh around the hand and create a histogram model for each of the three gestures. Also create a histogram of the face (if there’s a face in the image) so that you’ll have a (nongesture) model of that large  esh region. You might also take histograms of some similar but nongesture hand positions, just so they won’t be confused with the actual gestures. Test for recognition using a webcam: use the  esh interest regions to  nd “po-b. tential hands”; take gradients in each  esh region; use histogram matching 07-R4886-AT1.indd 22007-R4886-AT1.indd 220 9/15/08 4:21:59 PM9/15/08 4:21:59 PM Exercises | 221 above a threshold to detect the gesture. If two models are above threshold, take the better match as the winner. Move your hand 1–2 feet further back and see if the gradient histogram can c. still recognize the gestures. Report. Repeat exercise 7 but with EMD for the matching. What happens to EMD as you 8. move your hand back? With the same images as before but with captured image patches instead of his-9. tograms of the  esh around the hand, use cvMatchTemplate() instead of histogram matching. What happens to template matching when you move your hand back- wards so that its size is smaller in the image? 07-R4886-AT1.indd 22107-R4886-AT1.indd 221 9/15/08 4:22:00 PM9/15/08 4:22:00 PM 222 CHAPTER 8 Contours Although algorithms like the Canny edge detector can be used to  nd the edge pixels that separate di erent segments in an image, they do not tell you anything about those edges as entities in themselves.  e next step is to be able to assemble those edge pix- els into contours. By now you have probably come to expect that there is a convenient function in OpenCV that will do exactly this for you, and indeed there is: cvFindCon- tours() . We will start out this chapter with some basics that we will need in order to use this function. Speci cally, we will introduce memory storages, which are how OpenCV functions gain access to memory when they need to construct new objects dynamically; then we will learn some basics about sequences, which are the objects used to represent contours generally. With those concepts in hand, we will get into contour  nding in some detail.  erea er we will move on to the many things we can do with contours a er they’ve been computed. Memory Storage OpenCV uses an entity called a memory storage as its method of handling memory al- location for dynamic objects. Memory storages are linked lists of memory blocks that allow for fast allocation and de-allocation of continuous sets of blocks. OpenCV func- tions that require the ability to allocate memory as part of their normal functionality will require access to a memory storage from which to get the memory they require (typically this includes any function whose output is of variable size). Memory storages are handled with the following four routines: CvMemStorage* cvCreateMemStorage( int block_size = 0 ); void cvReleaseMemStorage( CvMemStorage** storage ); void cvClearMemStorage( CvMemStorage* storage ); void* cvMemStorageAlloc( CvMemStorage* storage, 08-R4886-AT1.indd 22208-R4886-AT1.indd 222 9/15/08 4:22:21 PM9/15/08 4:22:21 PM Sequences | 223 size_t size ); To create a memory storage, the function cvCreateMemStorage() is used.  is function takes as an argument a block size, which gives the size of memory blocks inside the store. If this argument is set to 0 then the default block size (64kB) will be used.  e function returns a pointer to a new memory store.  e cvReleaseMemStorage() function takes a pointer to a valid memory storage and then de-allocates the storage.  is is essentially equivalent to the OpenCV de-allocations of images, matrices, and other structures. You can empty a memory storage by calling cvClearMemStorage(), which also takes a pointer to a valid storage. You must be aware of an important feature of this function: it is the only way to release (and therea er reuse) memory allocated to a memory stor- age.  is might not seem like much, but there will be other routines that delete objects inside of memory storages (we will introduce one of these momentarily) but do not re- turn the memory they were using. In short, only cvClearMemStorage() (and, of course, cvReleaseMemStorage()) recycle the storage memory.* Deletion of any dynamic structure ( CvSeq, CvSet, etc.) never returns any memory back to storage (although the structures are able to reuse some memory once taken from the storage for their own data). You can also allocate your own continuous blocks from a memory store—in a man- ner analogous to the way malloc() allocates memory from the heap—with the func- tion cvMemStorageAlloc(). In this case you simply provide a pointer to the storage and the number of bytes you need.  e return is a pointer of type void* (again, similar to malloc()). Sequences One kind of object that can be stored inside a memory storage is a sequence. Sequences are themselves linked lists of other structures. OpenCV can make sequences out of many di erent kinds of objects. In this sense you can think of the sequence as some- thing similar to the generic container classes (or container class templates) that exist in various other programming languages.  e sequence construct in OpenCV is actually a deque, so it is very fast for random access and for additions and deletions from either end but a little slow for adding and deleting objects in the middle.  e sequence structure itself (see Example 8-1) has some important elements that you should be aware of.  e  rst, and one you will use o en, is total.  is is the total num- ber of points or objects in the sequence.  e next four important elements are point- ers to other sequences: h_prev, h_next, v_prev, and v_next.  ese four pointers are part of what are called CV_TREE_NODE_FIELDS; they are used not to indicate elements inside of the sequence but rather to connect di erent sequences to one another. Other objects in the OpenCV universe also contain these tree node  elds. Any such objects can be * Actually, one other function, called cvRestoreMemStoragePos(), can restore memory to the storage. But this function is primarily for the library’s internal use and is beyond the scope of this book. 08-R4886-AT1.indd 22308-R4886-AT1.indd 223 9/15/08 4:22:22 PM9/15/08 4:22:22 PM [...]... g_storage ); } CvSeq* contours = 0; cvCvtColor( g_image, g_gray, CV_BGR2GRAY ); cvThreshold( g_gray, g_gray, g_thresh, 255 , CV_THRESH_BINARY ); cvFindContours( g_gray, g_storage, &contours ); cvZero( g_gray ); if( contours ) cvDrawContours( g_gray, contours, cvScalarAll( 255 ), cvScalarAll( 255 ), 100 ); cvShowImage( “Contours”, g_gray ); } int main( int argc, char** argv ) { if( argc != 2 || !(g_image = cvLoadImage(argv[1]))... based on Euclidean distance The comparison function is set to return true (1) if the distance is less than or equal to 5 and to return false (0) otherwise The resulting clusters are labeled with their integer ordinal from labels Using a Sequence As a Stack As stated earlier, a sequence in OpenCV is really a linked list This means, among other things, that it can be accessed efficiently from either end... macro, so the added element will be essentially invisible until you are done writing It will become visible when the structure is completely updated by cvEndWriteSeq() * Effective with the beta 5 version of OpenCV, this size is automatically increased if the sequence becomes big; hence you’ll not need to worry about it under normal circumstances Sequences | 231 If necessary, the structure can be brought... at hand There are many ways to represent a curve Contours are represented in OpenCV by sequences in which every entry in the sequence encodes information about the location of the next point on the curve We will dig into the details of such sequences in a moment, but for now just keep in mind that a contour is represented in OpenCV by a CvSeq sequence that is, one way or another, a sequence of points... stored free blocks list pointer to the first sequence block } Creating a Sequence As we have alluded to already, sequences can be returned from various OpenCV functions In addition to this, you can, of course, create sequences yourself Like many objects in OpenCV, there is an allocator function that will create a sequence for you and return a pointer to the resulting data structure This function is called... cvCreateTrackbar( “Threshold”, “Contours”, &g_thresh, 242 | Chapter 8: Contours Example 8-2 Finding contours based on a trackbar’s location; the contours are updated whenever the trackbar is moved (continued) 255 , on_trackbar ); on_trackbar(0); cvWaitKey(); return 0; } Here, everything of interest to us is happening inside of the function on_trackbar() If the global variable g_storage is still at its (NULL)... argv[1], CV_LOAD_IMAGE_GRAYSCALE ); IplImage* img_edge = cvCreateImage( cvGetSize(img_8uc1), 8, 1 ); IplImage* img_8uc3 = cvCreateImage( cvGetSize(img_8uc1), 8, 3 ); cvThreshold( img_8uc1, img_edge, 128, 255 , CV_THRESH_BINARY ); CvMemStorage* storage = cvCreateMemStorage(); CvSeq* first_contour = NULL; Another Contour Example | 243 Example 8-3 Finding and drawing contours on an input image (continued) int... the various functions built into OpenCV that will either do these things for us or at least make it easier for us to perform our own tasks Polygon Approximations If we are drawing a contour or are engaged in shape analysis, it is common to approximate a contour representing a polygon with another contour having fewer vertices There are many different ways to do this; OpenCV offers an implementation... explanation of how the algorithm works In Figure 8 -5, starting with a contour (panel b), the algorithm begins by picking two extremal points and connecting them with a line (panel c) Then the original polygon is searched to find the point farthest from the line just drawn, and that point is added to the approximation * For aficionados, the method used by OpenCV is the Douglas-Peucker (DP) approximation... | 2 45 The process is iterated (panel d), adding the next most distant point to the accumulated approximation, until all of the points are less than the distance indicated by the precision parameter (panel f) This means that good candidates for the parameter are some fraction of the contour’s length, or of the length of its bounding box, or a similar measure of the contour’s overall size Figure 8 -5 Visualization . patch across another image looking for matches 07-R4886-AT1.indd 2 150 7-R4886-AT1.indd 2 15 9/ 15/ 08 4:21 :57 PM9/ 15/ 08 4:21 :57 PM 216 | Chapter 7: Histograms and Matching Correlation coefficient. a poorer score of 0.20 is reduced substantially (0 .50 5 =0.03). 07-R4886-AT1.indd 21807-R4886-AT1.indd 218 9/ 15/ 08 4:21 :59 PM9/ 15/ 08 4:21 :59 PM Exercises | 219 Exercises Generate 1,000 random. IMAGE: 07-R4886-AT1.indd 21707-R4886-AT1.indd 217 9/ 15/ 08 4:21 :59 PM9/ 15/ 08 4:21 :59 PM 218 | Chapter 7: Histograms and Matching Example 7 -5. Template matching (continued) for(i=0; i<6; ++i){

Ngày đăng: 12/08/2014, 21:20

TỪ KHÓA LIÊN QUAN