X
PrevPrev Go to previous topic
NextNext Go to next topic
Last Post 10 Jan 2017 06:01 PM by  anon
Extracting image values under an ROI or under a polygon
 7 Replies
Sort:
You are not authorized to post a reply.
Author Messages

anon



New Member


Posts:4
New Member


--
10 Jan 2017 06:01 PM
    I have an image and a vector file with polygons. I have used the VectorRecordsToSeparateROI task to generate individual rois. The number of elements in the roi match the number of polygons. I would now like to extract values from the image for each of the rois or polygons. My new roi file is an xml. In ENVI Classic it was very easy to do. ENVI_RESTORE_ROIS, then ENVI_GET_ROI_IDS, then loop through all of the rois extracting the image data using ENVI_GET_ROI_DATA. For my new code I have been trying to use the new functions, but I have not been able to get a roi.PixelCount other than 0 or roi.PixelAddresses other than !NULL. I tried running roi.Pixelate first but that did not change the results. If I print out the rois I get the color, name and n_definitions. So my question is what is the best way to extract data from an image under rois or under individual polygons.

    MariM



    Veteran Member


    Posts:2396
    Veteran Member


    --
    12 Jan 2017 10:51 AM
    At this time, I believe it is still easier to export your XML ROIs to Classic ROIs and use ENVI_GET_ROI_DATA. There currently is not a straight-forward and documented way to extract ROI data with the new ROIs because they are treated as a raster. One way would be to create a subset for each pixel in the ROI using sub_rect, which is fairly cumbersome.

    Deleted User



    New Member


    Posts:4
    New Member


    --
    12 Jan 2017 01:38 PM
    Thanks Mari, The other option I was looking at was to run ROIMaskRaster on each of the rois. Then extract the data. I have not tried this. I did implement the classic method. When I extracted values I still ended up with zeros. I also printed out the number of points in each roi, they were also zero. So I suspect my problem starts before the VectorRecordsToSeparateROI. My original vector has 904 records. I created a new vector that is a subset by record, of the original. The new vector has 113 records. The new vector does have valid polygons that match the corresponding polygon in the original vector file. The only difference I can see is that the new vector has an arbitrary projection instead of the UTM projection of the original. I used OBJ_NEW( 'IDLffShape', test_file, /UPDATE, ENTITY_TYPE=5 ) to create the new vector, but I could not find any way to set the projection. It appears that not having the projection set causes VectorRecordsToSeparateROI to produce rois with zero points. I changed my code to run VectorRecordsToSeparateROI on the original vector and rois that were created had varying numbers of points as I would expect. So, it looks my problem is related to creating a new vector, subsetting it by records and not being able to set the projection information. How would I set the projection on the new vector? Is the method I am using to subset a vector by records the best way, or is there an easier way to subset a vector by records? Below is generally the code I have been using to subset the vector by records. mynewshape = OBJ_NEW( 'IDLffShape', test_file, /UPDATE, ENTITY_TYPE=5 ) mynewshape->AddAttribute, 'CLASS_NAME', 7, 25, PRECISION=0 mynewshape->AddAttribute, 'AREA', 5, 15, PRECISION=8 mynewshape->AddAttribute, 'CLASS_CLRS', 7, 25, PRECISION=0 mynewshape->AddAttribute, 'MEAN', 5, 15, PRECISION=8 mynewshape->AddAttribute, 'MAXIMUM', 5, 15, PRECISION=8 attrNew = mynewshape.GetAttributes( /ATTRIBUTE_STRUCTURE ) index = WHERE( attributes[*].attribute_0 EQ TopClassName, count ) FOR i=0, (count-1) DO BEGIN attname = attributes[ index[i] ].attribute_0 ent = shape->GetEntity( index[i] ) attrNew.attribute_0 = attributes[ index[i] ].attribute_0 attrNew.attribute_1 = attributes[ index[i] ].attribute_1 attrNew.attribute_2 = attributes[ index[i] ].attribute_2 mynewshape->PutEntity, ent mynewshape->SetAttributes, i, attrNew shape->DestroyEntity, ent ENDFOR

    Deleted User



    New Member


    Posts:
    New Member


    --
    13 Jan 2017 03:05 AM
    I solved the problem of the projection simply copying the firstfile.prj file (that contains the projection information for the ESRI Shapefile vector format) of the original shapefile in the folder and with the same name (eg. secondfile.prj) of the producted shapefile. I hope this solution will help you.

    MariM



    Veteran Member


    Posts:2396
    Veteran Member


    --
    13 Jan 2017 07:22 AM
    I agree that your ROIs are not finding points because of the lack of spatial reference. The new XML ROIs are spatially aware unlike the classic ROIs, which are based on image coordinates and dimensions. If you define the projection of the shapefile you are converting, it should work. Shapefile projection information is stored in a .prj file which is just a text file with an Esri projection code. I believe if you have a .prj file for your original shapefile, you can just create another .prj file (duplicate it) with the same projection string. For example, here is the contents of a .prj file for a point file of cities across the US: GEOGCS["GCS_North_American_1983",DATUM["D_North_American_1983",SPHEROID["GRS_1980",6378137,298.257222101]],PRIMEM["Greenwich",0],UNIT["Degree",0.0174532925199433]] So if your new shapefile is called 'subsetVec.shp', you should also have a 'subsetVec.prj' in the same directory.

    Deleted User



    New Member


    Posts:4
    New Member


    --
    13 Jan 2017 11:10 AM
    Thank you Mari and Silvio. Copying the .prj file did solve my problem. Comments to Harris It appears that there needs to be more work put into working with the new XML ROIs. to make it as easy as it is in ENVI Classic. With vectors. It would be good to be able to extract image data from under a polygon without having to make ROIs. As I found here it was not easy to create a new vector that contained a subset of the records in another vector. Although if I could have extracted the image data directly from the original polygon I would not have needed to subset the vector or create ROIs.

    MariM



    Veteran Member


    Posts:2396
    Veteran Member


    --
    13 Jan 2017 01:54 PM
    There is a discussion in the help (that I just ran across) on how to get data from the new ROIs using a mask and the pixel_state that you might find useful in the future: http://www.harrisgeospatial.com/docs/...

    Deleted User



    New Member


    Posts:4
    New Member


    --
    18 Jan 2017 12:46 PM
    I have tried the method from the last example in the faq. I was able to get it to work and extract image data from an roi. I am trying to extract data values and export them to a spectral library. What I have is a roi file that has 113 rois, the image has 1030pixels, 3386lines and 196 bands. The faq method does work but it is very, very slow.
    You are not authorized to post a reply.