Foolproof OpenMV method for identifying rectangles, circles, and triangles

1. Official website method

Directly use find_circles() and find_rects() to identify rectangles and circles, use the find_lines() function to find three straight lines, and use triangle interior angles and 180° to identify triangles.
find_circles() detailed explanation
insert image description here
find_rects() detailed explanation
insert image description here
find_lines() detailed explanation
insert image description here
认真看手册!认真看手册!认真看手册!重要问题说三遍!
The parameters assigned to the function have a great influence on the final recognition effect, so be sure to understand the parameters carefully and then adjust them patiently.
When I use these functions, I either recognize a lot or none of them. The solution is to further process or filter the image. In the case of background clutter, the error of triangle recognition by this method will be very large.

Second, use the identification color block

My method is mainly to identify the color block, first identify the color and then identify the shape, and narrow the scope of judgment.
The detailed explanation of find_blobs() in the official website manual
mainly uses the function blob.density() to return the density of the color block. This is equal to the number of pixels of the color patch divided by the area of ​​the outer frame. For an orthographic rectangle, this value should be equal to 1, for an orthographic circle, this value should be equal to π/4, and for an orthographic triangle, it should be less than 0.5. But the actual situation is that the camera does not necessarily face it squarely, and the influence of other factors makes this value fluctuate up and down. The most effective way to adjust the parameters is: first print the density() of the rectangle, circle, and triangle respectively, and then determine the parameters after looking at the approximate range.
code show as below:

def detect(max_blob):#输入的是寻找到色块中的最大色块
    row_data=[-1,-1]#保存颜色和形状
    print(max_blob.solidity())
  
    if max_blob.density()>0.84:
        row_data[0]=max_blob.code()
        img.draw_rectangle(max_blob.rect())
        row_data[1]=1#表示矩形

    elif max_blob.density()>0.6:
        img.draw_circle((max_blob.cx(), max_blob.cy(),int((max_blob.w()+max_blob.h())/4)))
        row_data[0]=max_blob.code()
        row_data[1]=2#表示圆形
    elif max_blob.density()>0.4:
        row_data[0]=max_blob.code()
        row_data[1]=3#表示三角形
    return row_data#返回的是两个值,颜色和形状

It’s over here, but there’s always been a problem: if the rectangle is tilted at an angle or rotated, the calculated density() value is indeed very close to a circle, and sometimes it will be recognized as a circle, but if it is lowered, it will be Recognize circles as rectangles.
After being guided by a classmate, I learned that there is a function max_blob.solidity(), which is not mentioned in Xingtong Technology. This function works with identifying rectangles.
Final code:

def detect(max_blob):#输入的是寻找到色块中的最大色块
    row_data=[-1,-1]#保存颜色和形状
    print(max_blob.solidity())
  
    if max_blob.solidity()>0.9 or max_blob.density()>0.84:
        row_data[0]=max_blob.code()
        img.draw_rectangle(max_blob.rect())
        row_data[1]=1#表示矩形

    elif max_blob.density()>0.6:
        img.draw_circle((max_blob.cx(), max_blob.cy(),int((max_blob.w()+max_blob.h())/4)))
        row_data[0]=max_blob.code()
        row_data[1]=2#表示圆形
    elif max_blob.density()>0.4:
        row_data[0]=max_blob.code()
        row_data[1]=3#表示三角形
    return row_data #返回的是两个值,颜色和形状

Guess you like

Origin blog.csdn.net/weixin_52385589/article/details/126443973