MATLAB Answers


Why does "contourf" run slowly when producing a contour plot of image data in MATLAB R2014b and R2015a?

Why does "contourf" run slowly when producing a contour plot of image data in MATLAB R2014b and R2015a? When the provided script ('example_contourf.m') that produces the contour plot of example 960x1280 image data ('f.jpg') is run on MATLAB R2014a and previous releases (i.e. releases with the previous graphics system), the script runs quickly and displays the plot without any significant delay. However, the same script runs slowly when run using MATLAB R2014b and higher. Profiling the script shows that a small amount of time (0.054 seconds) is required to run "contourf" but at least 5-10 seconds are required for the contour plot to be displayed.
Additionally, the script uses the "ginput" function to have the user select two points to define the bottom-left and top-right points of a rectangular sub-image. The mouse motion lags when the script is run in MATLAB R2014b and higher but not on previous releases.





1 Answer

Answer by MathWorks Support Team on 1 Feb 2017
 Accepted Answer

This issue appears to be related to the new graphics system (HG2) introduced in MATLAB R2014b. The "contourf" function takes longer to render contour plots than it did in the old graphics system of previous releases.
A workaround for this issue is to instead use the "imagesc" function to display the image. This renders the image data very quickly and handles "ginput" events and other user interaction much better than a contour plot does. Additionally, for applications where some form of real-world scaling matters, the "imagesc" function provides properties 'XData' and 'YData' that allow the user to position the image in data coordinates to match the desired scaling.
The provided workaround script ('example_imagesc.m') performs the same task as the other provided script, except it uses "imagesc" instead of "contourf" to display the image data. Running the two scripts clearly shows that the "imagesc" function is much faster for displaying image data in this manner.


Sign in to comment.