Imagedata 3D visualization instance [mask, warp, normals, lookuptable]

Source: Internet
Author: User
Tags transparent color
I wrote an example of how to display imagedate in 3D for a long time. Now I am going to discuss it, because all the filters are tested by myself while reading the data, so it may not be the best solution. You are welcome to give your comments!

1. Design Ideas: As shown in:
Some filters have many parameter settings, such as vtkpolydatanormals. I have a few comments in the code. You can modify the relevant parameters and test the effect!
In addition, for the convenience of testing, I used data in vtkdata (compilation may change to your own path), but during the specific application, we must use our own data. The imagedata generation method is also attached below:
//---------------------------------------------------------
// Vtkimagedata
// NX ny indicates the number of rows and columns, space indicates the cell size, and ori_x and ori_y indicate the starting coordinates of X and Y, respectively.
Double * Data = new double [NX * ny];
// Input or read data from the file
Vtkdoublearray * scalars = vtkdoublearray: New ();
Scalars-> setarray (data, nx * NY, 1 );
Vtkimagedata * id = vtkimagedata: New ();
ID-> getpointdata ()-> setscalars (scalars );
ID-> setdimensions (NX, NY, 1 );
ID-> setscalartypetodouble ();
ID-> setspacing (Space, space, 1.0 );
ID-> setorigin (ori_x, ori_y, 0.0 );

2. program code
This code is the test program I just discussed with [the cat in the Nile] During vtkimagemask. Later, I did not delete the relevant part. You can also refer to the usage of magemask ~ In addition, we also encountered a problem here. If I want to make an empty effect in this figure (just like the transparent color in the GIF image), how should I do it? I want to use magemask :: setmaskalpha (1), later found that this method is only blocked. The part where the value 1 of mask is not dug out a circle ~ How can we achieve this? I hope you can give me some advice ~

In addition, in this code, I also used imageviewer for two-dimensional display. Of course, if you only want to display two-dimensional images, this is the quickest ~ However, the interactive style here is to use the mouse to change the colorwindow and colorlevel, which is not quite satisfactory. Of course, you can add obersver to the mouse event to block it! After the two-dimensional image is displayed, press 'E' or 'q' to view the three-dimensional image ~ The results are okay ~

