## Find rects, accessing x,y,h,w &magnitude

General discussion about topics related to OpenMV.
TheCrofter
Posts: 5
Joined: Wed Oct 30, 2019 6:07 pm

### Find rects, accessing x,y,h,w &magnitude

Hi,

Just purchased an H7 which is perfect for my project. The find rects demo program works well but I would like to use x y etc to trigger on output pin. I am very much a beginner, just starting a python course but it may be a while before I can figure out how to get at these variables. I note that the documentation lists rect.x rect.y . If anyone has the time to just add one of these parameter to the find rects code it would make my life easier and allow me to to move on with the project while still learning. Many thanks.

Code: Select all

``````# Find Rects Example
#
# This example shows off how to find rectangles in the image using the quad threshold
# detection code from our April Tags code. The quad threshold detection algorithm
# detects rectangles in an extremely robust way and is much better than Hough
# Transform based methods. For example, it can still detect rectangles even when lens
# distortion causes those rectangles to look bent. Rounded rectangles are no problem!
# (But, given this the code will also detect small radius circles too)...

import sensor, image, time

sensor.reset()
sensor.set_pixformat(sensor.RGB565) # grayscale is faster (160x120 max on OpenMV-M7)
sensor.set_framesize(sensor.QQVGA)
sensor.skip_frames(time = 2000)
clock = time.clock()

while(True):
clock.tick()
img = sensor.snapshot()

# `threshold` below should be set to a high enough value to filter out noise
# rectangles detected in the image which have low edge magnitudes. Rectangles
# have larger edge magnitudes the larger and more contrasty they are...

for r in img.find_rects(threshold = 10000):
img.draw_rectangle(r.rect(), color = (255, 0, 0))
for p in r.corners(): img.draw_circle(p[0], p[1], 5, color = (0, 255, 0))
print(r)

print("FPS %f" % clock.fps())``````
kwagyeman
Posts: 4529
Joined: Sun May 24, 2015 2:10 pm

### Re: Find rects, accessing x,y,h,w &magnitude

Hi, you just do something like:

r.x() in the for loop to get the x position of a rect.

e.g:

if r.x() == 5:
#do something
else:
#do something else
Nyamekye,
TheCrofter
Posts: 5
Joined: Wed Oct 30, 2019 6:07 pm

### Re: Find rects, accessing x,y,h,w &magnitude

Many thanks for the quick reply. Why would it be r.x and not rect.x ?
kwagyeman
Posts: 4529
Joined: Sun May 24, 2015 2:10 pm

### Re: Find rects, accessing x,y,h,w &magnitude

Hi, the Python object gets the name r from the for loop. The for r in list part creates an object called r which is the name of a rect from find rects. The rect then has methods that can be called to get the value.

You should Google how python code works for these types of questions. We run general python code so there's tons of info on all of this on the web. You need not ask me questions on this.
Nyamekye,
kwagyeman
Posts: 4529
Joined: Sun May 24, 2015 2:10 pm

### Re: Find rects, accessing x,y,h,w &magnitude

Just to be clear, one of the methods the rect has is called rect() which returns a python tuple which has (x,y,w,h). Or, you can just call x() to just get x.

We have both methods in our lib because some functions take only the full rect() tuple.

The actual rect object returned by find_rects is not a basic (x,y,w,h) tuple but more complex. So, the access methods allow extraction of data from the object.
Nyamekye,
TheCrofter
Posts: 5
Joined: Wed Oct 30, 2019 6:07 pm

Thanks