× {{alert.msg}} Never ask again
Get notified about new tutorials RECEIVE NEW TUTORIALS

Increase image contrast using look up table in MATLAB

Ray Phan
Feb 02, 2015
<p>Judging from your comments, you simply want a <strong>linear</strong> map where intensities that are <code>&lt; 50</code> get mapped to 0, intensities that are <code>&gt; 205</code> get mapped to 255, and everything else is a linear mapping in between. You can simply do this by:</p> <pre><code>slope = 255 / (205 - 50); % // Generate equation of the line - % // y = mx + b - Solve for m intercept = -50*slope; %// Solve for b --&gt; b = y - m*x, y = 0, x = 50 LUT = uint8(slope*(0:255) + intercept); %// Generate points LUT(1:51) = 0; %// Anything &lt; intensity 50 set to 0 LUT(206:end) = 255; %// Anything &gt; intensity 205 set to 255 </code></pre> <p>The LUT now looks like:</p> <pre><code>plot(0:255, LUT); axis tight; grid; </code></pre> <p><img src="http://i.stack.imgur.com/7rZGm.png" alt="enter image description here"></p> <p>Take note at how I truncated the intensities when they're <code>&lt; 50</code> and <code>&gt; 205</code>. MATLAB starts indexing at index 1, and so we need to offset the intensities by 1 so that they correctly map to pixel intensities which start at 0.</p> <p>To finally apply this to your image, all you have to do is:</p> <pre><code>out = LUT(img + 1); </code></pre> <p>This is assuming that <code>img</code> is your input image. Again, take note that we had to offset the input by <code>+1</code> as MATLAB starts indexing at location 1, while intensities start at 0. </p> <hr> <h1>Minor Note</h1> <p>You can easily do this by using <a href="http://www.mathworks.com/help/images/ref/imadjust.html" rel="nofollow"><code>imadjust</code></a>, which basically does this for you under the hood. You call it like so:</p> <pre><code>outAdjust = imadjust(in, [low_in; high_in], [low_out; high_out]); </code></pre> <p><code>low_in</code> and <code>high_in</code> represent the minimum and maximum input intensities that exist in your image. Note that these are normalized between <code>[0,1]</code>. <code>low_out</code> and <code>high_out</code> adjust the intensities of your image so that <code>low_in</code> maps to <code>low_out</code>, <code>high_in</code> maps to <code>high_out</code>, and everything else is contrast stretched in between. For your case, you would do:</p> <pre><code>outAdjust = imadjust(img, [0; 1], [50/255; 205/255]); </code></pre> <p>This should stretch the contrast such that the input intensity <code>50</code> maps to the output intensity <code>0</code> and the input intensity <code>205</code> maps to the output intensity <code>255</code>. Any intensities <code>&lt; 50</code> and <code>&gt; 205</code> get automatically saturated to <code>0</code> and <code>255</code> respectively.</p> <p>This tip was originally posted on <a href="http://stackoverflow.com/questions/25191218/Increase%20image%20contrast%20using%20look%20up%20table%20in%20MATLAB/25191937">Stack Overflow</a>.</p>
comments powered by Disqus