Image with Contours Overlayed
http://www.idlcoyote.com/gallery/image_plot_with_contours.pn g
More problems/bugs/not-intuitive-things with this one that I could not solve. My two attempts are below.
PRO Image_With_Contours_Overlayed_FG, WINDOW=aWindow
; Example Gaussian data.
img = cgDemoData(26)
; Set up variables for the contour plot. Normally, these values
; would be passed into the program as positional and keyword parameters.
minValue = Floor(Min(image))
maxValue = Ceil(Max(image))
nLevels = 10
xtitle = 'X Axis'
ytitle = 'Y Axis'
position = [0.125, 0.125, 0.9, 0.800]
cbposition = [0.125, 0.865, 0.9, 0.895]
cbTitle = 'Data Value'
;Contour levels
contourLevels = cgConLevels(img, NLevels=10, MinValue=minValue)
; Set up colors for contour plot.
cgLoadCT, 33, CLIP=[30,255]
tvLCT, palette, /Get
;APPROACH 1: Plot the image directly (does not work)
; The data coordinates are tied to the dimensions of the image. Setting [XY]Range
; will select a subwindow of the image (a 10x200 strip). Setting Image_Location and
; Image_Dimensions allows you to squeeze the entire image into the thin strip. There
; is no natural way to then set the size of the image to something normal without
; clicking on the image and stretching it.
fgImage1 = Image(img, XRange=[10,20], YRange=[-100,100], RGB_Table=palette, $
Position=position, Axis_Style=1, Image_Location=[10,-100], $
Image_Dimensions=[10,200])
; Open a window and return its reference to the user.
aWindow = Window(WINDOW_TITLE="Image with Contours Overlayed")
;APPROACH 2: Paste the image into a set of axes
; Because the axes are fixed to the image's dimensions, setting the Overplot keyword
; results in the same behavior as described above. Here, I will play with the Image's
; aspect ratio to fit it inside the axes.
fgPlot = Plot([10,20], [-100,100], /Current, /NoData, XStyle=1, YStyle=1, $
Xtitle=xtitle, YTitle=ytitle, Position=position)
;Must set the aspect ratio in order to fill out the axes, otherwise
coords = fgPlot -> ConvertCoord(position[[0,2]], position[[1,3]], /Normal, /To_Device)
aspect = double(coords[1,1]-coords[1,0]) / double(coords[0,1]-coords[0,0])
;Create the image
; - The image does not stay fitted to the axes if the window is resized.
; The Aspect_Ratio would have to be updated each time.
fgImage = Image(img, Aspect_Ratio=aspect, Position=position, /Current, RGB_Table=palette)
;Bring the plot forward so that the tickmarks are on top.
fgPlot -> Order, /Bring_Forward
;Create the contours
; - Labels are still up-side-down. See the C_Label_Objects and C_Use_Label_Orientation
fgContour = Contour(img, C_Value=contourLevels, C_Color='Dark Grey', Overplot=fgPlot, $
C_Label_Show=1)
;Create the colorbar
; - Generates error: "% Attempt to call undefined method: 'IDLITSYMBOL::GETVISUALIZATIONS'."
; but still creates the colorbar
; - fgCB is undefined after the call
fgCB = Colorbar(Target=fgImage, Position=cbPosition, TextPos=1, Title='Data Value')
END ;*********************************************************** ******
; This main program shows how to call the program and produce
; various types of output.
; Display the plot in a resizeable graphics window.
Image_With_Contours_Overlayed_FG, Window=window
; Create a PostScript file. Linestyles are not preserved in IDL 8.2.3 due
; to a bug. Only encapsulated PostScript files can be created.
window.save, 'image_with_contours_overlayed_fg.eps'
; Create a PNG file with a width of 600 pixels. Resolution of this
; PNG file is not very good.
window.save, 'image_with_contours_overlayed_fg.png', WIDTH=600
; For better resolution PNG files, make the PNG full-size, then resize it
; with ImageMagick. Requires ImageMagick to be installed.
window.save, 'image_with_contours_overlayed_fg_fullsize.png'
Spawn, 'convert image_with_contours_overlayed_fg_fullsize.png -resize 600 image_with_contours_overlayed_fg_resized.png'
END
|