Shape Detection

General discussion about topics related to OpenMV.
lucafelici
Posts: 4
Joined: Wed Jan 18, 2017 11:45 am
Location: Italy

Shape Detection

Postby lucafelici » Wed Jan 18, 2017 11:47 am

will the new cam m7 be able to do shape detection?
User avatar
kwagyeman
Posts: 2797
Joined: Sun May 24, 2015 2:10 pm

Re: Shape Detection

Postby kwagyeman » Wed Jan 18, 2017 12:38 pm

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?
Nyamekye,
lucafelici
Posts: 4
Joined: Wed Jan 18, 2017 11:45 am
Location: Italy

Re: Shape Detection

Postby lucafelici » Thu Jan 19, 2017 2:27 am

yes, I have to recognize shares without color. Triangles, circles and squares
User avatar
kwagyeman
Posts: 2797
Joined: Sun May 24, 2015 2:10 pm

Re: Shape Detection

Postby kwagyeman » Thu Jan 19, 2017 11:00 am

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.
Nyamekye,
lucafelici
Posts: 4
Joined: Wed Jan 18, 2017 11:45 am
Location: Italy

Re: Shape Detection

Postby lucafelici » Thu Jan 19, 2017 3:17 pm

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
User avatar
kwagyeman
Posts: 2797
Joined: Sun May 24, 2015 2:10 pm

Re: Shape Detection

Postby kwagyeman » Sat Jan 21, 2017 10:39 am

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.
Nyamekye,
lucafelici
Posts: 4
Joined: Wed Jan 18, 2017 11:45 am
Location: Italy

Re: Shape Detection

Postby lucafelici » Sat Jan 21, 2017 2:51 pm

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

Luca
prasap1
Posts: 12
Joined: Wed Dec 12, 2018 4:37 am
Location: India

Re: Shape Detection

Postby prasap1 » Mon Dec 31, 2018 9:21 am

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.
Thanks
and
Regards
Prasath
User avatar
kwagyeman
Posts: 2797
Joined: Sun May 24, 2015 2:10 pm

Re: Shape Detection

Postby kwagyeman » Mon Dec 31, 2018 11:49 am

Hi, you need to write code in python to filter the object output. This is very application specific so it's up to you.
Nyamekye,
prasap1
Posts: 12
Joined: Wed Dec 12, 2018 4:37 am
Location: India

Re: Shape Detection

Postby prasap1 » Wed Jan 02, 2019 3:25 am

kwagyeman wrote:
Mon Dec 31, 2018 11:49 am
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
Regards
Prasath
User avatar
kwagyeman
Posts: 2797
Joined: Sun May 24, 2015 2:10 pm

Re: Shape Detection

Postby kwagyeman » Wed Jan 02, 2019 11:22 am

Nyamekye,
prasap1
Posts: 12
Joined: Wed Dec 12, 2018 4:37 am
Location: India

Re: Shape Detection

Postby prasap1 » Mon Jan 07, 2019 2:11 am

kwagyeman wrote:
Wed Jan 02, 2019 11:22 am
See this example: https://github.com/openmv/openmv/blob/m ... on.py#L267
Dear Sir

Can you please send me the rectangles output filter example. Kindly do needful
User avatar
kwagyeman
Posts: 2797
Joined: Sun May 24, 2015 2:10 pm

Re: Shape Detection

Postby kwagyeman » Mon Jan 07, 2019 2:55 am

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:

Code: Select all

rect_list = find_rects(...)
sorted(rect_list, key = lambda rect: rect.x())
if len(rect_list) > 3:
  fourth_rect = rect_list[3]
Nyamekye,
prasap1
Posts: 12
Joined: Wed Dec 12, 2018 4:37 am
Location: India

Re: Shape Detection

Postby prasap1 » Sat Jan 12, 2019 2:03 am

kwagyeman wrote:
Mon Jan 07, 2019 2:55 am
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:

Code: Select all

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.
Thanks
&
Best regards
Prasath
Attachments
Cam position2.jpg
Cam image2.jpg
User avatar
kwagyeman
Posts: 2797
Joined: Sun May 24, 2015 2:10 pm

Re: Shape Detection

Postby kwagyeman » Sat Jan 12, 2019 12:44 pm

Do something like this:

Code: Select all

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.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
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)

while(True):
    clock.tick()
    img = sensor.snapshot()
    blobs = img.find_blobs(thresholds, pixels_threshold=200, area_threshold=200, merge=True)
    for b in blobs:
        img.draw_rectangle(b.rect())
        img.draw_cross(b.cx(), b.cy())
    pin1.value(len(blobs) == 6)
    print(clock.fps())
You can use the Tools -> Machine Vision -> Threshold Editor to pick the color thresholds.
Nyamekye,
prasap1
Posts: 12
Joined: Wed Dec 12, 2018 4:37 am
Location: India

Re: Shape Detection

Postby prasap1 » Mon Jan 21, 2019 11:09 am

kwagyeman wrote:
Sat Jan 12, 2019 12:44 pm
Do something like this:

Code: Select all

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.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
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)

while(True):
    clock.tick()
    img = sensor.snapshot()
    blobs = img.find_blobs(thresholds, pixels_threshold=200, area_threshold=200, merge=True)
    for b in blobs:
        img.draw_rectangle(b.rect())
        img.draw_cross(b.cx(), b.cy())
    pin1.value(len(blobs) == 6)
    print(clock.fps())
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?

Code: Select all

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

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()

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

while(True):
    clock.tick()
    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(r)

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

Re: Shape Detection

Postby kwagyeman » Mon Jan 21, 2019 3:36 pm

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.
Nyamekye,
prasap1
Posts: 12
Joined: Wed Dec 12, 2018 4:37 am
Location: India

Re: Shape Detection

Postby prasap1 » Thu Jan 24, 2019 1:32 am

kwagyeman wrote:
Mon Jan 21, 2019 3:36 pm
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
User avatar
kwagyeman
Posts: 2797
Joined: Sun May 24, 2015 2:10 pm

Re: Shape Detection

Postby kwagyeman » Thu Jan 24, 2019 1:33 am

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

Return to “General Topics”

Who is online

Users browsing this forum: No registered users and 3 guests