Skip to content

How it works

Jan Rabe edited this page Oct 4, 2019 · 1 revision

This wiki page is a backup of How-it-works

This is taken from a conversation I had.

Reported by hasanshareef1, May 5, 2012


could you please tell me what is the algorithm you have used for smoothing the red pixels signal?

May 21, 2012 Project Member #1 phishman3579


It's essentially a rolling median of the last 4 images. You'll see a big jump to the peak value when the heart beats.

Jun 12, 2012 #2 hasanshareef1


and why did you choose the last 4 images ? if i make it six for example what would be the influence on changing number of images?

Jul 8, 2012 Project Member #3 phishman3579


Nothing scientific about the number 4, it seemed to work best in my testing. The larger the number the.more likely you'll include both peak and trough of the heart beat in the average which is not desirable.

Aug 31, 2012 #5 newm2007


Can you give me more information about the algorithm that u use in it ? cause i want to make a graduation project like a ( heart monitor )

Aug 31, 2012 Project Member #6 phishman3579


Take a look at the code in HeartRateMonitor.java specifically the onPreviewFrame() method.

Aug 31, 2012 #7 newm2007


Thanks alot man i will look at the code the algorithm is tough...

Oct 3, 2012 #8 the.ghost01800


hmmmm cant understand why we using that the array dosent have any values right !!!

int averageArrayAvg=0;
int averageArrayCnt=0;
for (int i=0; i<averageArray.length; i++) {
     if (averageArray[i]>0) {
          averageArrayAvg += averageArray[i];
          averageArrayCnt++;
     }
}

Oct 4, 2012 Project Member #9 phishman3579


averageArray is updated each time you go through the code keeping the 4 latest averages.

Initially:

averageArray[0] = 0
averageArray[1] = 0
averageArray[2] = 0
averageArray[3] = 0
averageArray[4] = 0

First time through, let's say the average is 68

averageArray[0] = 68
averageArray[1] = 0
averageArray[2] = 0
averageArray[3] = 0
averageArray[4] = 0

Second time through, let's say the average is 72

averageArray[0] = 68
averageArray[1] = 72
averageArray[2] = 0
averageArray[3] = 0
averageArray[4] = 0

Second time through, average is 65

averageArray[0] = 68
averageArray[1] = 72
averageArray[2] = 65
averageArray[3] = 0
averageArray[4] = 0

Third time through, the average is 71

averageArray[0] = 68
averageArray[1] = 72
averageArray[2] = 65
averageArray[3] = 71
averageArray[4] = 0

Fourth time through, the average is 67

averageArray[0] = 68
averageArray[1] = 72
averageArray[2] = 65
averageArray[3] = 71
averageArray[4] = 67

Fifth time through; the average is 69, so I use the zero-th index.

averageArray[0] = 69
averageArray[1] = 72
averageArray[2] = 65
averageArray[3] = 71
averageArray[4] = 67`

Oct 4, 2012 #10 the.ghost01800


i got it thank u so much phishman

Oct 9, 2012 #11 [email protected]


hi, how are you can you tell me why you are using

greenBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.green_icon);
redBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.red_icon);

green_icon and red_icon, also can you provide me with this icon because they are not found in source code, all thanks to you

Oct 9, 2012 Project Member #12 phishman3579


They are located in res/drawable-mdpi folder of the source code.

https://code.google.com/p/android-heart-rate-monitor/source/browse/#svn%2Ftrunk%2Fres%2Fdrawable-mdpi

Oct 9, 2012 #13 [email protected]


Thanks very much,but can you tell me whats the benefits for using this images in app

Oct 9, 2012 Project Member #14 phishman3579


They are used to visually show the user when a heart beat happens. If you run the App on a phone, it's the small rectangle that changes from green to red and vice versa.

May 8, 2013 #16 hisham.171


Could you please explain how you detect the beat? (Theoretical side) Thank you.

May 8, 2013 Project Member #17 phishman3579


Basically, you want to use the camera to with as little focus as possible. So, if you put your finger on the lens, it'll be confused and not be able to focus. Because it isn't focused you'll essentially have very coarse data picking up only shades of light and dark RGB. I look at the a single channel (red) and try to detect when it goes from light red to dark red.

Jun 8, 2013 #18 rajishmathara


I can't understand the image processing steps. could you please explain that for me?

Jun 12, 2013 #19 PhedonRousou


I was wondering how can I extract the green values instead of the red values. In some papers that I have been reading, they say that the green values have less noise than any other color. Thus, I would like to give it a shot with the green color.

In the imageProcessing.java, in the decodeYUV420SPtoRedSum() method, there is this line of code:

int red = (pixel >> 16) & 0xff;

I suppose this extracts the red value. How can I extract the green value?

Jun 15, 2013 Project Member #20 phishman3579


You can try:

int green = (pixel >>  8) & 0xff;

Jul 1, 2013 #21 PhedonRousou


How can I detect whether the user's finger is placed on the camera or not? Can I somehow know that? For example, if the average of the red pixels are not greater than a specific number-threshold, then it means that the user's finger is not placed on the camera... Is it possible to do something like that or is there any easiest way? It would be a good addition to the project if we can do that.

Jul 7, 2013 #22 PhedonRousou


For anyone interested, I figured out how to detect whether the user has his finger placed on the camera or not. In the imageProcessing.java, in the decodeYUV420SPtoRedAvg() method, we calculate the average value of the pixels of the red color. Doing a small experiment, I found out that when the user has his finger placed on the camera lens, the average has a value of >200. In other case, the average value is <200. thus, in the HeartRateActivity you can add an if statement after the call of the decodeYUV420SPtoRedAvg(), to find wether the value returned is >200 or <199. You can use this to display an alert box or something similar to guide the user to place his finger on the camera lens.

Aug 29, 2013 #25 mo.extentia


Hi, I have seen that the app works fine if the surrounding light is good, but in dim light it does not give correct output like other leading apps.

Jan 6, 2015 #35 anish.vilayil.s


I couldn't understand the concept of Image Processing used here.? What is YUV420SP..? Is it a video format.?

Jan 6, 2015 #36 anish.vilayil.s


Suppose an averageArray value is getting and like you said it's 69 or maybe any value.... It's for 10's .? Is it.? How we can convert it into minute.? Beats per minute

Jan 25, 2015 #38 nayaka.ravikumar


Hi phishman, your application is working fine but can you please change algorithm, which can be like Heart rate. Your application retrieving each heart rate value every 10 seconds but Heart rate application is calculating average heart rate value with in 10 seconds. thanks in advance.

Jan 27, 2015 Project Member #39 phishman3579 via email


nayaka.ravikumar,

The code is completely open sourced, feel free to fork the code and change the algorithm to be whatever you'd like.

Jan 27, 2015 Project Member #40 phishman3579 via email


anish.vilayil.s,

It's a simple change; look at like 172 here.. the reference to 10 is the signal.

https://code.google.com/p/android-heart-rate-monitor/source/browse/src/com/jwetherell/heart_rate_monitor/HeartRateMonitor.java#172
Clone this wiki locally