title: Difference between Matlab's imwrite and dct2 for JPEG compression
body: <p>Below is a snippet taken from matlab's <a href="https://www.mathworks.com/help/matlab/ref/imwrite.html" rel="nofollow noreferrer">website</a> describing imwrite:</p>
  <blockquote>
  <p><code>imwrite(___,Name,Value)</code> specifies additional parameters for output GIF, HDF, JPEG, PBM, PGM, PNG, PPM, and TIFF files, using one or more name-value pair arguments. You can specify Name,Value after the input arguments in any of the previous syntaxes.</p>
  </blockquote>
  <p>Here are some <a href="https://www.mathworks.com/help/matlab/ref/imwrite.html#namevaluepairarguments" rel="nofollow noreferrer">Name,Value pairs arguments</a> that you can use for <code>imwrite</code> compression:</p>
  <ul>
  <li>'Compression' — Compression scheme</li>
  <li>'Quality' — Quality of JPEG-compressed file</li>
  </ul>
  <p>So an example of imwrite doing the compression is: <code>imwrite(A,'C:\project\lena.jpg','jpeg','Quality',Q)</code> where <strong>Q</strong> is the quality factor.</p>
  <p>I'm new to matlab. I read that matlab's <code>imwrite</code> is able to do JPEG compression to a specific quality factor (as described above). Hence, I am curious in what is the difference (is there any?) in doing JPEG compression using <code>imwrite</code> as compared to <code>dct2</code> when all other factors are fixed as much as possible (e.g, same quality factor, same input image source)? Ignoring any difference in computational cost, just focusing on the resulting jpeg image.</p>
  <h3>Update:</h3>
  <p>I found a <a href="http://dde.binghamton.edu/vholub/pdf/EURASIP14_Universal_Distortion_Function_for_Steganography_in_an_Arbitrary_Domain.pdf" rel="nofollow noreferrer">paper</a> detailing the differences in using these two techniques in <strong>page 4</strong>, section <strong>3.3.1 Technical issues with zero embedding costs</strong>. Here is a snippet taken from the paper:</p>
  <blockquote>
  <p>When using the slow DCT (implemented using ‘dct2’ in Matlab), the number 1/2-coefficients is small and does not affect security at least for low-quality factors. However, in the fast-integer implementation of DCT (e.g., Matlab’s imwrite), all Dij are multiples of 1/8. Thus, with decreasing quantization step (increasing JPEG quality factor), the number of 1/2-coefficients increases.</p>
  </blockquote>
  <p>The &quot;1/2 coefficients&quot; above refers to the unquantized DCT coefficients having a &quot;0.5&quot; decimal point. It is also known as the &quot;rounding error&quot;. Based on this, it seems like there is indeed a difference in the resulting unquantized DCT coefficients for different quality factors.</p>