"whether I am calculating only from the face pixels or the entire image including black as background." <== you are calculating total_median from G, which is the green channel of the background-removed image that you wrote to disk. I must say though that it looks like a very roundabout way of coding, like why write the rgb matrix out to disk only to read it back in as img? Etc.
Best Answer