About Object Detection
Environment: OpenCV 2.4.11+vs2013
Reference:
Http://www.cnblogs.com/tornadomeet/archive/2012/06/02/2531705.html
Http://www.cnblogs.com/xinyuyuanm/archive/2013/04/29/3050750.html
1#include <string>2#include <iostream>3#include <stdio.h>4#include <fstream>5#include <math.h>6 7#include <opencv2\opencv.hpp>8#include <opencv2\highgui\highgui.hpp>9#include <opencv2\imgproc\imgproc.hpp>Ten#include <opencv2\core\core.hpp> One A using namespacestd; - using namespaceCV; - the #defineThreshold_diff 20 - intMain () - { -Mat Src1 = Imread ("Picture 1.png"), gray1; + Cvtcolor (Src1, Gray1, Color_bgr2gray); -Mat Src2 = Imread ("picture 2.png"), Gray2; + Cvtcolor (Src2, Gray2, Color_bgr2gray); A //Noise Reduction atBlur (Gray1, Gray1, Size (3,3)); -Blur (Gray2, Gray2, Size (3,3)); - - //two value of - Mat Gray_diff; - //Cvmat *src1, *src2; in //Cvabsdiff (gray_src_y, Gray_src_no, abs_src); - Subtract (gray2, gray1, Gray_diff); to for(inti =0; i < gray_diff.rows; i++) + { - for(intj =0; J < Gray_diff.cols; J + +) the { * if(ABS (gray_diff.at<unsignedChar> (i,j)) >=Threshold_diff) $gray_diff.at<unsignedChar> (i, j) =255;Panax Notoginseng Else -gray_diff.at<unsignedChar> (i, j) =0; the } + } A the //Open operation (expansion corrosion operator) +Mat out; -Mat element = Getstructuringelement (Morph_rect, Size (3,3)); $Erode (Gray_diff, out, Element); $Dilate ( out, out, Element); - - //Find Outlines theMat DST = Mat::zeros ( out. Size (), cv_8uc3); -Vector<vector<point>>contours;WuyiVector<vec4i>hierarchy; theFindcontours ( out, contours, hierarchy, Cv_retr_ccomp, cv_chain_approx_simple); - intindex =0, Largestcomp; Wu DoubleMaxarea =0; - for(; index >=0; index = hierarchy[index][0]) About { $ Constvector<point>& C =Contours[index]; - DoubleArea =fabs (Contourarea (Mat (c))); - if(Area >Maxarea) - { AMaxarea =Area ; +Largestcomp =index; the - } $ } theScalar Color (0,255,0); theDrawcontours (DST, contours, largestcomp, color, cv_filled,8, hierarchy); the theImshow ("Contour Map", DST); - inImshow ("Original", SRC1); theImshow ("put items", SRC2); theImshow ("Picture Difference", Gray_diff); AboutImshow ("Open Operation", out); the theWaitkey (0); theSystem"Pause"); + return 0; -}
OpenCV, about object detection