You are fantastic. Thanks for the explanation. I will try and work out these values by hand using the explanation you gave me and hopefully make sense of the result!<br><br>Thank you so much!<br><br>Luca<br><br><div><span class="gmail_quote">
On 9/19/07, <b class="gmail_sendername">David Gobbi</b> &lt;<a href="mailto:david.gobbi@gmail.com">david.gobbi@gmail.com</a>&gt; wrote:</span><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
Hi Luca,<br><br>The equations for bilinear interpolation are easy.&nbsp;&nbsp;Let&#39;s say that you<br>want to interpolate a value at (x,y).&nbsp;&nbsp;First thing the algorithm does is<br>calculate (i,j) = (floor(x), floor(y)) to find the indices of a pixel in the
<br>original image.&nbsp;&nbsp;Then it calculates fx = x-i and fy = y-j, and uses these to<br>compute interpolation weights for the four voxels surrounding (x,y),<br>so that it can give you v(x,y) which is the value of the new pixel:
<br><br>v(x,y) = (1-fx)(1-fy)*v(i,j) + (1-fx)(fy)*v(i,j+1) +<br>(fx)(1-fy)*v(i+1,j) + (fx)(fy)*v(i+1,j+1)<br><br>The reason that your results are odd must be due to the way that (x,y)<br>is calculated.&nbsp;&nbsp;For the simple vtkImageReslice example that I gave in
<br>my previous email, the x values at which the new pixel are interpolated are<br>x = [0, 0.5, 1, 1.5, 2, 2.5, 3, 3.5, 4]<br><br>It&#39;s just my guess by looking at your numbers, that for you the x values are<br>x = [-0.25
, 0.25, 0.75, 1.25, 1.75, 2.25, 2.75, 3.25, 3.75, 4.25]<br><br>I don&#39;t want to get too technical here, but both of the above sets of x values<br>are correct if you want to get a zoom factor of 2.0.&nbsp;&nbsp;Which of the two<br>
behaviours<br>you want depends on how you want edges to be handled.<br><br>The first behaviour (averaging the neighboring pixels) is what you want if<br>you are expanding a 5x5 image to create a 9x9 image.&nbsp;&nbsp;As for why you get
<br>a 9x9 image when you do averaging like this, instead of a 10x10 image,<br>that should be obvious when you think about it.<br><br>The second behaviour with all those 0.25&#39;s is what you use if you really need<br>to expand your 5x5 image into a 10x10 image.&nbsp;&nbsp;Again, let me be clear
<br>on this: both of these behaviours are giving you a zoom factor of 2.0.<br>The difference lies in how you want the edges to be handled.<br><br>So, now about edges.&nbsp;&nbsp;The edge of the image is defined as the position<br>of the center of the outermost pixels.&nbsp;&nbsp;Please note that I said the CENTER
<br>of those pixels.&nbsp;&nbsp;That is how VTK defines the edge, and this is the best<br>definition in most circumstances, so please don&#39;t argue with that definition.<br>When vtkImageReslice needs to interpolate beyond the edge of an image,
<br>(e.g. when x = -0.25) the rule that is applied is as follows<br>1) if the distance from x to the edge is less than half of the output pixel<br>&nbsp;&nbsp;&nbsp;&nbsp;spacing, then x is clamped to the edge before interpolation occurs<br>
2) if the distance is greater than half of the output pixel spacing, then<br>&nbsp;&nbsp;&nbsp;&nbsp;the output pixel is set to the background color<br>3) In vtk 4 and earlier, this &quot;half pixel&quot; rule doesn&#39;t apply, all out-of-bounds
<br>&nbsp;&nbsp;&nbsp;&nbsp;pixels were set to the background value until vtk 5.0<br><br>So let me quickly summarize: I think that your results are following<br>behaviour 2 as described above.&nbsp;&nbsp;Whether or not this is what you want,<br>well, that&#39;s up to you.&nbsp;&nbsp;As for why vtkImageReslice is giving you these
<br>results, I can&#39;t tell you that unless you post your code.<br><br> David<br><br><br><br>On 9/19/07, Luca Pamparana &lt;<a href="mailto:luca.pamparana@gmail.com">luca.pamparana@gmail.com</a>&gt; wrote:<br>&gt; Hi David,
<br>&gt;<br>&gt; Many many thanks for answering my question! I am really grateful.<br>&gt;<br>&gt; Yes, you are right I see that the number of pixels generated are correct.<br>&gt; However, I am unable to figure out what the algorithms are doing to generate
<br>&gt; the values.<br>&gt;<br>&gt; For example, I created a small synthetic image (unsigned char) type with the<br>&gt; following pattern. Origin is at lower left (image dimension 5 X 5):<br>&gt;<br>&gt; 2&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 3&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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sp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 2&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;3&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;3&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 4
<br>&gt;&nbsp;&nbsp; 4<br>&gt;<br>&gt; What I want to know is how this is calculated? Could you, by any chance,<br>&gt; tell me the formula and the process as which pixels are considered when<br>&gt; calculating the intermediate values and what happens with the edge pixels? I
<br>&gt; see the first two rows are exactly the same.<br>&gt;<br>&gt; Also, what about cubic interpolation? Is it something similar as well?<br>&gt;<br>&gt; I am attaching an image of the output, so you can see the pixel blocks. If
<br>&gt; you load it in GIMP than you can easily see each pixel and see the intensity<br>&gt; value with the color picker tool...<br>&gt;<br>&gt; Again, I will be really grateful for all your help.<br>&gt;<br>&gt; Thanks,
<br>&gt;<br>&gt; Luca<br>&gt;<br>&gt;<br>&gt; On 9/19/07, David Gobbi &lt; <a href="mailto:david.gobbi@gmail.com">david.gobbi@gmail.com</a>&gt; wrote:<br>&gt; &gt; Hi Luca,<br>&gt; &gt;<br>&gt; &gt; To see how vtkImageReslice zooms an image by a factor of 2, you
<br>&gt; &gt; should be doing something like this:<br>&gt; &gt;<br>&gt; &gt; vtkPNGReader *reader = vtkPNGReader::New();<br>&gt; &gt; reader-&gt;SetFileName(&quot;myfile.png&quot;);<br>&gt; &gt; reader-&gt;SetDataSpacing(
1.0, 1.0, 1.0);&nbsp;&nbsp;// tell VTK what pixel spacing to<br>&gt; use<br>&gt; &gt;<br>&gt; &gt; vtkImageReslice *reslice = vtkImageReslice::New();<br>&gt; &gt; reslice-&gt;SetInput(reader-&gt;GetOutput());<br>&gt; &gt; reslice-&gt;SetOutputSpacing(
0.5, 0.5, 0.5);&nbsp;&nbsp;// resample with zoom factor of<br>&gt; 2.0<br>&gt; &gt;<br>&gt; &gt; If you write the output of this reslice to a file, you will see<br>&gt; &gt; exactly what you expect, i.e. a new pixel inserted between all
<br>&gt; &gt; the old pixels in both the vertical and horizontal directions.&nbsp;&nbsp;So if<br>&gt; &gt; the original image size was 100x100, the new image will be<br>&gt; &gt; 199x199 because 99 new pixels have been inserted in the
<br>&gt; &gt; horizontal and vertical directions.<br>&gt; &gt;<br>&gt; &gt; Since this is not what you are seeing, then there might be something<br>&gt; &gt; wrong with the way you are appying the zoom factor.&nbsp;&nbsp;If you can send
<br>&gt; &gt; a few lines of code that show how you are using vtkImageReslice, that<br>&gt; &gt; would help me to fully answer your question.<br>&gt; &gt;<br>&gt; &gt; Cheers,<br>&gt; &gt;<br>&gt; &gt;&nbsp;&nbsp; David<br>&gt; &gt;
<br>&gt; &gt;<br>&gt; &gt; On 9/19/07, Luca Pamparana &lt;<a href="mailto:luca.pamparana@gmail.com">luca.pamparana@gmail.com</a>&gt; wrote:<br>&gt; &gt; &gt; Hi everyone,<br>&gt; &gt; &gt;<br>&gt; &gt; &gt; I had asked this question a few days back but did not receive any help.
<br>&gt; &gt; &gt; Asking it again hoping someone would answer my query!<br>&gt; &gt; &gt;<br>&gt; &gt; &gt; Just trying to understand the linear interpolation algorithm as<br>&gt; implemented<br>&gt; &gt; &gt; in the vtkImageReslice class. I tried looking at the source code but it
<br>&gt; is<br>&gt; &gt; &gt;<br>&gt; &gt; &gt; really complicated and I could not understand much :(<br>&gt; &gt; &gt;<br>&gt; &gt; &gt; Anyway, I am zooming my image by a factor of 2. The image is a synthetic<br>&gt; &gt; &gt; image which has a thick edge going along its diagonal (the value is
<br>&gt; changing<br>&gt; &gt; &gt; from 0 to 255) and I am trying to look at how the values are<br>&gt; interpolated in<br>&gt; &gt; &gt; that region.<br>&gt; &gt; &gt;<br>&gt; &gt; &gt;<br>&gt; &gt; &gt; One thing I noticed is that there are 2 pixels being inserted in the
<br>&gt; &gt; &gt; horizontal direction. So, I get the values 255, 191, 64 and 0 (255 and 0<br>&gt; are<br>&gt; &gt; &gt; the old values and the two new values are being generated by taking a<br>&gt; &gt; &gt; weighted average). Is this correct? I thought that if I zoom by a factor
<br>&gt; of<br>&gt; &gt; &gt;<br>&gt; &gt; &gt; 2.0 than one pixel should be inserted between successive pixels. I am<br>&gt; not<br>&gt; &gt; &gt; sure what happens at the edge pixels though.<br>&gt; &gt; &gt;<br>&gt; &gt; &gt; Also, what happens in the vertical direction? I cannot really figure out
<br>&gt; how<br>&gt; &gt; &gt; this works...<br>&gt; &gt; &gt;<br>&gt; &gt; &gt;<br>&gt; &gt; &gt; So if I have pixel values as follows:<br>&gt; &gt; &gt;<br>&gt; &gt; &gt; 255 0 1 2<br>&gt; &gt; &gt; 0 255 0 1<br>&gt; &gt; &gt;
<br>&gt; &gt; &gt; How is the lienar interpolation being done on a zoom of 2.0?<br>&gt; &gt; &gt;<br>&gt; &gt; &gt; I would really appreciate it if someone can clarify these doubts for<br>&gt; me....<br>&gt; &gt; &gt; I have spend a lot of time trying to understand this but have not yet
<br>&gt; been<br>&gt; &gt; &gt; able to comprehend this implementation.<br>&gt; &gt; &gt;<br>&gt; &gt; &gt;<br>&gt; &gt; &gt; Thanks,<br>&gt; &gt; &gt;<br>&gt; &gt; &gt; Luca<br>&gt; &gt; &gt;<br>&gt; &gt; &gt; _______________________________________________
<br>&gt; &gt; &gt; This is the private VTK discussion list.<br>&gt; &gt; &gt; Please keep messages on-topic. Check the FAQ at:<br>&gt; &gt; &gt; <a href="http://www.vtk.org/Wiki/VTK_FAQ">http://www.vtk.org/Wiki/VTK_FAQ</a>
<br>&gt; &gt; &gt; Follow this link to subscribe/unsubscribe:<br>&gt; &gt; &gt; <a href="http://www.vtk.org/mailman/listinfo/vtkusers">http://www.vtk.org/mailman/listinfo/vtkusers</a><br>&gt; &gt; &gt;<br>&gt; &gt; &gt;<br>
&gt; &gt;<br>&gt;<br>&gt;<br>&gt;<br></blockquote></div><br>