opencv幀差法找出相差大的圖像
更新時(shí)間:2020年03月21日 09:14:31 作者:amulet0703
這篇文章主要為大家詳細(xì)介紹了opencv幀差法找出相差大的圖像,包含訪問mat的像素值,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
本文實(shí)例為大家分享了opencv幀差法找出相差大的圖像,供大家參考,具體內(nèi)容如下
#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:\\數(shù)據(jù)集\\目標(biāo)區(qū)域圖像\\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) //達(dá)到閾值的像素?cái)?shù)達(dá)到一定的數(shù)量則保存該圖像
{
sprintf(OutName,"E:\\Image篩選之后\\%5d.jpg",num);
imwrite(OutName,Image);
}
cvtColor(tempImage, previousImage, CV_BGR2GRAY);
}
}
}
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C語言數(shù)組按協(xié)議存儲(chǔ)與按協(xié)議解析數(shù)據(jù)的實(shí)現(xiàn)
今天小編就為大家分享一篇關(guān)于C語言數(shù)組按協(xié)議存儲(chǔ)與按協(xié)議解析數(shù)據(jù)的實(shí)現(xiàn),小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2018-12-12

