Wolfram Function Repository
Instant-use add-on functions for the Wolfram Language
Function Repository Resource:
Simple tool for working with regions of interest on images
ResourceFunction["ImageROIConvert"][roi] represents a rectangular region of interest roi, given by<|"x"→xmin,"y"→ymin,"w"→width,"h"→height|>. | |
ResourceFunction["ImageROIConvert"][roi,image,transform] represents a rectangular region of interest roi in an image after applying a coordinate system transform. | |
ResourceFunction["ImageROIConvert"][roi, {width,height}, transform] represents a rectangular region of interest roi in an image of given dimensions after applying a coordinate system transform. |
Sometimes ROIs (regions of interest) are provided in terms of width and height:
In[1]:= |
|
Out[2]= |
|
Having to switch between coordinate systems with a top or bottom origin is a very common. When converting to a "TopLeft" or "BottomRight" origin, you need to supply the original image (or its dimensions):
In[3]:= |
|
Out[3]= |
|
As a convenience, ImageROIConvert handles regions given in XYWH form. They can be given as real pixel coordinate values in the range [0,w]⨯[0,h] or as scaled coordinates in the range [0,1]⨯[0,1]:
In[4]:= |
|
Out[4]= |
|
To run this example, you first need to connect to a python executable in an environment (e.g. here my environment is named dl) where both opencv and pyzmq are installed (for more details see this workflow):
In[5]:= |
|
Out[5]= |
|
Now let's draw a rectangle on an image with the python computer vision library OpenCV (which is named cv2 in code):
In[6]:= |
|
Out[7]= |
|
Convert the coordinate system's origin to "BottomLeft" to plot it:
In[8]:= |
|
Out[8]= |
|
Create a Rectangle region of interest and highlight it:
In[9]:= |
|
Out[10]= |
|
To use this in OpenCV (whose python library is named cv2), we ask for a "TopLeft" origin coordinate transform:
In[11]:= |
|
Out[11]= |
|
Now we can send it to a session for drawing (requires a python installation):
In[12]:= |
|
Out[13]= |
|
Create a Rectangle with scaled coordinates for use in OpenCV (this example requires a python installation):
In[14]:= |
|
Out[18]= |
|
If setting the option "ScaledCoordinates"→True, the coordinates of the input are assumed to lie between zero and one:
In[19]:= |
|
Out[20]= |
|
In[21]:= |
|
Out[22]= |
|
Convert object detections from the Wolfram Language to Python (this example requires a python installation):
In[23]:= |
|
In[24]:= |
|
Out[24]= |
|
Highlight the identified bounding boxes:
In[25]:= |
|
Out[25]= |
|
Convert the bird's bounding boxes to have top left origin:
In[26]:= |
|
Out[26]= |
|
Export the variables to the python session:
In[27]:= |
|
Draw the boxes:
In[28]:= |
|
Out[28]= |
|
This work is licensed under a Creative Commons Attribution 4.0 International License