Shape Detection

will the new cam m7 be able to do shape detection?

Hi, what kind of shape detection do you want to do? find_blobs() gives you an area and pixels that match the color settings in that area. This can be used to distinguish between circles and squares. However, I’m assuming you want something else right?

yes, I have to recognize shares without color. Triangles, circles and squares

Okay, well, find_blobs() again can be used for this task using just grayscale. The ratio between the area of the bounding box around the object and the number of pixels tracked in the object is different for rectangles, circles, and squares.

However, what’s the vision setup for your problem? Are you looking at objects straight on, at angles? With different lighting? Please give me a detailed picture of your problem and what you want to do. You’ve given two responses now with a low amount of info so I can’t really help you if you don’t know exactly what you want to do.

I have to recognize a triangle painted on a surface and state where it is relative to the center of the screen and the angle of rotation.
I have to detect a particuoar triangle with angles defined… for example 45 45 and 90 degrees

Sorry, I forgot about this post for a while.

Okay, so, find_blobs() will work for this. First, you setup the thresholds to find the color of the triangle. This will let you find all the triangles in the image. find_blobs() will tell you the centroid of the object. So, that’s the location. find_blobs() will also tell you the rotation of the object too assuming that it has an interesting enough shape for the rotation to be uniquely determined. If all the sides of the triangle are not equal then this should be the case.

Lastly, if you have other objects in the view… the ratio between the bounding rectangle around the triangle and the number of pixels tracked should be equal to:

pixels*2 = area of rect.

So, you’d filter out all blobs find_blobs() returns that aren’t close to that ratio between the number of pixels tracked and the area. Where the definition of “close” is some tolerance you have to determine through testing.

Thank you so much for your reply.

I assume I mandato need to search for colors
I don’t know if my situation will allow me to do that as the light will change so much

Anyway as sono as I will get my new cam M7 I will try.

Thank you


Dear Sir
I am taken rectangular detection examples, it’s also working fine but it’s detecting all rectangles I want to detect only middle rectangle means what I want to do.
Example: one paper total 8 rectangles are there I want to detect 4th one means what I want to do.

Hi, you need to write code in python to filter the object output. This is very application specific so it’s up to you.

Dear Sir
Can you give the small example code for how to filter the object output, I will pickup after that.

Thanks in advance

See this example:

Dear Sir

Can you please send me the rectangles output filter example. Kindly do needful

Hi, if the rectangles are in a row in the x direction you just need to sort the rectangle objects by their x positions and then pick the 4th one from the list if it exist.

You can use the sorted() method in MicroPython to sort the list and you can set the key for sorting to be equal to the x value of each rectangle.

For example:

rect_list = find_rects(...)
sorted(rect_list, key = lambda rect: rect.x())
if len(rect_list) > 3:
  fourth_rect = rect_list[3]

Dear Sir
Please find below images for your reference, I want to detect that 6 rectangle’s presence. In that if anything missing means pin 1 have to on. After that I will control my process by using pin 1 voltage. Can you please send me the code for that.
Best regards
Cam position2.jpg

Do something like this:

import sensor, image, time

# Color Tracking Thresholds (L Min, L Max, A Min, A Max, B Min, B Max)
thresholds = [(30, 100, 15, 127, 15, 127)]

sensor.skip_frames(time = 2000)
sensor.set_auto_gain(False) # must be turned off for color tracking
sensor.set_auto_whitebal(False) # must be turned off for color tracking
clock = time.clock()

# Only blobs that with more pixels than "pixel_threshold" and more area than "area_threshold" are
# returned by "find_blobs" below. Change "pixels_threshold" and "area_threshold" if you change the
# camera resolution. "merge=True" merges all overlapping blobs in the image.

pin1 = Pin('P1', Pin.OUT_PP, Pin.PULL_NONE)

    img = sensor.snapshot()
    blobs = img.find_blobs(thresholds, pixels_threshold=200, area_threshold=200, merge=True)
    for b in blobs:
    pin1.value(len(blobs) == 6)

You can use the Tools → Machine Vision → Threshold Editor to pick the color thresholds.

Dear sir
Some what i did like this based on your inputs. This is working now, but want to know that i am doing right method?

import sensor, image, time
from image import SEARCH_EX, SEARCH_DS
from pyb import Pin

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

pin1 = Pin('P1', Pin.OUT_PP, Pin.PULL_DOWN)

    img = sensor.snapshot()
    rect = img.find_rects(threshold = 10000)
    for r in rect:
        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))
        pin1.value(len(rect) == 6)

    print("FPS %f" % clock.fps())

Move setting the pin outside of the loop. Otherwise the code is only executed if something is found and not if something is not found.

Sorry sir I am not understand, can you explain briefly.
Thanks for your supports

Move the pin1.value() statement outside of the for loop.

I am doing shape detection by using OpenMV cam, after shape detection pin1 value will change 0 to 1 but I am getting only 1.2V from pin1. voltage drop is happening. what can I do for that.

I checked voltage value in pin1 its coming 3.2V but after jointed 2 feet length wire voltage drop is happening its showing 1.2V only what can I do for that. can you please help me.

Thanks in advance.