updated ppg.cpp and debuged, both with GIBERT's help and close assistance

This commit is contained in:
Nicolas TRAGLIA 2023-03-01 12:32:57 +01:00
parent 58d4612738
commit dfc3b9b40a
1 changed files with 167 additions and 158 deletions

69
ppg.cpp
View File

@ -6,14 +6,14 @@
#include "opencv2/videoio.hpp" #include "opencv2/videoio.hpp"
#include "opencv2/highgui.hpp" #include "opencv2/highgui.hpp"
int FPS=30; //FPS variable. FPS is the framerate of your video, aka your recording device's int FPS=10; //FPS variable. FPS is the framerate of your video, aka your recording device's
int DISCARD_DURATION=5; int DISCARD_DURATION=5;
bool isDiscardData=true; bool isDiscardData=true;
int countDiscard=0; int countDiscard=0;
bool isBufferFull = false; //buffer variables to initialise before main(); bool isBufferFull = false; //buffer variables to initialise before main();
int sampleIdBuffer = 0; int sampleIdBuffer = 0;
int BUFFER_DURATION= 5; int BUFFER_DURATION= 15;
//Display normalised signal //Display normalised signal
template <typename T> template <typename T>
@ -56,16 +56,6 @@ int main(){
}; };
while (true) while (true)
{
if(isDiscardData) //This function delays the beginning of the analysis of the data to avoid processing frames taken during white balancing.
{
countDiscard++;
if (countDiscard == DISCARD_DURATION*FPS)
{
isDiscardData=false;
}
}
else
{ {
//Create a matrix to store the image from the cam //Create a matrix to store the image from the cam
cv::Mat frame; cv::Mat frame;
@ -78,36 +68,51 @@ int main(){
std::cerr<<"[ERROR] Blank frame grabbed"<<std::endl; std::cerr<<"[ERROR] Blank frame grabbed"<<std::endl;
break; break;
} }
cv::imshow("Color", frame); //shows the colored frame
if (cv::waitKey(1000.0/FPS)>=0) //Stops after 1000/FPS frames
{
break;
}
if(isDiscardData) //This function delays the beginning of the analysis of the data to avoid processing frames taken during white balancing.
{
countDiscard++;
if (countDiscard == DISCARD_DURATION*FPS)
{
isDiscardData=false;
}
}
else
{
cv::Mat frame_gray; cv::Mat frame_gray;
cv::cvtColor(frame, frame_gray, cv::COLOR_BGR2GRAY); cv::cvtColor(frame, frame_gray, cv::COLOR_BGR2GRAY);
cv::imshow("Gray", frame_gray); //Shows frame in greyscale //cv::imshow("Gray", frame_gray); //Shows frame in greyscale
std::vector<cv::Rect> faceRectangles; std::vector<cv::Rect> faceRectangles;
faceDetector.detectMultiScale(frame_gray, faceRectangles, 1.1, 3, 0, cv::Size(20,20)); //Detects face faceDetector.detectMultiScale(frame_gray, faceRectangles, 1.1, 3, 0, cv::Size(20,20)); //Detects face
if (faceRectangles.size() > 0)
cv::rectangle(frame, faceRectangles[0], cv::Scalar(0,0,255),1,1,0);
cv::Rect foreheadROI; //create a forehead ROI equal to the face ROI slightly moved upward. cv::Rect foreheadROI; //create a forehead ROI equal to the face ROI slightly moved upward.
if (faceRectangles.size() > 0)
{
foreheadROI = faceRectangles[0]; foreheadROI = faceRectangles[0];
foreheadROI.height *= 0.3; foreheadROI.height *= 0.3;
cv::Mat frame_forehead = frame(foreheadROI); cv::Mat frame_forehead = frame(foreheadROI);
cv::Scalar avg_forehead = mean(frame_forehead); //calculates mean of object frame_forehead cv::Scalar avg_forehead = mean(frame_forehead); //calculates mean of object frame_forehead
//Buffer of average value for the green channel over the forehead ROI //Buffer of average value for the green channel over the forehead ROI
cv::Mat greenSignal(1, FPS*BUFFER_DURATION, CV_64F); cv::Mat greenSignal(1, FPS*BUFFER_DURATION, CV_64F);
if (!isBufferFull) if (!isBufferFull)
{ {
std::cout << "sampleIdBuffer= " << sampleIdBuffer << " / " << FPS*BUFFER_DURATION << std::endl;
greenSignal.at<double>(0, sampleIdBuffer) = avg_forehead[1] ; greenSignal.at<double>(0, sampleIdBuffer) = avg_forehead[1] ;
sampleIdBuffer++; sampleIdBuffer++;
if (sampleIdBuffer == FPS*BUFFER_DURATION) if (sampleIdBuffer == FPS*BUFFER_DURATION)
{ {
isBufferFull = true; isBufferFull = true;
std::cout<<"greenSignal= "<<greenSignal<<std::endl;
} }
} }
else
{
//Normalisation of our signal //Normalisation of our signal
std::vector<double> greenSignalNormalized; std::vector<double> greenSignalNormalized;
@ -121,7 +126,7 @@ int main(){
int range[2] = {0, (int)(FPS*BUFFER_DURATION)}; int range[2] = {0, (int)(FPS*BUFFER_DURATION)};
cv::imshow("green", plotGraph(greenSignalNormalized, range)); cv::imshow("green", plotGraph(greenSignalNormalized, range));
cv::Mat greenFFT; cv::Mat greenFFT; //Fast Fourier Transform
std::vector<double> greenFFTModule; std::vector<double> greenFFTModule;
cv::dft(greenSignalNormalized,greenFFT,cv::DFT_ROWS|cv::DFT_COMPLEX_OUTPUT); cv::dft(greenSignalNormalized,greenFFT,cv::DFT_ROWS|cv::DFT_COMPLEX_OUTPUT);
cv::Mat planes[] = {cv::Mat::zeros(greenSignalNormalized.size(),1, CV_64F), cv::Mat planes[] = {cv::Mat::zeros(greenSignalNormalized.size(),1, CV_64F),
@ -137,22 +142,26 @@ int main(){
// display green FFT // display green FFT
cv::imshow("FFT module green", plotGraph(greenFFTModule, range)); cv::imshow("FFT module green", plotGraph(greenFFTModule, range));
std::vector<double> sampleVector{}; float maxValue=-1;
for (int i=0; i<greenFFTModule.length();++i) int indexValue=0;
for(auto i=0.5*(BUFFER_DURATION);i<(4*BUFFER_DURATION);i++)
{ {
if (greenFFTModule.at(i)>=0.5 && greenFFTModule.at(i)<=4) if(greenFFTModule[i]>maxValue)
{ {
sampleVector.push_back(greenFFTModule.at(i)); maxValue=greenFFTModule[i];
indexValue=i;
} }
} }
std::cout << "values in interval: "<<"\n"; float HRBPM=(indexValue*60.0)/(BUFFER_DURATION);
for (auto i: sampleVector()) std::cout<<HRBPM << std::endl;
{
std::cout << i << ' '; // will print vector's content
} }
//get maximum value of sampleVector and print it }
std::cout<<"max frequency: "<<"\n"<<max_element(sampleVector.begin(cloud), sampleVector.end(cloud)); }
return 0;
cv::imshow("Color", frame); //shows the colored frame
if (cv::waitKey(1000.0/FPS)>=0) //Stops after 1000/FPS frames
{
break;
} }
} }
} }