Try looking at the average difference over all bins
if mean(HistDiff) > threshold
By the way, don't you want
The mean * 4 will get clipped to 255 most likely so you'll never exceed it. Also, be sure not to use mean or std as variable names since they're built-in functions. And you don't want to add the mean because the difference has the mean already subtracted out.