Recently, some OPENCV-based target detection algorithms have been researched, and today is the first day.
First download a simple online video of the movement of the two-value display of the code to learn, the following is my understanding, beginners will make a number of mistakes hope that everyone to correct.
#include <cv.h>
#include "cxcore.h"
#include
intMainintargc,unsignedChar*argv[]) {
cvcapture* capture = Cvcreatefilecapture ("Out.avi"); Iplimage* frame1=NULL; Iplimage* frame2=NULL; while(1) { //through the Cvqueryframe function to get the next frame of the video, after crawling, capture will point to the next frame, where the captured frame can not be released or modifiedframe1=Cvqueryframe (Capture); if(!frame1) { Break; }
Iplimage* Gray1 = Cvcreateimage (Cvgetsize (frame1), ipl_depth_8u,1);
//Frame1 color image into gray gray image Cvcvtcolor (Frame1,gray1,cv_bgr2gray); Frame2=Cvqueryframe (Capture); if(!frame2) { Break; } iplimage* Gray2 = Cvcreateimage (Cvgetsize (frame2), ipl_depth_8u,1); Cvcvtcolor (Frame2,gray2,cv_bgr2gray);
Iplimage* Gray_diff = Cvcreateimage (Cvgetsize (gray2), ipl_depth_8u,1);
Frame1 and Frame2 are two adjacent frames of the video respectively, then Gray1 and Gray2 are the grayscale images corresponding to the neighboring frames respectively.
//cvabsdiff function By comparing the difference between gray1 and Gray2, the different points of two graphs are found out and assigned to Gray_diff Cvabsdiff (Gray1,gray2,gray_diff);
for(inti =0; I < gray_diff->height; ++i) {
Traverses the entire Gray_diff image, whose ImageData member is the first address of the image, and Widthstep is the number of bytes in a row
//Then this is the first line that points to the image Uchar* Pucpixel = (uchar*) Gray_diff->imagedata + i*gray_diff->Widthstep; for(intj =0; J < gray_diff->width; ++j) {
//If the grayscale value of the point is <35, it is set to black, indicating the unchanged part, or 255, indicating movement if(pucpixel[j]> *) Pucpixel[j]=255; ElsePucpixel[j]=0; }} iplimage*gray_diff_erode = Cvcreateimage (Cvgetsize (Gray_diff),8,1); Iplimage*gray_diff_dilate = Cvcreateimage (Cvgetsize (Gray_diff),8,1);
An expansion treatment, with gray_diff_dilate storage, a corrosion treatment, with Gray_diff_erode storage
In fact, this is still not very understanding why these two to appear in pairs, I will be one of the comments can also show
Especially after the expansion, the image of the merging of similar background points will be more obvious.
///1 is the number of expansion or corrosion, the more the number is more obvious cvdilate (Gray_diff,gray_diff_dilate, NULL,1);//swellingCverode (Gray_diff_dilate,gray_diff_erode, NULL,1);//CorrosionCvnamedwindow ("avi"); Cvnamedwindow ("222"); /*Show Video Frames*/Cvshowimage ("avi", frame1); Cvshowimage ("222", Gray_diff_erode); Charc = Cvwaitkey ( -); /*Exit Loop*/ if(c = = -) { Break; } cvreleaseimage (&gray1); Cvreleaseimage (&gray2); Cvreleaseimage (&Gray_diff); Cvreleaseimage (&Gray_diff_erode); Cvreleaseimage (&gray_diff_dilate); } cvreleasecapture (&capture); /*Logout window, release image copy*/Cvdestroywindow ("avi"); Cvdestroywindow ("Act"); return 0;}
OpenCV Target Detection Learning notes (i)