Recently in the study OpenCV, did a camera before and after the frame comparison effect, found by comparison, can find the object contour.
The specific algorithm is shown below:
Compare two pictures bool Copencvcameractrl::D ifftwopic () {Mat Pre_gray;cv::namedwindow ("Showcamera"); Cv::namedwindow (_T ("JG") ); while (true) {//Get image mat Frame, BIGFRAME;VC >> bigframe;if (!bigframe.data) {continue;} frame = Bigframe (Cv::rect (+/-), frame = Bigframe;cv::imshow ("Showcamera", frame); Mat Frame_gray;cv::cvtcolor (frame, frame_gray, Cv_bgr2gray); if (!pre_gray.empty ()) {//Compare image Mat Abcdiff, EROSION_DST;CV :: Absdiff (Pre_gray, Frame_gray, Abcdiff);//corrosion operation/*int erosion_size = 1;int Erosion_type = morph_rect; Mat element = Getstructuringelement (erosion_type,size (2*erosion_size + 1, 2*erosion_size+1), point (Erosion_size, Erosi on_size))////Corrosion operation Erode (Abcdiff, erosion_dst, Element); *///Two value Operation Mat Matthre;threshold (Abcdiff, Matthre, 5, 255, 0); c V::imshow (_t ("JG"), Matthre), int ncount = Cv::countnonzero (Abcdiff), if (Ncount >) {//beep (5500,150);}} Save Image Frame_gray.copyto (Pre_gray); Cv::waitkey (30);} return TRUE;}
Process the effect as shown in:
The effect is the effect of non-corrosive treatment, if the addition of corrosion treatment, the entire picture will be black. Note that the difference points in the contour you see are discrete points that cannot be referenced directly.
Interesting phenomenon of camera image comparison