opencv边界识别,输入是一个传送带上的物体,通过识别边界判断是哪个物体。方法还不是特别完善,判断物体的时候其实应该用周长和面积的比,但是已经没有问题了
#include<iostream>
#include<opencv2\core\core.hpp>#include<opencv2\highgui\highgui.hpp>
#include<opencv2\imgproc\imgproc.hpp>
#include<opencv2\video\background_segm.hpp>
#include "opencv2/imgproc.hpp"
#include "opencv2/videoio.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/video/background_segm.hpp"
#include <stdio.h>
#include <string>
//(maskArct<500&&maskArct>400&&areat<4000&&areat>3500)||
using namespace cv;
using namespace std;
Mat MotionSeg(VideoCapture & v,VideoCapture & v2){
Mat Frame, fgMask, bg, bfgMask, sfgMask;
int FrameNum = 0;
if (!v.isOpened()) {
printf("Reading video failed!\n");
//return false;
}
Ptr<BackgroundSubtractorMOG2> bgSub = createBackgroundSubtractorMOG2(); //Define background minus object and initialization
while (v.read(Frame)) {
double maskArct=0;
double areat=0;
FrameNum++;
bgSub->apply(Frame, fgMask, 0.005);
bgSub->getBackgroundImage(bg); //get background image
blur(fgMask, sfgMask, Size(3, 3)); //smooth the foreground video
threshold(sfgMask, bfgMask, 127.5, 255, cv::THRESH_BINARY); //binary the foreground video