这篇文章将为大家详细讲解有关如何使用opencv帧差法找出相差大的图像,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。
本文实例为大家分享了opencv帧差法找出相差大的图像,供大家参考,具体内容如下
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <fstream>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/objdetect/objdetect.hpp>
#include <opencv2/ml/ml.hpp>
#include <string.h>
#define IMAGENO 18456
using namespace std;
using namespace cv;
int main(int argc,char * argv())
{
string ImgName;
char OutName[100];
Mat Image,tempImage,previousImage,currentImage,resultImage;
ifstream fin("ImageList.txt");
//ifstream fin("new.txt");
for(int num=0; num<IMAGENO && getline(fin,ImgName); num++)
{
cout <<"读入"<<ImgName<<endl;
ImgName = "E:\\Image\\" + ImgName ;
Image = imread(ImgName);
resize(Image,tempImage,Size(130,130));
if (num == 0)
{
cvtColor(tempImage, previousImage, CV_BGR2GRAY);
sprintf(OutName,"E:\\数据集\\目标区域图像\\StudentsArea抠图筛选\\%5d.jpg",num);
imwrite(OutName,Image);
}
if (num > 0)
{
cvtColor(tempImage, currentImage, CV_BGR2GRAY);
absdiff(currentImage,previousImage,resultImage); //帧差法,相减
threshold(resultImage, resultImage, 20, 255.0, CV_THRESH_BINARY); //二值化,像素值相差大于20则置为255,其余为0
int counter = 0;
// 访问mat中的像素
for (int i=0; i<resultImage.rows; i++)
{
uchar *data = resultImage.ptr<uchar>(i); //获取每一行的指针
for (int j=0;j<resultImage.cols; j++)
{
if (data[j] == 255) //访问到像素值
{
counter++;
}
}
}
if (counter > 4000) //达到阈值的像素数达到一定的数量则保存该图像
{
sprintf(OutName,"E:\\Image筛选之后\\%5d.jpg",num);
imwrite(OutName,Image);
}
cvtColor(tempImage, previousImage, CV_BGR2GRAY);
}
}
}
关于“如何使用opencv帧差法找出相差大的图像”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。