opencv幀差法找出相差大的圖像
更新時(shí)間:2020年03月21日 09:14:31 作者:amulet0703
這篇文章主要為大家詳細(xì)介紹了opencv幀差法找出相差大的圖像,包含訪問(wèn)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;
// 訪問(wèn)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) //訪問(wèn)到像素值
{
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);
}
}
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
利用c++和easyx圖形庫(kù)做一個(gè)低配版掃雷游戲
這篇文章主要介紹了用c++和easyx圖形庫(kù)做一個(gè)低配版掃雷游戲,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-01-01
c語(yǔ)言中的局部跳轉(zhuǎn)及全局跳轉(zhuǎn)功能
本文介紹了C語(yǔ)言中的goto語(yǔ)句,以及如何使用setjmp和longjmp實(shí)現(xiàn)跨函數(shù)的跳轉(zhuǎn),詳細(xì)講解了setjmp和longjmp的使用方法和注意事項(xiàng),以及使用這種全局跳轉(zhuǎn)后變量狀態(tài)的不確定性,感興趣的朋友一起看看吧2024-09-09
C++ 漢諾塔問(wèn)題知識(shí)點(diǎn)總結(jié)
在本篇文章里小編給大家整理的是關(guān)于C++ 漢諾塔問(wèn)題知識(shí)點(diǎn)內(nèi)容,有需要的朋友們可以參考下。2020-02-02
C語(yǔ)言中的數(shù)據(jù)整除判斷問(wèn)題
這篇文章主要介紹了C語(yǔ)言中的數(shù)據(jù)整除判斷問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-11-11

