Why this simple skin detection code always returns Message=Arithmetic operation resulted in an overflow.
xMax = bmp.Width - 1 : yMax = bmp.Height - 1
For y = 0 To yMax
For x = 0 To xMax
tmpColor = fixColor(bmp.GetPixel(x, y))
If (((tmpColor.R > 95) And (tmpColor.G > 40) And (tmpColor.B > 20) And (tmpColor.R - tmpColor.G > 15) And (tmpColor.R > tmpColor.G) And (tmpColor.R > tmpColor.B)) Or _
((tmpColor.R > 220) And (tmpColor.G > 210) And (tmpColor.B > 170) And (tmpColor.R - tmpColor.G <= 15) And (tmpColor.R > tmpColor.B) And (tmpColor.G > tmpColor.B))) Then bmp.SetPixel(x, y, Color.Black)
Next x
Next y
Assuming that
tmpColoris defined asSystem.Color, an educated guess would be that, when this error occurstmpColor.Gis greater thantmpColor.R, which would render the result less than zero, and unable to be stored into abyte.One possible solution would be to do this
and then use these new values within your calculation. It would make the code cleaner some (and much cleaner if you were to put casting within that if statement).
Another option would be to re-order the tests relying on that subtraction, and use the AndAlso operator:
AndAlsois VB.Net’s short-circuiting logical-and operator, and will cause evaluation of the expression to stop at the firstFalse. As all you use isAnds, replacing them all withAndAlsomight see a minor performance increase.A combination of these two items might make the code more readable, overall.