OpenCV Q&A Forum - RSS feedhttp://answers.opencv.org/questions/OpenCV answersenCopyright <a href="http://www.opencv.org">OpenCV foundation</a>, 2012-2018.Tue, 24 Jul 2012 12:43:11 -0500Bilinear sampling from a GpuMathttp://answers.opencv.org/question/646/bilinear-sampling-from-a-gpumat/Hi everyone,
I'm writing a GPU-based shape/appearance model, for which I have to crop patches centered on given key points. The patches are square but not necessarily aligned with the image axes, so I cannot just use a rowRange/colRange. My plan is to create a fixed matrix of coordinate offsets, O:
O = [x1, x2, ..., xn;
y2, y2, ..., yn;
1, 1, ..., 1]
In Homogeneous coordinates. I will store this matrix on the GPU. When I want to sample a patch around X = [x, y, 1]^T, I simply transform the coordinates by a similarity transformation matrix M (which performs translation, rotation and scaling).
P = M * O
So P will again have the same layout as O, but with transformed coordinates.
**Now for the question**:
Given a matrix P of coordinates, how can I sample an image f(x,y) at the coordinates in P in an efficient manner? The output should be a vector or matrix with the pixel values at the coordinates in P. I want to use bilinear sampling, which is a built in operation on the GPU (so it should be efficient). I suppose I could write a custom kernel for this, but I would think this is already in opencv somewhere. I searched the documentation but didn't find anything.
Alternatively, I could rotate/scale the whole image and then crop an axis-aligned patch, but this seems less efficient.
Thanks in advanceMon, 23 Jul 2012 03:57:42 -0500http://answers.opencv.org/question/646/bilinear-sampling-from-a-gpumat/Comment by XCoder for <p>Hi everyone,</p>
<p>I'm writing a GPU-based shape/appearance model, for which I have to crop patches centered on given key points. The patches are square but not necessarily aligned with the image axes, so I cannot just use a rowRange/colRange. My plan is to create a fixed matrix of coordinate offsets, O:</p>
<pre><code>O = [x1, x2, ..., xn;
y2, y2, ..., yn;
1, 1, ..., 1]
</code></pre>
<p>In Homogeneous coordinates. I will store this matrix on the GPU. When I want to sample a patch around X = [x, y, 1]^T, I simply transform the coordinates by a similarity transformation matrix M (which performs translation, rotation and scaling).</p>
<pre><code>P = M * O
</code></pre>
<p>So P will again have the same layout as O, but with transformed coordinates.</p>
<p><strong>Now for the question</strong>:
Given a matrix P of coordinates, how can I sample an image f(x,y) at the coordinates in P in an efficient manner? The output should be a vector or matrix with the pixel values at the coordinates in P. I want to use bilinear sampling, which is a built in operation on the GPU (so it should be efficient). I suppose I could write a custom kernel for this, but I would think this is already in opencv somewhere. I searched the documentation but didn't find anything.</p>
<p>Alternatively, I could rotate/scale the whole image and then crop an axis-aligned patch, but this seems less efficient.</p>
<p>Thanks in advance</p>
http://answers.opencv.org/question/646/bilinear-sampling-from-a-gpumat/?comment=650#post-id-650"Alternatively, I could rotate/scale the whole image and then crop an axis-aligned patch, but this seems less efficient." - This exactly what I'm doing in my project, it might not be the most efficient way to do it, but it takes less than 2 millisecond to execute on my computer while image being 640x480. Mon, 23 Jul 2012 04:14:37 -0500http://answers.opencv.org/question/646/bilinear-sampling-from-a-gpumat/?comment=650#post-id-650Answer by tsc for <p>Hi everyone,</p>
<p>I'm writing a GPU-based shape/appearance model, for which I have to crop patches centered on given key points. The patches are square but not necessarily aligned with the image axes, so I cannot just use a rowRange/colRange. My plan is to create a fixed matrix of coordinate offsets, O:</p>
<pre><code>O = [x1, x2, ..., xn;
y2, y2, ..., yn;
1, 1, ..., 1]
</code></pre>
<p>In Homogeneous coordinates. I will store this matrix on the GPU. When I want to sample a patch around X = [x, y, 1]^T, I simply transform the coordinates by a similarity transformation matrix M (which performs translation, rotation and scaling).</p>
<pre><code>P = M * O
</code></pre>
<p>So P will again have the same layout as O, but with transformed coordinates.</p>
<p><strong>Now for the question</strong>:
Given a matrix P of coordinates, how can I sample an image f(x,y) at the coordinates in P in an efficient manner? The output should be a vector or matrix with the pixel values at the coordinates in P. I want to use bilinear sampling, which is a built in operation on the GPU (so it should be efficient). I suppose I could write a custom kernel for this, but I would think this is already in opencv somewhere. I searched the documentation but didn't find anything.</p>
<p>Alternatively, I could rotate/scale the whole image and then crop an axis-aligned patch, but this seems less efficient.</p>
<p>Thanks in advance</p>
http://answers.opencv.org/question/646/bilinear-sampling-from-a-gpumat/?answer=662#post-id-662I've found the solution:
gpu::buildWarpAffineMaps - to build matrices of x and y coordinates containing the lookup coordinates
gpu::remap - to sample the image given the lookup coordinatesMon, 23 Jul 2012 09:40:03 -0500http://answers.opencv.org/question/646/bilinear-sampling-from-a-gpumat/?answer=662#post-id-662Comment by Philipp Wagner for <p>I've found the solution:</p>
<p>gpu::buildWarpAffineMaps - to build matrices of x and y coordinates containing the lookup coordinates</p>
<p>gpu::remap - to sample the image given the lookup coordinates</p>
http://answers.opencv.org/question/646/bilinear-sampling-from-a-gpumat/?comment=732#post-id-732Ah didn't know it. Sorry! :)Tue, 24 Jul 2012 12:43:11 -0500http://answers.opencv.org/question/646/bilinear-sampling-from-a-gpumat/?comment=732#post-id-732Comment by Philipp Wagner for <p>I've found the solution:</p>
<p>gpu::buildWarpAffineMaps - to build matrices of x and y coordinates containing the lookup coordinates</p>
<p>gpu::remap - to sample the image given the lookup coordinates</p>
http://answers.opencv.org/question/646/bilinear-sampling-from-a-gpumat/?comment=680#post-id-680You could also accept your answer. :)Mon, 23 Jul 2012 13:43:41 -0500http://answers.opencv.org/question/646/bilinear-sampling-from-a-gpumat/?comment=680#post-id-680Comment by tsc for <p>I've found the solution:</p>
<p>gpu::buildWarpAffineMaps - to build matrices of x and y coordinates containing the lookup coordinates</p>
<p>gpu::remap - to sample the image given the lookup coordinates</p>
http://answers.opencv.org/question/646/bilinear-sampling-from-a-gpumat/?comment=725#post-id-725It says I need >50 points to accept my answer. I'm new here :)Tue, 24 Jul 2012 08:58:22 -0500http://answers.opencv.org/question/646/bilinear-sampling-from-a-gpumat/?comment=725#post-id-725