青未了分享 http://blog.sciencenet.cn/u/yanghang

博文

[转载]IDL对象图形法

已有 1748 次阅读 2019-2-28 11:24 |个人分类:IDL学习笔记|系统分类:科研笔记|文章来源:转载

对象图形法更适合开发应用程序。

一、基本操作

1.类名解析

IDL中对象类的名字格式是IDLxxYyyy,其中,xx有下面几种:gr(grahpics objects),表示图形对象类;db(database objects)表示数据库对象类;an(analysis)表示分析类。Yyyy是类名,如Axis表示坐标轴;Surface表示曲面。

其他还有:

File Format Object Classes

Miscellaneous Object Classes

Network Object Classes

2.基本操作

2.1创建对象

用OBJ_NEW函数可以基于对象类创建一个新的对象,调用格式:

Result = OBJ_NEW( [ObjectClassName [, Arg1...Argn]] )

例:object=OBJ_NEW("IDLgrModel")

自IDL8.0起,可以通过对象类名函数方式创建,格式为:

object=objclassnew( [ Arg1...Argn]] )

例:object=IDLgrModel()

对象一旦创建成功就分配了内存,直到对象销毁才释放内存。

2.2对象调用方法

对象创建成功后,通过对象方法调用来获得设置对象的属性。对象方法的调用分为过程调用和函数调用两种。初基础容器类IDL_Container外,所有对象都具备四种基本方法:init,cleanup,setproperty和getproperty。

调用过程为:object.procedurename, argument[, optional_arguments]

其中,object是一个对象实例;“.”是对象调用符号,也可以用“->”;procedurename是过程名;argument是函数输入参数;optional_arguments是调用该函数时的可选参数。

2.3对象属性修改

对象具备一定的属性,如线对象(IDLgrPolyline)具有颜色、线型和线宽度等属性。对象初始化时,可以设置属性;初始化后,可通过调用SetProperty方法修改属性:

