OpenCV Q&A Forum - RSS feedhttp://answers.opencv.org/questions/OpenCV answersenCopyright <a href="http://www.opencv.org">OpenCV foundation</a>, 2012-2018.Mon, 12 Sep 2016 08:47:59 -0500Change pixel x,y location with mathematical functionhttp://answers.opencv.org/question/102020/change-pixel-xy-location-with-mathematical-function/ As the title suggests, I have an image, whose pixels I want to move based on a mathematical function. The code I use right now is consisted of two loops. And because of the nested loop, the process takes forever, -to be exact more than two minutes most of the times-, having in mind that each image has 12000 pixels to run through the loops. Is there another way to do this? I looked through the documentation but couldn't find anything.
imgcor = np.zeros(img.shape, dtype=img.dtype)
for f in range(rowc):
for k in range(colc):
offX = k + (f*b*c*(math.sin(math.radians(a))))
offY = f + (f*b*d*(math.cos(math.radians(a))))
imgcor[f, k] = img[int(offY)%rowc, int(offX)%colc]
The a,b,c,d values are certain parameters that are assigned earlier in the script.
P.S. I am using Python2.7 and opencv 2.4Fri, 09 Sep 2016 09:47:39 -0500http://answers.opencv.org/question/102020/change-pixel-xy-location-with-mathematical-function/Comment by kbarni for <p>As the title suggests, I have an image, whose pixels I want to move based on a mathematical function. The code I use right now is consisted of two loops. And because of the nested loop, the process takes forever, -to be exact more than two minutes most of the times-, having in mind that each image has 12000 pixels to run through the loops. Is there another way to do this? I looked through the documentation but couldn't find anything.</p>
<pre><code>imgcor = np.zeros(img.shape, dtype=img.dtype)
for f in range(rowc):
for k in range(colc):
offX = k + (f*b*c*(math.sin(math.radians(a))))
offY = f + (f*b*d*(math.cos(math.radians(a))))
imgcor[f, k] = img[int(offY)%rowc, int(offX)%colc]
</code></pre>
<p>The a,b,c,d values are certain parameters that are assigned earlier in the script.</p>
<p>P.S. I am using Python2.7 and opencv 2.4</p>
http://answers.opencv.org/question/102020/change-pixel-xy-location-with-mathematical-function/?comment=102199#post-id-102199That's strange.
I implemented a similar method for optical corrections (two nested *for* loops) in C++. It has more complex operations to get the pixel coordinates.
I benchmarked it, it can process a huge 80 megapixel RGB photo in 250ms (1/4s) on my laptop.
Anyway, for further optimisations, you can parallelize the outer loop (I don't know how it's done in Python). My parallelized function processed the same image in 150ms (1/7s).Mon, 12 Sep 2016 08:47:59 -0500http://answers.opencv.org/question/102020/change-pixel-xy-location-with-mathematical-function/?comment=102199#post-id-102199Comment by kbarni for <p>As the title suggests, I have an image, whose pixels I want to move based on a mathematical function. The code I use right now is consisted of two loops. And because of the nested loop, the process takes forever, -to be exact more than two minutes most of the times-, having in mind that each image has 12000 pixels to run through the loops. Is there another way to do this? I looked through the documentation but couldn't find anything.</p>
<pre><code>imgcor = np.zeros(img.shape, dtype=img.dtype)
for f in range(rowc):
for k in range(colc):
offX = k + (f*b*c*(math.sin(math.radians(a))))
offY = f + (f*b*d*(math.cos(math.radians(a))))
imgcor[f, k] = img[int(offY)%rowc, int(offX)%colc]
</code></pre>
<p>The a,b,c,d values are certain parameters that are assigned earlier in the script.</p>
<p>P.S. I am using Python2.7 and opencv 2.4</p>
http://answers.opencv.org/question/102020/change-pixel-xy-location-with-mathematical-function/?comment=102025#post-id-102025Normally an algorithm like this should be very fast.
If you have constant parameters, try to precompute the most you can: `m=b*math.sin(math.radians(a))`, and then `offX=k+(f*c*m)`. I think the sinus function takes the most time in your function.Fri, 09 Sep 2016 11:13:26 -0500http://answers.opencv.org/question/102020/change-pixel-xy-location-with-mathematical-function/?comment=102025#post-id-102025Comment by n.argirop for <p>As the title suggests, I have an image, whose pixels I want to move based on a mathematical function. The code I use right now is consisted of two loops. And because of the nested loop, the process takes forever, -to be exact more than two minutes most of the times-, having in mind that each image has 12000 pixels to run through the loops. Is there another way to do this? I looked through the documentation but couldn't find anything.</p>
<pre><code>imgcor = np.zeros(img.shape, dtype=img.dtype)
for f in range(rowc):
for k in range(colc):
offX = k + (f*b*c*(math.sin(math.radians(a))))
offY = f + (f*b*d*(math.cos(math.radians(a))))
imgcor[f, k] = img[int(offY)%rowc, int(offX)%colc]
</code></pre>
<p>The a,b,c,d values are certain parameters that are assigned earlier in the script.</p>
<p>P.S. I am using Python2.7 and opencv 2.4</p>
http://answers.opencv.org/question/102020/change-pixel-xy-location-with-mathematical-function/?comment=102029#post-id-102029@kbarni Thanks for the reply! I thought about after I posted my question and tried it but the differences are -if not zero- at least very small (maybe less than a second).Fri, 09 Sep 2016 12:21:50 -0500http://answers.opencv.org/question/102020/change-pixel-xy-location-with-mathematical-function/?comment=102029#post-id-102029Answer by FooBar for <p>As the title suggests, I have an image, whose pixels I want to move based on a mathematical function. The code I use right now is consisted of two loops. And because of the nested loop, the process takes forever, -to be exact more than two minutes most of the times-, having in mind that each image has 12000 pixels to run through the loops. Is there another way to do this? I looked through the documentation but couldn't find anything.</p>
<pre><code>imgcor = np.zeros(img.shape, dtype=img.dtype)
for f in range(rowc):
for k in range(colc):
offX = k + (f*b*c*(math.sin(math.radians(a))))
offY = f + (f*b*d*(math.cos(math.radians(a))))
imgcor[f, k] = img[int(offY)%rowc, int(offX)%colc]
</code></pre>
<p>The a,b,c,d values are certain parameters that are assigned earlier in the script.</p>
<p>P.S. I am using Python2.7 and opencv 2.4</p>
http://answers.opencv.org/question/102020/change-pixel-xy-location-with-mathematical-function/?answer=102032#post-id-102032Have a look at cv::remap Fri, 09 Sep 2016 13:35:35 -0500http://answers.opencv.org/question/102020/change-pixel-xy-location-with-mathematical-function/?answer=102032#post-id-102032