Extract rotated rectangle around feature

Share your programming ideas for various measurement and analysis tasks here
Post Reply
user-6033
Posts: 89
Joined: Thu Jan 01, 1970 1:00 am

Extract rotated rectangle around feature

Post by user-6033 »

Hi,

Is it possibe to extract the rotated rectangle from the annotation layer, preferable also with some kind of ID, to link it to the very same feature in the feature table? We would like to calculate distances between feature boundaries very fast and oriented rectangles would be a great help for that as a boundary approximation.

Best regards
Fredrik Olsson
user-4
Posts: 397
Joined: Thu Jan 01, 1970 1:00 am

Post by user-4 »

Sorry, but we do not fully understand.

You get the a Graphic element from Image.Graphics collection and the sequence is the same as in the measurement table.

Could you please specify in more detail the application backround or send a code snippet.
user-6033
Posts: 89
Joined: Thu Jan 01, 1970 1:00 am

Post by user-6033 »

Hi,

Well, I do not really manage to extract these rotated rectangle graphics. Here is an example of a code snippet:

image = Zen.Analyzing.Analyze(b,ias)
Zen.Windows.ShowImage2d(image,False) - works excellent with nice rotated rectamgles around features
print image.Graphics.Count - Gives 0
print image.Graphics[0] - Does not work
print image.GraphicLayers.Count - Gives 3
print image.GraphicLayers[0].Count - Gives 0
print image.GraphicLayers[1].Count - Gives 0
print image.GraphicLayers[2].Count - Gives 0
print image.GraphicLayers[0].Name - Gives ""
print image.GraphicLayers[1].Name - Gives Annotation
print image.GraphicLayers[2].Name - Gives ""

So what is the right way to fetch those?
At the moment I use the feature data with the ellipse approximation to create my own rotated rectangles but using the already available rectangles could save some important processing time. We need to calculate on parameters such as contour distances between features and rotated rectangles is an accepted approximation with simpler mathematics compared with ellipses. We can use image processing techniques such as dilation -> thinning -> pruning but it simply adds too much time per image. Its better when we use higher binnings whereas gray8 or gray16 does not affect the timings. We run ~400 images per sample and the image acquiring counts for 1 - 1.2s per image including scanning step. So we try to optimize the processing step as much as possible.


Kind regards

Fredrik Olsson
user-4
Posts: 397
Joined: Thu Jan 01, 1970 1:00 am

Post by user-4 »

Hello Fredrik Olsson,

sorry, but access to the graphics that have been generated via Analysis is not possible at the moment.

This is a missing feature and we will put it on the ToDo list.

Thank you for your contribution!
user-4
Posts: 397
Joined: Thu Jan 01, 1970 1:00 am

Post by user-4 »

Hello Fredrik Olsson,

there is an alternative apporach via regions!

Code: Select all

image = Zen.Analyzing.Analyze(b,ias)

# Get regions from Analysis
regs = Zen.Analyzing.GetRegions(image, "Class1")

print("Number of Regions found: " + str(len(regs)))

#initialize Regions counter
r = 0

# initialize Table
table = ZenTable()


# for all regions get the polygon 
for reg in regs:
    poly = reg.GetPolygon()
    i = 0
    table.Columns.Add("Polygon_x")
    table.Columns.Add("Polygon_y")
    # write the coordinates in a table
    for point in poly:
        table.SetValue(i,0, point.X)
        table.SetValue(i,1, point.Y)
        i += 1
    # save table as csv
    filename = OutputFolder + "\\table_polygon_" + str(r) + ".csv"
    table.Save(filename)
    table.Close()
    r += 1
I hope this code snippet helps to speed up your processing.
user-6033
Posts: 89
Joined: Thu Jan 01, 1970 1:00 am

Post by user-6033 »

Hi,

I have tried it out and it looks promising and I get the point lists for the polygons. However, I have struggled a bit with trying to add polygon back as a graphic element in order to verify their positions. When setting the shape of a graphical element I only see the setbound method, which seems to be the same for all different graphical elements, thus works good for object whose size can be determined by applying boundary box position and box size. Is there an uncomplicated way to add a pointlist-polygon back as graphical element?


Best regards

Fredrik Olsson
user-4
Posts: 397
Joined: Thu Jan 01, 1970 1:00 am

Post by user-4 »

Hello Fredrik Olsson,

this worked for me:

Code: Select all

image = Zen.Application.ActiveDocument

polyline = image.Graphics.Add(ZenGraphicCategory.Polyline)

polyline AddPoint(10,20)
polyline AddPoint(20,30)
polyline AddPoint(30,50)
polyline AddPoint(20,60)
polyline AddPoint(10,20)
user-6033
Posts: 89
Joined: Thu Jan 01, 1970 1:00 am

Post by user-6033 »

Thank you!

I hoped for something as simple as this. I was not aware of that there were different options for the cases:
poly = ZenGraphic(ZenGraphicCategory.Polyline) (no AddPoint possibility)

and

poly = image.Graphics.Add(ZenGraphicCategory.Polyline)

Best regards

Fredrik Olsson
jhkhk jnkjk
Posts: 1
Joined: Thu Aug 17, 2023 5:33 pm

Re: Extract rotated rectangle around feature

Post by jhkhk jnkjk »

Hello Fredrik Olsson,

Thank you for reaching out. Yes, it is indeed possible to extract rotated rectangles from the annotation layer and associate them with unique IDs to link them to corresponding features in the feature table. This approach can significantly aid in rapidly calculating distances between feature boundaries, using oriented rectangles as a boundary approximation.

By extracting these rotated rectangles from the annotation layer and establishing a connection with their respective IDs in the feature table, you can efficiently perform boundary calculations and analyses, enhancing the accuracy and speed of your computations. INTERIOR & EXTERIOR WINDOW CLEANING

If you require any further assistance or have additional questions, please don't hesitate to ask.
Tuan Cho
Posts: 1
Joined: Thu Sep 14, 2023 12:20 pm

Re:

Post by Tuan Cho »

user-6033 wrote: Fri Oct 11, 2019 9:48 am Hi,

Well, I do not really manage to extract these rotated rectangle graphics. Here is an example of a code snippet:

image = Zen.Analyzing.Analyze(b,ias)
Zen.Windows.ShowImage2d(image,False) - works excellent with nice rotated rectamgles around features
print image.Graphics.Count - Gives 0
print image.Graphics[0] - Does not work
print image.GraphicLayers.Count - Gives 3
print image.GraphicLayers[0].Count - Gives 0
print image.GraphicLayers[1].Count - Gives 0
print image.GraphicLayers[2].Count - Gives 0
print image.GraphicLayers[0].Name - Gives ""
print image.GraphicLayers[1].Name - Gives Annotation
print image.GraphicLayers[2].Name - Gives ""

So what is the right way to fetch those?
At the moment I use the feature data with the ellipse approximation to create my own rotated rectangles but using the already available rectangles could save some important processing time. We need to calculate on parameters such as contour distances between features and rotated rectangles is an accepted approximation with simpler mathematics compared with ellipses. We can use image processing techniques such as dilation -> thinning -> nyt wordle -> pruning but it simply adds too much time per image. Its better when we use higher binnings whereas gray8 or gray16 does not affect the timings. We run ~400 images per sample and the image acquiring counts for 1 - 1.2s per image including scanning step. So we try to optimize the processing step as much as possible.


Kind regards

Fredrik Olsson
I think this will be the best and most suitable answer for you.
Post Reply