opencv幀差法找出相差大的圖像
更新時間:2020年03月21日 09:14:31 作者:amulet0703
這篇文章主要為大家詳細(xì)介紹了opencv幀差法找出相差大的圖像,包含訪問mat的像素值,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實(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á)到閾值的像素數(shù)達(dá)到一定的數(shù)量則保存該圖像
{
sprintf(OutName,"E:\\Image篩選之后\\%5d.jpg",num);
imwrite(OutName,Image);
}
cvtColor(tempImage, previousImage, CV_BGR2GRAY);
}
}
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
c語言中的局部跳轉(zhuǎn)及全局跳轉(zhuǎn)功能
本文介紹了C語言中的goto語句,以及如何使用setjmp和longjmp實(shí)現(xiàn)跨函數(shù)的跳轉(zhuǎn),詳細(xì)講解了setjmp和longjmp的使用方法和注意事項(xiàng),以及使用這種全局跳轉(zhuǎn)后變量狀態(tài)的不確定性,感興趣的朋友一起看看吧2024-09-09

