# Kinect v2 Color Tracking Issue : ArrayIndexOutOfBoundsException

**URL:** <https://discourse.processing.org/t/kinect-v2-color-tracking-issue-arrayindexoutofboundsexception/5636>\
**Category:** Libraries\
**Created:** [November 17, 2018, 7:44pm UTC](https://discourse.processing.org/t/kinect-v2-color-tracking-issue-arrayindexoutofboundsexception/5636 "2018-11-17T19:44:30Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![flemingo](https://avatars.discourse-cdn.com/v4/letter/f/67e7ee/32.png) [@flemingo](https://discourse.processing.org/u/flemingo)\
**Post date:** [November 17, 2018, 7:44pm UTC](https://discourse.processing.org/t/kinect-v2-color-tracking-issue-arrayindexoutofboundsexception/5636/1 "2018-11-17T19:44:30Z")

</div>

Hi everyone!

I’m currently working on a project to detect and track a color in the kinect image.  
However when my program gets to the line:

**_color currentColor = img.pixels [loc];_**

It crashes and gives me the following error message:

_ArrayIndexOutOfBoundsException: 825032704_

I have tried everything and I am really stuck at how to fix this so any advice would be much appreciated. I’ve seen this line of code being used online in many examples so I cannot understand why it won’t work.

Many thanks!

**My code is as follows:**

```auto
import processing.video.*;
import org.openkinect.processing.*;

Kinect2 kinect2;
color trackColor;

void setup() {
  size(526,424);

  kinect2 = new Kinect2(this);
  kinect2.initVideo();
  kinect2.initRegistered();
  kinect2.initDevice();
}

void draw(){
  PImage img = kinect2.getRegisteredImage();
  image(img,0,0);
  int [] depth = kinect2.getRawDepth();
  
  int avgX = 0;
  int avgY = 0;
  int count = 0;
  
   for(int x =0; x <kinect2.depthWidth; x++) {
   for(int y=0; y <kinect2.depthWidth; y++){
     int loc = x +y*kinect2.depthWidth;
     
     color currentColor = img.pixels [loc];

   }
   }
}

```

---

<div class="post-metadata">

**Author:** ![jb4x](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jb4x/32/789_2.png) [@jb4x](https://discourse.processing.org/u/jb4x)\
**Post date:** [November 17, 2018, 9:07pm UTC](https://discourse.processing.org/t/kinect-v2-color-tracking-issue-arrayindexoutofboundsexception/5636/2 "2018-11-17T21:07:54Z")

</div>

I guess you have to same assignment: [Video Processing with a Rasperry Pi](https://discourse.processing.org/t/video-processing-with-a-rasperry-pi/5540/4)

You should find the answer there.

---

<div class="post-metadata">

**Author:** ![flemingo](https://avatars.discourse-cdn.com/v4/letter/f/67e7ee/32.png) [@flemingo](https://discourse.processing.org/u/flemingo)\
**Post date:** [November 17, 2018, 9:56pm UTC](https://discourse.processing.org/t/kinect-v2-color-tracking-issue-arrayindexoutofboundsexception/5636/3 "2018-11-17T21:56:46Z")

</div>

Hi there,

thank you for your help!

So just to clarify do you mean that I should load the pixels before entering the for loops and then update the pixels within the loop?

Sorry I currently don’t have my kinect with me to check if this works!

Thanks!

---

<div class="post-metadata">

**Author:** ![jb4x](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jb4x/32/789_2.png) [@jb4x](https://discourse.processing.org/u/jb4x)\
**Post date:** [November 17, 2018, 10:05pm UTC](https://discourse.processing.org/t/kinect-v2-color-tracking-issue-arrayindexoutofboundsexception/5636/4 "2018-11-17T22:05:45Z")

</div>

In your case, you are just reading the pixels value so you don’t need to update them but otherwise yes: load the pixels before your for loop.

---

<div class="post-metadata">

**Author:** ![flemingo](https://avatars.discourse-cdn.com/v4/letter/f/67e7ee/32.png) [@flemingo](https://discourse.processing.org/u/flemingo)\
**Post date:** [November 18, 2018, 12:21pm UTC](https://discourse.processing.org/t/kinect-v2-color-tracking-issue-arrayindexoutofboundsexception/5636/5 "2018-11-18T12:21:51Z")

</div>

Hi There,

I added in that function but the program still freezes when I run that line of code.  
img.loadPixels();

This is the message that I get in the console:

```auto
[Freenect2DeviceImpl] submitting usb transfers...
[RgbPacketStreamParser::onDataReceived] packetsize or sequence doesn't match!
[Freenect2DeviceImpl] started
Device Serial: 502554743142
Device Firmware: 4.3.3916.0.7
[DepthPacketStreamParser::onDataReceived] not all subsequences received 0
[TurboJpegRgbPacketProcessor::doProcess] Failed to decompress rgb image! TurboJPEG error: 'Corrupt JPEG data: premature end of data segment'
[DepthPacketStreamParser::onDataReceived] not all subsequences received 1022
[DepthPacketStreamParser::onDataReceived] not all subsequences received 512
[DepthPacketStreamParser::onDataReceived] not all subsequences received 959
Could not run the sketch (Target VM failed to initialize).
For more information, read revisions.txt and Help ? Troubleshooting.

```

But that has cleared up the _ArrayIndexOutOfBoundsException_ error.

\*\*\*\*\*EDIT \*\*\*  
Actually so I’m getting different error messages in the console each time but theyre something along the lines:

```auto
[TurboJpegRgbPacketProcessor::doProcess] Failed to decompress rgb image! TurboJPEG error: 'Corrupt JPEG data: 860 extraneous bytes before marker 0xd5'

```

or

```auto
[TurboJpegRgbPacketProcessor::doProcess] Failed to decompress rgb image! TurboJPEG error: 'Corrupt JPEG data: premature end of data segment'

```

The ArrayIndexOutOfBoundsException error is still coming up too.