# Include "vtkimageellipsoidsource. H "<br/> # include" vtkimagemask. H "<br/> # include" vtkimageviewer. H "<br/> # include" vtkpnmreader. H "<br/> # include <vtkrenderwindowindowinteractor. h> <br/> # include <vtkimage1_eometryfilter. h> <br/> # include <vtkwarpscalar. h> <br/> # include "vtkrenderer. H "<br/> # include" vtkpolydatamapper. H "<br/> # include" vtkpolydatanormals. H "<br/> # include" vtklookuptable. H "<br/> # include <vtkinteractorstyletrackballcamera. h> </P> <p> int main () <br/> {<br/> // image pipeline <br/> vtkpnmreader * reader = vtkpnmreader :: new (); <br/> reader-> releasedataflagoff (); <br/> reader-> setfilename ("F: // VTK // vtkdata/data/Earth. PPM "); </P> <p> // ------------------------------------------------- <br/> // a script to test the mask filter. <br/> // replaces a circle with a color </P> <p> // reset <br/> // mask 1 <br/> vtkimageellipsoidsource * sphere = vtkimageellipsoidsource:: New (); <br/> sphere-> setwholeextent (0,511, 0,255, 0, 0); <br/> sphere-> setcenter (128,128, 0 ); <br/> sphere-> setradius (80, 80, 1); </P> <p> vtkimagemask * mask = vtkimagemask: New (); <br/> mask-> setimageinput (Reader-> getoutput (); <br/> mask-> setmaskinput (sphere-> getoutput ()); <br/> // mask-> setmaskedoutputvalue (100,128,200); <br/> // mask-> setmaskalpha (0.5); <br/> mask-> setmaskalpha (1.0 ); <br/> mask-> notmaskon (); <br/> mask-> releasedataflagoff (); </P> <p> // --------------------------------------------- <br/> // mask 2 <br/> vtkimageellipsoidsource * sphere2 = vtkimageellipsoidsource: New (); <br/> sphere2-> setwholeextent (0,511, 0,255, 0, 0); <br/> sphere2-> setcenter (328,128, 0 ); <br/> sphere2-> setradius (80, 50, 1 ); </P> <p> // test the wrapping of the output masked value <br/> vtkimagemask * mask2 = vtkimagemask: New (); <br/> mask2-> setimageinput (mask-> getoutput (); <br/> mask2-> setmaskinput (sphere2-> getoutput ()); <br/> mask2-> setmaskedoutputvalue (100); <br/> mask2-> notmaskon (); <br/> mask2-> releasedataflagoff (); </P> <p> // --------------------------------------------- <br/> // mask 3 <br/> vtkimageellipsoidsource * sphere3 = vtkimageellipsoidsource: New (); <br/> sphere3-> setwholeextent (0,511, 0,255, 0, 0); <br/> sphere3-> setcenter (228,155, 0 ); <br/> sphere3-> setradius (80, 80, 1 ); </P> <p> // test the wrapping of the output masked value <br/> vtkimagemask * mask3 = vtkimagemask: New (); <br/> mask3-> setimageinput (mask2-> getoutput (); <br/> // mask3-> setimageinput (mask2-> getoutput ()); <br/> mask3-> setmaskinput (sphere3-> getoutput (); <br/> mask3-> setmaskedoutputvalue (255); <br/> mask3-> notmaskon (); <br/> mask3-> setmaskalpha (0.5 ); </P> <p> // --------------------------------------------- <br/> // use vtkimageviewer to show 2D image <br/> vtkimageviewer * viewer = vtkimageviewer: New (); <br/> // viewer-> setinputconnection (mask-> getoutputport (); <br/> viewer-> setinputconnection (mask3-> getoutputport ()); <br/> viewer-> setcolorwindow (255); <br/> viewer-> setcolorlevel (128 ); </P> <p> // viewer debugon <br/> vtkrenderincluwinteractor * iren = vtkrenderincluwinteractor: New (); <br/> viewer-> setupinteractor (iren ); <br/> iren-> initialize (); <br/> iren-> Start (); <br/> // press 'E' or 'q' then change 3D view </P> <p> // ------------------------------------------------- <br/> // imagegeometryfilter, here just use mask 1 <br/> vtkimage1_eometryfilter * geometry = vtkimage1_eometryfilter: New (); <br/> geometry-> setinputconnection (mask-> getoutputport ()); </P> <p> // configure <br/> // warpscalar <br/> vtkwarpscalar * warp = vtkwarpscalar: New (); <br/> warp-> setinputconnection (geometry-> getoutputport (); <br/> warp-> setnormal (0, 0, 1 ); <br/> warp-> usenormalon (); <br/> warp-> setscalefactor (0.2); // This parameter does not affect the color rendering, take zero as a two-dimensional graph </P> <p> // --------------------------------------------------------- <br/> // compute normals to give a better look. <br/> vtkpolydatanormals * normals = vtkpolydatanormals: New (); <br/> Normals-> setinputconnection (warp-> getoutputport ()); <br/> Normals-> setfeatureangle (60); // when the angle and size are 60-degree peak shifting <br/> Normals-> consistencyoff (); <br/> Normals-> splittingoff (); </P> <p> // values <br/> // create a lookup table <br/> vtklookuptable * W1 = vtklookuptable: New (); <br/> W1-> setrange (20, 30); // image intensity range <br/> W1-> setvaluerange (0.0, 1.0 ); // from black to white <br/> W1-> setsaturationrange (0.0, 1.0); // color saturation <br/> W1-> setramptolinear (); <br/> // The above is automatically generated, or you can manually perform the following interpolation <br/> // W1-> setnumberoftablevalues (ncolors ); <br/> // For (INT num = 0; num <pcolors_ren-> get_count (); num ++) <br/> // {<br/> // W1-> settablevalue (Num, R, G, B ); <br/> //} <br/> // W1-> build (); </P> <p> // specify <br/> // terminate of the pipeline <br/> vtkpolydatamapper * mapper = vtkpolydatamapper: New (); <br/> mapper-> setinputconnection (warp-> getoutputport (); <br/> mapper-> setlookuptable (W1 ); </P> <p> // ------------------------------------------------------- <br/> // render <br/> vtkactor * Actor = vtkactor: New (); <br/> actor-> setmapper (Mapper); <br/> vtkrenderer * Ren = vtkrenderer: New (); <br/> Ren-> addactor (actor ); <br/> vtkrenderwindow * renwin = vtkrenderwindow: New (); <br/> renwin-> addrenderer (ren ); </P> <p> // start <br/> vtkrenderwindowindowinteractor * iren2 = vtkrenderwindowindowinteractor: New (); <br/> vtkinteractorstyletrackballcamera * style = style: New (); <br/> iren2-> setinteractorstyle (style); // change interactorstyle <br/> renwin-> setinteractor (iren2); <br/> iren2-> initialize (); <br/> iren2-> Start (); </P> <p> return 0; <br/>}</P> <p>3. The results show from the two pictures above that the black circle was dug and I was cheated. It was actually blocked or replaced with black, not the transparent color we want! You can see the following figure. if you dig a part, you can see the opposite peak... 4. Coming back
The idea in the first part was originally intended to use text. Later I found that my understanding of VTK was limited. I could not say anything, so I used illustrations ~ Here we use VTK designer, casually recommended to everyone these two software, mainly to the novice, you can go to his home to download: http://www.vcreatelogic.com/products/vtkd/ also recommend another, called paraview, can go to http://www.paravie#org/to download.

Literally, the former should be designed and belongs to rad. The latter may be mainly used to visualize VTK data. In fact, I don't use much of these two software. It's only a month for me to install these two software on my computer. It's hard to make too many comments ~ Of course, I will not introduce these two things to everyone, especially new users like me who actually use them for development, in my opinion, sometimes learning programs are really boring, especially for domestic reference such as VTK and there are few materials to learn from, it's okay to play with the GUI software that can greatly increase our interest in VTK, or enhance our understanding of VTK, such as reading the figure I made with VTK designer, the idea of the VTK pipeline is clearly displayed. There are two resource managers on the Right of VTK designer, which can display the Algorith, filter, and function of the basic VTK .. And their corresponding attributes and Methods ~ And the classification is clear and clear! Paraview is also used once. The advantage of paraview is that it can directly read all data formats supported by VTK without programming, or directly add widgets to interactive operations. Let you have a deeper understanding of different data, different filters and widgets .... The benefits are certainly more than that. Let's explore them by yourself. After all, this is more interesting than watching the lengthy code above ~~

Attach three VTK designer instances (the instances of the software are running): paraview:
In the process just now, I felt the power of paraview again, so I couldn't help playing it for a while ~
PS: Finally, I 'd like to ask for help. What filter can be used to empty imagedata ??

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.