Matlab: How to avoid artefacts in filled contour plots - matlab

I am trying to export filled contour plots from Matlab as vector graphics to include in a Latex file. My current methodology is:
contourf(x,y,v_mag,20), axis([0,width,0,height]),daspect('manual') ;
grid off
colormap jet
h = colorbar;
caxis([0 v_lid])
h.Label.String = 'Velocity Magnitude (m/s)';
set(gcf,'renderer','painters')
export_fig('-painters', '-transparent', 'pdf', 'filename.pdf');
The problem with this method is that it produces artefacts (the white lines) which look like the following:
I understand that these white lines are the polygons defining the shaded areas which have invisible edges, and don't quite overlap (according to here). The problem is caused by the pdf viewer itself which tries to smooth the lines displayed on the screen (according to here). My problem is that most people viewing the document will not know this and will not know how to prevent the viewer doing this. So my questions is:
Is it possible to create a vector graphic of a filled contour plot from Matlab without these artefacts?
Eps produces the same problems. I have tried to use the SVG function but have not had any luck. I am trying to avoid using raster graphics due to the pixelation caused by zooming in. Any advice would be much appreciated.
EDIT - Additional info - Using Matlab v.2014b and Ghostscript v.9.15

This is an extremely frustrating issue for which there seems to be no solution (or even, few attempts at a solution), and it has been many years now. In summary, Matlab cannot cope with outputting artefact-free contour or surface plots (anything with complicated meshes or transparencies).
I can suggest a simple workaround that will work in most cases, where the colours or details of the underlying contour plot do not need to be preserved perfectly.
Output a version of the figure without lines in png format with high enough resolution.
Output a version of the figure without colours in pdf format. This should be free of any artefacts. If your figure it complicated and has many transparencies, you may need to output multiple versions building up the 'levels'.
Use Adobe Illustrator (or some equivalent) to perform a vectorized trace of the raster image. You may lose some detail here, but for simple contour plots with limited details, it will be converted easily to vectorized form.
Overlay the two images within Illustrator. Output in vector format.
This also allows you to use things like Illustrator's ability to compress pdfs.
If you don't want to toy with vectorizing the raster output, you can also simply replace steps 3-4 and combine a raster colour image with a vectorized line image. This would work well for complicated contour plots giving you crisp lines, but the ability to compress the underlying colours.

Eventually, MatLab 2013b doesn't have this problem. Furthermore the files it produces has much less volume. That is because MatLab 2013b composes vectorized image of big overlapping figures, while MatLab 2014b makes that awful meshing.
Here the first file was got with 2013b and the second with MatLab 2014b (I highlighted one of the polygons with red stroke to show the difference). The volumes differ in approximately 22 times (38 Kb vs. 844 Kb).
So it is not the viewer problem, it's how the image is exported from MatLab.
The issue is also discussed here Triangular split patches with painters renderer in MATLAB 2014b and above, but still no direct solution.

Related

Matlab, high quality plot, two legends

I would like to produce a plot with two y-axis and two legends, looking something like this:
I have modified a code I found online to produce a high-quality plot to use in reports/papers. I was wondering how you add a second y-axis in such a code? I have attached the matrix and code to produce the high-quality plot: https://drive.google.com/file/d/1aKZLFeoO1wmQ1P2tEiiucvOFI7PehkGL/view?usp=sharing
https://drive.google.com/file/d/1aKZLFeoO1wmQ1P2tEiiucvOFI7PehkGL/view?usp=sharing
NOTE: This is basically a comment, however, it grew too long, and I felt it was too important not to mention properly.
Reporting plots from Matlab should always, unless you have some very good excuse, be done using vector graphics, i.e. pdf, ps, eps or similar format. The reason for this is the quality, e.g. here I have taken your high-quality and the similar pdf-version and zoomed in.
The png version has artifacts. The reason for this is that the png (similar for jpg and more) is that the picture is saved using pixels, thus when you zoom the quality deteriorate.
The pdf version, which is made with vector graphics, save the vectors, thus when I zoom the pdf viewer can regenerate the pixels and maintain the same quality. As an added bonus, the vector-graphic version is typically smaller in size.
This is made in Matlab using
saveas(gcf,'myfigure.pdf')
Use yyaxis to add another plot with an axis. Take a look at the following modified portion of your code.
yyaxis left
plot(N(:,1),N(:,3)/(27.5*2),'b-','DisplayName','Location','LineWidth',lw); %<- Specify plot properites
yyaxis right
plot(N(:,1),N(:,4)/(27.5*2),'r-', 'DisplayName','NorthEast','LineWidth',lw); %<- Specify plot properites
legend('Location', 'NorthEast');

How to change efficiently Colormap of big .fig files in Matlab?

