Note that there are some explanatory texts on larger screens.

plurals
  1. POConvert RGB IplImage to 3 arrays
    text
    copied!<p>I need some C++/pointer help. When I create an RGB IplImage and I want to access i,j I use the following C++ class taken from: <a href="http://www.cs.iit.edu/~agam/cs512/lect-notes/opencv-intro/opencv-intro.html" rel="nofollow noreferrer">http://www.cs.iit.edu/~agam/cs512/lect-notes/opencv-intro/opencv-intro.html</a></p> <pre><code>template&lt;class T&gt; class Image { private: IplImage* imgp; public: Image(IplImage* img=0) {imgp=img;} ~Image(){imgp=0;} void operator=(IplImage* img) {imgp=img;} inline T* operator[](const int rowIndx) { return ((T *)(imgp-&gt;imageData + rowIndx*imgp-&gt;widthStep));} }; typedef struct{ unsigned char b,g,r; } RgbPixel; typedef struct{ float b,g,r; } RgbPixelFloat; typedef Image&lt;RgbPixel&gt; RgbImage; typedef Image&lt;RgbPixelFloat&gt; RgbImageFloat; typedef Image&lt;unsigned char&gt; BwImage; typedef Image&lt;float&gt; BwImageFloat; </code></pre> <p>I've been working with CUDA so sometimes I have to put all the data into an array, I like to keep every channel in its own array, seems easier to handle the data that way. So I would usually do something like this:</p> <pre><code>IplImage *image = cvLoadImage("whatever.tif"); RgbImageFloat img(image); for(int i = 0; i &lt; exrIn-&gt;height; i++) { for(int j = 0; j &lt; exrIn-&gt;width; j++) { hostr[j*data-&gt;height+i] = img[i][j].r; hostg[j*data-&gt;height+i] = img[i][j].g; hostb[j*data-&gt;height+i] = img[i][j].b; } } </code></pre> <p>I would then copy my data to the device, do some stuff with it, get it back to the host and then loop, yet again, through the array assigning the data back to the IplImage and saving my results. </p> <p>It seems like I'm looping to much there has to be a faster way to do this with pointers but I'm lost, there has to be a more efficient way to do it. Is there a way I can simply use a pointer for every channel? I tried doing something like this but it didn't work:</p> <pre><code>float *hostr = &amp;img[0][0].r float *hostg = &amp;img[0][0].b float *hostb = &amp;img[0][0].g </code></pre> <p>Any suggestions? Thanks!</p> <p>EDIT: Thanks everyone for answering. Maybe I wasn't very clear on my question. I am familiar on how to access channels and their data. What I am interested is in increasing the performance and efficiency of completely copying data off the IplImage to a standard array, more along the lines of what csl said so far. The problem I see is that the way data in an IplImage is arranged is "rgbrgbrgbrgb".</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload