title: C++, Access Violation using OpenCV to get RGB value of pixel
body: <p>I'm trying to use OpenCV to find the RGB values of a pixel in an image. so far I've tried the following:</p>
  
  <pre><code>int blue = ((uchar *)(img-&gt;imageData + y*img-&gt;widthStep))[x*img-&gt;nChannels + 0];
  int green = ((uchar *)(img-&gt;imageData + y*img-&gt;widthStep))[x*img-&gt;nChannels + 1];
  int red = ((uchar *)(img-&gt;imageData + y*img-&gt;widthStep))[x*img-&gt;nChannels + 2];
  
  int blue = ((float *)(img-&gt;imageData + i*img-&gt;widthStep))[j*img-&gt;nChannels + 0];
  int green = ((float *)(img-&gt;imageData + i*img-&gt;widthStep))[j*img-&gt;nChannels + 1];
  int red = ((float *)(img-&gt;imageData + i*img-&gt;widthStep))[j*img-&gt;nChannels + 2];
  
  CvPoint pt = {5,5};
  uchar* temp_ptr = &amp;((uchar*)(img-&gt;imageData + img-&gt;widthStep*pt.y))[pt.x*3];
  int blue = temp_ptr[0];
  int green = temp_ptr[1];
  int red = temp_ptr[2];
  </code></pre>
  
  <p>But in all of the above, I get the same error:</p>
  
  <pre><code>Unhandled exception at 0x00f5104f in test.exe: 0xC0000005: Access violation reading location: 0x00000048
  </code></pre>
  
  <p>The last hex number (<code>0x0...48</code>) never changes.  I looks like this can be caused by writing further than the bounds of an array.  So I've run each of the examples in isolation without any other code at all, and still get the same error.  What is causing this error and how can I fix it?</p>
  
  <p>Extra info:  Windows 7, MSVC 2010 Express, OpenCV 2.1</p>
  
  <pre><code>--UPDATE--
  </code></pre>
  
  <p>I've realised the above code is more compicated than it needs to be, so I took the snippet provided by karlphillip (thanks!) as a base and used a similar method.  I'm still getting an error, and this time in an even stranger place:</p>
  
  <pre><code>IplImage *slice = cvLoadImage("test.png");
  int bpp = slice -&gt;nChannels;
  </code></pre>
  
  <p>The error occurs on the second line, and is still an Access Violation.  There is no code executed before this to do with OpenCV, just some variable initializations.  'test.png' is just a 7*7 pixel 'X' I made in paint to test this out, using a .jpg has hte saem result.</p>
  
  <p>To make sure I hadn't installed OpenCV improperly, I used this code (copied from below) in isolation:</p>
  
  <pre><code>int main ()
  {
      IplImage* pRGBImg = cvCreateImage(cvSize(5,5),IPL_DEPTH_8U,3);
  
      int width = pRGBImg-&gt;width; 
      int height = pRGBImg-&gt;height;
      int bpp = pRGBImg-&gt;nChannels; 
  
      cvNamedWindow("Image view", 1);
      cvShowImage("Image view", pRGBImg);
      cvWaitKey(0);
      cvDestroyWindow("Image view");
  
      for (int i=0; i &lt; width*height*bpp; i+=bpp) 
      {
        if (!(i % (width*bpp))) // print empty line for better readability
            std::cout &lt;&lt; std::endl;
  
        std::cout &lt;&lt; std::dec &lt;&lt; "R:" &lt;&lt; (int) pRGBImg-&gt;imageData[i] &lt;&lt;  
                                " G:" &lt;&lt; (int) pRGBImg-&gt;imageData[i+1] &lt;&lt;  
                                " B:" &lt;&lt; (int) pRGBImg-&gt;imageData[i+2] &lt;&lt; " "; 
      }
  }
  </code></pre>
  
  <p>This didn't return any errors, but I did get some possibly strange results, here are the first few lines of console output:</p>
  
  <pre><code>R:13 G:-16 B:-83
  R:-70: G:13 B:-16
  R:-83 G:-70 B: 13
  </code></pre>
  
  <p>Negative RGB values?  Is this to be expected, or is even this not working.  If it is normal, then the image I'm loading ('test.png') must be the problem.  But, what am I doing wrong if a simple request for the number of channels causes an access violation?</p>