I have some 2 GB .fig files where I would like to change colormaps smartly.
The initial colormap is colormap(1-gray(1024)); made initially for computers.
I would like to change smoothly to Parula etc for visualization purposes.
There is a need for changing to many different colormaps efficiently.
It may be the case that the original gray is not the optimum for the starting point.
My main interest is the time-series analysis with Mathematica where I need to find some colormap which I can use with meshgrid data structure.
There are some colormaps presented in the book Passive Acoustic Monitoring of Cetaceans by Walter M.X. Zimmer which seem to be relevant here. Some alternatives to be considered
colormap(1-gray(1024))
colormap(1-gray(12))
colormap(1-gray)
colormap(cmap)
colormap(1-gray(7*2))
colormap(1-gray(8*2))
The situation is that changing from one colormap to another is too slow with big .fig files. Little (or no?) history is taken into account when changing the colormap, I think. The previous change of a colormap does not decrease the execution time of the next colormap; although you would change subsequently back to the initial colormap. The biggest problem is with colormaps that are not injective with each other.
Questions
Why do they take everything except clauses (1-gray)?
How can you change smoothly colormap of big .fig files in Matlab? There are similarities between some colormaps. Sometimes, the default way etc colormap(parula(200)) is too slow. I would like to speed up things if similarities between colormaps could be used; by configuring the initial colormap suitable for some changes of colormaps.
How can you decide a colormap such that it is usable for the time-series analysis in Mathematica? Just an example, please.
Use Mathematica 11 instead, since its default colormap has much better contrast and its viewer fits much better for your dynamic target.

How to obtain the difference between 2 image in Matlab?

I have 2 scatter plots obtained from an experiment. The images look very similar on a naked eye. I would like to obtain the difference between these 2 images. The 2 images have :
Same Background
Line markers are yellow and blue.
I am not an expert with image processing tools in Matlab. What would be the right approach to highlight the differences in the 2 scatter plots ?
Do we need to plot the scatter plots using the same linemarkers in order to obtain the difference ?
Thanks
You could use the command imshowpair(img1,img2) to compare between images, more help can be found at Mathworks Compare differences between images section.
Simple, in openCV, I would use absDiff which will highlight the difference very well. MatLab too have this function, while I have never used the MatLab's version before, it shouldn't differ too much from the OpenCV version.
Here's the MatLab equivalent: imabsdiff
An example showing how to utilise absdiff for your case: imabsdiff example with code
Do comment if you need anymore help, or if that doesn't solve your problem.

Matlab: showing anomalies in a plot

How can make I tiny sticks like those shown in red surroundings in the plot below? I used a binary vector of anomalies (1 if anomaly 0 if not) and used plot function, but rather I' d like to see spikes like in the figure.
These sticks correspond to anomaly indicators in the data such as discontinuities (as can be seen in the plot). I'd also like if you could propose good alternative visualizations.
Matlab allows drawing rectangles on the plot.

Corner Detection in 2D Vector Data

I am trying to detect corners (x/y coordinates) in 2D scatter vectors of data.
The data is from a laser rangefinder and our current platform uses Matlab (though standalone programs/libs are an option, but the Nav/Control code is on Matlab so it must have an interface).
Corner detection is part of a SLAM algorithm and the corners will serve as the landmarks.
I am also looking to achieve something close to 100Hz in terms of speed if possible (I know its Matlab, but my data set is pretty small.)
Sample Data:
[Blue is the raw data, red is what I need to detect. (This view is effectively top down.)]
[Actual vector data from above shots]
Thus far I've tried many different approaches, some more successful than others.
I've never formally studied machine vision of any kind.
My first approach was a homebrew least squares line fitter, that would split lines in half resurivly until they met some r^2 value and then try to merge ones with similar slope/intercepts. It would then calculate the intersections of these lines. It wasn't very good, but did work around 70% of the time with decent accuracy, though it had some bad issues with missing certain features completely.
My current approach uses the clusterdata function to segment my data based on mahalanobis distance, and then does basically the same thing (least squares line fitting / merging). It works ok, but I'm assuming there are better methods.
[Source Code to Current Method] [cnrs, dat, ~, ~] = CornerDetect(data, 4, 1) using the above data will produce the locations I am getting.
I do not need to write this from scratch, it just seemed like most of the higher-class methods are meant for 2D images or 3D point clouds, not 2D scatter data. I've read a lot about Hough transforms and all sorts of data clustering methods (k-Means etc). I also tried a few canned line detectors without much success. I tried to play around with Line Segment Detector but it needs a greyscale image as an input and I figured it would be prohibitivly slow to convert my vector into a full 2D image to feed it into something like LSD.
Any help is greatly appreciated!
I'd approach it as a problem of finding extrema of curvature that are stable at multiple scales - and the split-and-merge method you have tried with lines hints at that.
You could use harris corner detector for detecting corners.