OBJ= OBJ_NEW( [ObjectClassName , Property = value, ...)

OBJ.SetProperty, Property = value, ...

其中Property 是对象的属性;value是对象属性变量值。属性值能够通过GetProperty方法获得,调用格式:

OBJ.GetProperty, Property = value, ...

示例:

x = (FINDGEN(21) / 10.0 - 1.0) * 10.0

y = [3.0, -2.0, 0.5, 4.5, 3.0, 9.5, 9.0, 4.0, 1.0, -8.0, $

    -6.5, -7.0, -2.0, 5.0, -1.0, -2.0, -6.0, 3.0, 5.5, 2.5, -3.0]

myplot1 = OBJ_NEW('IDLgrPlot', x, y, COLOR=[120, 120, 120])

属性修改:

myplot1.SetProperty,COLOR=[250, 0, 0]

此外还可以通过Widget_PropertySheet进行交互属性修改。

2.4对象销毁

OBJ_DESTROY

2.5对象比较

通过EQ或NE等逻辑运算符可以判断两个对象是否相同。

2.6相关函数

2.6.1OBJ_CLASS:可获取对象的类名或继承类名

Syntax

Result = OBJ_CLASS( [Arg] [, COUNT=variable] [, /SUPERCLASS{must specify Arg}] )

2.6.2OBJ_ISA:可判断当前对象是否是类名或继承基类的对象,若是则返回1,若不是则返回0。

2.6.3OBJ_VALID:可以判断变量是否为有效对象。

二、显示图形图像

1.框架体系

\"111111.bmp\"/

例:

mywindow = OBJ_NEW('IDLgrWindow', COLOR_MODEL=1)

myview = OBJ_NEW('IDLgrView',    VIEWPLANE_RECT=[0,-4,10,8], COLOR=255)

mymodel = OBJ_NEW('IDLgrModel')

myplot = OBJ_NEW('IDLgrPlot', RANDOMN(seed, 10), COLOR=0,    THICK=3)

; Organize the object hierarchy:

myview->Add, mymodel

mymodel->Add, myplot

; Draw to the window:

mywindow->Draw, myview

; Next, use the window object’s Read method to create

; an image object with the rendered scene as its image data:

myimage = mywindow->Read()

; Retrieve the image data using the GetProperty method

; of the image object:

myimage->GetProperty, DATA=image

; Write the image to a TIFF file named myfile.tif:

WRITE_TIFF, 'myfile.tif', image

2.基础框架类

2.1 IDLgrWindow

该对象类初始化后是一个显示窗口,常用关键字属性参数有:DIMENSIONS、LOCATION、RENDERER、RETAIN、title、等,常用的方法有Draw和read

  • Draw方法可以渲染显示IDLgrScene、IDLgrView等对象

       第一种方式:直接渲染显示oView对象:mywindow->Draw, myview

       第二种方式:先将myview对象设置为mywindow的一个属性

                           mywindow->SetProperty, GRAPHICS_TREE=myview

  • read方法可以获取IDLgrWindow的显示内容,返回IDLgrImage对象,示例代码如下:

            myimage = mywindow->Read()

            myimage->GetProperty, DATA=image

2.2 IDLgrView

该对象类用来显示各种图像原子对象。常用关键字有:DIMENSIONS、LOCATION、PROJECTION、EYE、VIEWPLANE_RECT、TRANSPARENT、ZCLIP等。

对象层次体系结构:

IDLgrWindow->IDLgrView->IDLgrModel->IDLgrImage

示例程序:(Routines (alphabetical) > Object Classes > Graphics Object Classes > IDLgrView)

1.     Determine the path to the nyny.dat file:

file = FILEPATH('nyny.dat', $

   SUBDIRECTORY = ['examples', 'data'])

2.     Initialize the image size parameter:

imageSize = [768, 512]

3.     Import the image from the file:

image = READ_BINARY(file, DATA_DIMS = imageSize)

4.     Resize this large image to entirely display it on the screen:

imageSize = [256, 256]

image = CONGRID(image, imageSize[0], imageSize[1])

5.     Initialize the display objects:

oWindow = OBJ_NEW('IDLgrWindow', RETAIN = 2, $

   DIMENSIONS = imageSize, $

   TITLE = 'A Grayscale Image')

oView = OBJ_NEW('IDLgrView', $

   VIEWPLANE_RECT = [0., 0., imageSize])

oModel = OBJ_NEW('IDLgrModel')

6.     Initialize the image object:

oImage = OBJ_NEW('IDLgrImage', image, /GREYSCALE)

7.     Add the image object to the model, which is added to the view, then display the view in the window:

oModel -> Add, oImage

oView -> Add, oModel

oWindow -> Draw, oView

The following figure shows the resulting grayscale image display of New York in Object Graphics.

8.     Initialize another window:

oWindow = OBJ_NEW('IDLgrWindow', RETAIN = 2, $

DIMENSIONS = imageSize, TITLE = 'Panning Enlarged Image')

9.     Change the view to zoom into the lower left quarter of the image:

viewplane = [0., 0., imageSize/2]

oView -> SetProperty, $

VIEWPLANE_RECT = [0., 0., imageSize/2]

The view object still contains the entire image object, but the region displayed by the view (the viewplane rectangle) is reduced in size by half in both directions. Since the window object remains the same size, the view region is enlarged to fit it to the window.

10.   Display the updated view in the new window:

oWindow -> Draw, oView

The following figure shows the resulting enlarged image area of New York in Object Graphics.

11.   Pan the view from the left side of the image to the right side of the image:

FOR i = 0, ((imageSize[0]/2) - 1) DO BEGIN & $

viewplane = viewplane + [1., 0., 0., 0.] & $

oView -> SetProperty, VIEWPLANE_RECT = viewplane & $

oWindow -> Draw, oView & $

ENDFOR

Note: The & after BEGIN and the $ allow you to use the FOR/DO loop at the IDL command line. These & and $ symbols are not required when the FOR/DO loop in placed in an IDL program as shown in Panning_Object.pro in the examples/doc/objects subdirectory of the IDL installation directory.

The following figure shows the resulting enlarged image area of New York panned to the right side in Object Graphics.

12.   Clean up the object references. When working with objects always remember to clean up any object references with the OBJ_DESTROY routine. Since the view contains all the other objects, except for the window (which is destroyed by the user), you only need to use OBJ_DESTROY on the view object.

OBJ_DESTROY, oView

2.3 IDLgrScene

场景对象类,该对象使用时可以包含多个view对象。

IDLgrWindow->IDLgrScene->IDLgrView

                                            ╲>IDLgrView


2.4 IDLgrModel


3. 二维图形类

3.1线对象

mywindow = OBJ_NEW('IDLgrWindow', dimension=[400, 400])

myview = OBJ_NEW('IDLgrView')

mymodel = OBJ_NEW('IDLgrModel')

myview->Add, mymodel

x=[-.5,.5]

y=[-.5,.5]

myPolyline = OBJ_NEW('IDLgrPolyline',x,y)

mymodel->add, myPolyline

;显示

mywindow.draw, myview

;改变线条颜色

myPolyline.SetProperty, color=[255,0,0]

mywindow.draw, myview

;修改线粗为5

myPolyline.SetProperty, thick=5

mywindow.draw, myview

;创建折线段

data=fltarr(2,4)

data[0,*]=[-.5,-.5,.5,.5]

data[1,*]=[-.5,.5,.5,-.5]

myPolyline.SetProperty, data=data

mywindow.draw, myview

;交叉图形连接关系

Polylines=[4,0,1,3,2]

myPolyline.SetProperty, Polylines=Polylines

mywindow.draw, myview

;正方形连接关系

Polylines=[5,0,1,2,3,0]

myPolyline.SetProperty, Polylines=Polylines

mywindow.draw, myview

;X形连接关系

Polylines=[2,0,2,2,1,3]

myPolyline.SetProperty, Polylines=Polylines

mywindow.draw, myview

;X形连接关系

Polylines=[5,0,1,2,3,0,2,0,2,2,1,3]

myPolyline.SetProperty, Polylines=Polylines

mywindow.draw, myview



3.2多边形对象

mywindow = OBJ_NEW('IDLgrWindow', dimension=[400, 400])

myview = OBJ_NEW('IDLgrView')

mymodel = OBJ_NEW('IDLgrModel')

myview->Add, mymodel

myPolygon = OBJ_NEW('IDLgrPolygon')

mymodel->add, myPolygon

;创建正方形

data=fltarr(2,4)

data[0,*]=[-.5,-.5,.5,.5]

data[1,*]=[-.5,.5,.5,-.5]

myPolygon.SetProperty, color=[255,0,0],data=data

mywindow.draw, myview

;创建样式对象

mypattern=OBJ_NEW('IDLgrPattern',1)

myPolygon.SetProperty, FILL_PATTERN=mypattern

mywindow.draw, myview

3.3文字对象

文字对象(IDLgrText)能够在特定位置、特定平面上显示特定大小、粗心和走向的文字,主要关键字如下:

ALIGNMENTALL
ALPHA_CHANNELBASELINE
CHAR_DIMENSIONSCLIP_PLANES
COLORDEPTH_TEST_DISABLE
DEPTH_TEST_FUNCTIONDEPTH_WRITE_DISABLE
DRAW_CURSORENABLE_FORMATTING
FILL_BACKGROUNDFILL_COLOR
FONTHIDE
KERNINGLOCATIONS
ONGLASSPALETTE
PARENTRECOMPUTE_DIMENSIONS
REGISTER_PROPERTIESRENDER_METHOD
SELECTION_LENGTHSELECTION_START
SHADERSTRINGS
UPDIRVERTICAL_ALIGNMENT
XCOORD_CONVXRANGE
YCOORD_CONVYRANGE
ZCOORD_CONVZRANGE

与其相关的字体对象IDLgrText等。



3.4坐标轴对象


3.5图形对象

利用图像对象IDLgrImage可以用来创建、显示图像对象。

Annotating Indexed Image Objects

When using Object Graphics, the original color table does not need to be modified. The color table (palette) pertains only to the image object, not the window, view, model, polygon, or text objects. Color annotations are usually applied to label each color level within the image or to allow color comparisons. This section shows how to label each color level on an indexed image in Object Graphics. As an example, an image of average world temperature is imported from the worldtmp.png file. This file does not contain a color table associated with this image, so a pre-defined color table will be applied. This table provides the colors for the polygons and text used to make a colorbar for this image. Each polygon uses the color of each level in the table. The text represents the average temperature (in Celsius) of each level.

See applycolorbar_indexed_object.pro in the examples/doc/objects subdirectory of the IDL installation directory for code that duplicates this example. Run the example procedure by entering applycolorbar_indexed_objectat the IDL command prompt or view the file in an IDL Editor window by entering .EDIT applycolorbar_indexed_object.pro.

S1.Determine the path to the worldtmp.png file:

worldtmpFile = FILEPATH('worldtmp.png', $

   SUBDIRECTORY = ['examples', 'demo', 'demodata'])

S2.Import the image from the worldtmp.png file into IDL:

worldtmpImage = READ_PNG(worldtmpFile)

S3.Determine the size of the imported image:

worldtmpSize = SIZE(worldtmpImage, /DIMENSIONS)

S4.Initialize the display objects necessary for an Object Graphics display:

oWindow = OBJ_NEW('IDLgrWindow', RETAIN = 2, $

   DIMENSIONS = [worldtmpSize[0], worldtmpSize[1]], $

   TITLE = 'Average World Temperature (in Celsius)')

oView = OBJ_NEW('IDLgrView', $

   VIEWPLANE_RECT = [0, 0, worldtmpSize[0], $

   worldtmpSize[1]])

oModel = OBJ_NEW('IDLgrModel')

S5.Initialize the palette object, load the Rainbow18 color table into the palette, and then apply the palette to an image object:

oPalette = OBJ_NEW('IDLgrPalette')

oPalette -> LoadCT, 38

oImage = OBJ_NEW('IDLgrImage', worldtmpImage, $

   PALETTE = oPalette)

S6.Add the image to the model, then add the model to the view, and finally draw the view in the window:

oModel -> Add, oImage

oView -> Add, oModel

oWindow -> Draw, oView

The following figure is displayed. It shows a temperature image and Rainbow18 color table (Object Graphics).

Before applying the color polygons and text of each level, you must first initialize their color values and their locations. The Rainbow18 color table has only 18 different color levels, but still has 256 elements. You can use the INDGEN routine to make an array of 18 elements ranging from 0 to 17 in value, where each element contains the index of that element. Then you can use the BYTSCL routine to scale these values to range from 0 to 255. The resulting array contains the initial color value (from 0 to 255) of the associated range (from 0 to 17, equalling 18 elements).

S7.Initialize the color level parameter:

fillColor = BYTSCL(INDGEN(18))

S8.Initialize the average temperature of each level, which directly depends on the initial color value of each range. Temperature is linearly scaled to range from -60 to 40 Celsius. You can convert the resulting temperature value to a string variable to be used as text:

temperature = STRTRIM(FIX(((20.*fillColor)/51.) - 60), 2)

Note: When the fillColor variable in the previous statement is multiplied by the floating-point value of 20 (denoted by the decimal after the number), the elements of the array are converted from byte values to floating-point values. These elements are then converted to integer values with the FIX routine so the decimal part will not be displayed. The STRTRIM routine converts the integer values to string values to be displayed as text. The second argument to STRTRIM is set to 2 to note the leading and trailing black values should be trimmed away when the integer values are converted to string values.

With the polygon color and text now defined, you can determine their locations. You can use a polygon object to draw each polygon and text objects to display each element of text. The process is repetitive from level to level, so a FOR/DO loop is used to display the entire colorbar. Since each polygon and text is drawn individually within the loop, you only need to determine the location of a single polygon and an array of offsets for each step in the loop. The following two steps describe this process.

S9.Initialize the polygon and the text location parameters. Each polygon is 35 pixels in width and 18 pixels in height. The offset will move the y-location 18 pixels every time a new polygon is displayed:

x = [5., 40., 40., 5., 5.]

y = [5., 5., 23., 23., 5.] + 5.

offset = 18.*FINDGEN(19) + 5.

S10.Initialize the polygon and text objects:

oPolygon = OBJARR(18)

oText = OBJARR(18)

FOR i = 0, (N_ELEMENTS(oPolygon) - 1) DO BEGIN & $

   oPolygon[i] = OBJ_NEW('IDLgrPolygon', x, $

   y + offset[i], COLOR = fillColor[i], $

   PALETTE = oPalette) & $

   oText[i] = OBJ_NEW('IDLgrText', temperature[i], $

   LOCATIONS = [x[0] + 3., y[0] + offset[i] + 3.], $

   COLOR = 255*(fillColor[i] LT 255), $

   PALETTE = oPalette) & $

ENDFOR

Note: The & after BEGIN and the $ allow you to use the FOR/DO loop at the IDL command line. These & and $ symbols are not required when the FOR/DO loop in placed in an IDL program as shown in ApplyColorbar_Indexed_Object.pro in the examples/doc/objects subdirectory of the IDL installation.

S11.Add the polygons and text to the model, then add the model to the view, and finally redraw the view in the window:

oModel -> Add, oPolygon

oModel -> Add, oText

oWindow -> Draw, oView

The following figure displays a temperate image with the colorbar annotation applied to the image. (Object Graphics)

S12.Clean up object references. When working with objects always remember to clean up any object references with the OBJ_DESTROY routine. Since the view contains all the other objects, except for the window (which is destroyed by the user), you only need to use OBJ_DESTROY on the view and the palette objects:

OBJ_DESTROY, [oView, oPalette]


4.颜色显示

对象图形法中的显示颜色分为索引颜色(index)和真彩色颜色(RGB)。对象图形法中使用索引颜色需要在IDLgrImage对象中设置pallette参数为IDLgrPalette对象,即颜色表是通过调用IDLgrPalette对象来实现的。

See displayindexedimage_object.pro in the examples/doc/objects subdirectory of the IDL installation directory for code that duplicates this example.

See highlightfeatures_object.pro in the examples/doc/objects subdirectory of the IDL installation directory for code that duplicates this example

5.坐标系


6.三维显示




三、对象交互


四、对象类列表

IDL中对象类分为绘图设备对象类,基础框架对象类,可视化对象类,文件对象类,几何分析对象类,网络对象类和其他类。

IDLgrAxis

IDLgrBuffer

IDLgrClipboard

IDLgrColorbar

IDLgrContour

IDLgrFilterChain

IDLgrFont

IDLgrImage

IDLgrLegend

IDLgrLight

IDLgrModel:显示的框架对象

IDLgrPalette

IDLgrPattern

IDLgrPDF

IDLgrPlot

IDLgrPolygon

IDLgrPolyline

IDLgrPrinter

IDLgrROI

IDLgrROIGroup

IDLgrScene:场景对象

IDLgrShader

IDLgrShaderBytscl

IDLgrShaderConvol3

IDLgrSurface

IDLgrSymbol

IDLgrTessellator

IDLgrText:文本对象

IDLgrTextEdit

IDLgrView:视图对象

IDLgrViewgroup

IDLgrVolume

IDLgrVRML

IDLgrWindow



五、自定义对象类


六、源码参考





http://blog.sciencenet.cn/blog-346157-1164711.html

上一篇:[转载]瓦片地图
下一篇:ENVI二次开发新功能

0

该博文允许注册用户评论 请点击登录 评论 (0 个评论)

数据加载中...

Archiver|手机版|科学网 ( 京ICP备07017567号-12 )

GMT+8, 2021-1-27 09:12

Powered by ScienceNet.cn

Copyright © 2007- 中国科学报社

返回顶部