- Home /
How to recognize movement in webcam?
I used webcamtexture.GetPixel(x,y). But it always returned different values every time, even though I covered webcam with paper. I just want to return true if there is any movement in webcam, and return false vice versa. I don't have any other devices such as Leapmotion controller or Kinect. I can't buy them. Then, how can i recognize any movement in Unity webcam? It doesn't matter whether it would be Unity 2D or 3D. Thank you in advance!
Since GetPixel returns a Color instance, you've probably noticed that RGB values can be a bit tedious to compare with one another. You're probably also dealing with electronic gain and general camera stuff which will change your pixel values slightly between frames.
Pixel comparisons are a bit simpler with an HSV format, so you might want to try that:
[Range(0, 1)] public float dh = 0.1f;
[Range(0, 1)] public float ds = 0.1f;
[Range(0, 1)] public float dv = 0.1f;
float h, s, v, hp, sp, vp;
void Update()
{
Color.RGBToHSV(
webcamtexture.GetPixel(x,y),
out h,
out s,
out v
);
var hueChanged = $$anonymous$$athf.Abs(h - hp) > dh;
var satChanged = $$anonymous$$athf.Abs(s - sp) > ds;
var valChanged = $$anonymous$$athf.Abs(v - vp) > dv;
if (hueChanged || satChanged || valChanged)
Debug.LogFormat("Pixel {0},{1} changed!");
hp = h;
sp = s;
vp = v;
}
Note that Hue needs to account for wrapping. This was just a quick and dirty block to help you move in the right direction, but you'll need to make sure things like 0.95 and 0.04 are correctly evaluated as being within 0.1 of each other.
Converting to comment, didn't see that you wanted to detect movement in general. That is a bit different than pixel comparison.
What problem are you trying to solve? What does your ideal outcome look like here?
Answer by sh_code · Jan 17, 2019 at 02:48 PM
this is a very difficult problem even when you're using libraries. don't attempt to implement something like this for yourself, use a CV (computer-vision) or AR (augmented-reality) library, such as OpenCV, or Vuforia (comes built-in unity), or metaio, or something like that.
the pixel(s) always change value even if covered with a sheet of paper because of signal noise.
in general, whatever are you trying to do with the webcam image, and recognizing anything meaningful from it, is going to be hard even using the libraries I mentioned above.
Answer by xxmariofer · Jan 17, 2019 at 02:50 PM
They are returning true cause even if it black it will probably be a bit darker some times, try the code but using a float and discard if the difference between old and new texture are lower than those
Your answer
![](https://koobas.hobune.stream/wayback/20220612185930im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Could not connect pins - RenderStream() UnityEngine.WebCamTexture:Play() 0 Answers
Show a WebCamTexture with alpha 1 Answer
Efficient way to Capture Webcam image stream? 0 Answers
WebcamTexture not refreshing 0 Answers
Web Cam Texture on UI Panel 0 Answers