Fanning Software Consulting

Displaying Contour Lines on Images

Facebook Twitter RSS

QUESTION: I am trying to overlay contour lines on top of an image with cgImage and cgContour and even though I have read the documentation for these programs and discovered the Overplot and OnImage keywords, I am having no luck whatsoever. Here is my code.

   cgDisplay
   cgLoadCT, 33, NColors=12
   image = cgdemodata(18)
   cgImage, image, Margin=1.0, /Scale, Top=11
   cgContour, image, NLEVELS=12, /OnImage, Color='white' 

The error I get when I do this comes from cgScaleVector.

   CGSCALEVECTOR: Range max and min are conincidental

If I try the Overplot keyword, rather than the OnImage keyword, I don't get an error message, but I either get an occasional stray line showing up on my image, or the contour lines are in the completely wrong place! What in the world am I doing wrong?

ANSWER: The problem here is that image display commands in IDL (typically TV and TVScl) don't set up a data coordinate system at the time they display an image, the way a Plot or Contour command would.

And, while cgImage is a heck of a lot smarter than a TV command, it is meant to be a drop-in replacement for the TV command, so it doesn't change this aspect of its behavior unless you specifically ask it to. Without a data coordinate system, it is impossible to know what "overplot" or "onimage" mean.

The way to create a data coordinate system with cgImage is to set the Save keyword so that the data coordinate system is saved in IDL system variables. (The Save keyword is set automatically if you add axes to the image plot with the Axes keyword.) Your commands above will work if you simple place the Save keyword at the end of the cgImage command, like this.

   cgDisplay
   cgLoadCT, 33, NColors=12
   image = cgdemodata(18)
   cgImage, image, Margin=1.0, /Scale, Top=11, /Save
   cgContour, image, NLEVELS=12, /OnImage, Color='white' 

Contour lines on top of an image.
Contour lines on shown on top of an image after establishing a data coordinate system.
 

Version of IDL used to prepare this article: IDL 7.1.2.

Written: 20 March 2012