02 find_blobs函数的探究 ---- 效率

基础怎么用官网说的太多了,但是就博主使用的过程中,有些坑还是要提醒下同样使用OpenMv的同学。

首先贴出官网对于该函数的说明:

image.find_blobs(thresholds[, roi=Auto,x_stride=2, y_stride=1, invert=False, area_threshold=10,pixels_threshold=10, merge=False, margin=0, threshold_cb=None,merge_cb=None])

Finds all blobs (connected pixel regionsthat pass a threshold test) in the image and returns a list of blob objects whichdescribe each blob. Please see the blob object moremore information.

thresholds must be a listof tuples [(lo, hi), (lo, hi), ..., (lo, hi)] defining theranges of color you want to track. You may pass up to 16 threshold tuples inone image.find_blobs call. For grayscale images each tupleneeds to contain two values - a min grayscale value and a max grayscale value.Only pixel regions that fall between these thresholds will be considered. ForRGB565 images each tuple needs to have six values (l_lo, l_hi, a_lo, a_hi,b_lo, b_hi) - which are minimums and maximums for the LAB L, A, and B channelsrespectively. For easy usage this function will automatically fix swapped minand max values. Additionally, if a tuple is larger than six values the rest areignored. Conversely, if the tuple is too short the rest of the thresholds areassumed to be zero.

Note

To get thethresholds for the object you want to track just select (click and drag) on theobject you want to track in the IDE frame buffer. The histogram will thenupdate to just be in that area. Then just write down where the colordistribution starts and falls off in each histogram channel. These will be yourlow and high values for thresholds. It’s best tomanually determine the thresholds versus using the upper and lower quartilestatistics because they are too tight.

The latestversion of OpenMV IDE features a threshold editor to help make pickingthresholds easer. It lets you control the threshold with sliders so you can seewhat the thresholds are segmenting.

roi is theregion-of-interest rectangle tuple (x, y, w, h). If not specified, it is equalto the image rectangle. Only pixels within the roi are operatedon.

x_stride is the numberof x pixels to skip when searching for a blob. Once a blob is found the linefill algorithm will be pixel accurate. Increase x_stride to speed upfinding blobs if blobs are know to be large.

y_stride is the numberof y pixels to skip when searching for a blob. Once a blob is found the linefill algorithm will be pixel accurate. Increase y_stride to speed upfinding blobs if blobs are know to be large.

invert inverts thethresholding operation such that instead of matching pixels inside of someknown color bounds pixels are matched that are outside of the known colorbounds.

If a blob’s bounding box area is lessthan area_threshold it is filtered out.

If a blob’s pixel count is less than pixel_threshold it is filteredout.

merge if True mergesall not filtered out blobs who’s bounding rectangles intersect each other. margin can be used toincrease or decrease the size of the bounding rectangles for blobs during theintersection test. For example, with a margin of 1 blobs who’s boundingrectangles are 1 pixel away from each other will be merged.

Merging blobs allows you to implementcolor code tracking. Each blob object has a code value which isa bit vector made up of 1s for each color threshold. For example, if you pass image.find_blobs two colorthresholds then the first threshold has a code of 1 and the second 2 (a thirdthreshold would be 4 and a fourth would be 8 and so on). Merged blobs logicallyOR all their codes together so that you know what colors produced them. Thisallows you to then track two colors if you get a blob object back with twocolors then you know it might be a color code.

You might also want to merge blobs ifyou are using tight color bounds which do not fully track all the pixels of anobject you are trying to follow.

Finally, if you want to merge blobs,but, don’t want two color thresholds to be merged then just call image.find_blobs twice withseparate thresholds so that blobs aren’t merged.

threshold_cb may be set tothe function to call on every blob after its been thresholded to filter it fromthe list of blobs to be merged. The call back function will receive oneargument - the blob object to be filtered. The call back then must return Trueto keep the blob and False to filter it.

merge_cb may be set tothe function to call on every two blobs about to be merged to prevent or allowthe merge. The call back function will receive two arguments - the two blobobjects to be merged. The call back then must return True to merge the blobs orFalse to prevent merging the blobs.

Not supported on compressed images.

具体怎么用自行脑补。由于在IDE中我们使用的是MicroPython进行编程。所以想测试一下调用该函数的效率。

场景:在一个下方的图中查找质心  该图已经进行二值化,使用灰度图像进行拍摄

startTime = pyb.millis()        # 测试用时,调试用

for blob in img.find_blobs([thresholds],roi=(0,0,256,8)pixels_threshold=15, area_threshold=10, merge=False):

   img.draw_cross(blob.cx(),blob.cy(),size = 8 ,color = 67)

   pointTemp[i+1]= blob.cx()  # pointTemp数组用于存储质心

stopTime = pyb.millis()

print(stopTime - startTime)    # 打印测试时间

结果显示:该算法在一个256*8的矩阵相框中执行完毕只需要1ms

 

OK,现在博主用micropython写一个查找质心的算法

startTime = pyb.millis()        # 测试用时,调试用

 

   for m in range(roi_x, roi_x + roi_w):

                for n in range(roi_y, roi_y +roi_h):

                    pixel_value =imgSample.get_pixel(m,n)

                    if pixel_value == 255:

                        sum_x = sum_x + m

                       area = area + 1

           if area > 20:

                pointTemp[i] = int(sum_x/area)                 #二值化中计算白点的个数 x点加完后进行除总数而得出质心

                Blobs_Num = Blobs_Num + 1

           else:

                pointTemp[i] = 0

 

           rect =(roi_x,roi_y,roi_w,roi_h)

           imgSample.draw_rectangle(rect,color = (128,0,128))   #test

stopTime = pyb.millis()

print(stopTime - startTime)    # 打印测试时间

测试完毕,数据结果和上面自带的库函数差不多,但是耗时整整多达37ms

 

目前有两个猜测:

1、  如果使用C进行编译的话耗时不会多达37MSMicroPython自带的库函数的底层库是否是用C编程的呢?

2、  Micropython 继承于Python,而python是一种解释型、面向对象、动态数据类型的语言,会不会在for循环的时候一直开辟内存空间给变量吧,如果不是,为何在for循环耗时这么久?

你可能感兴趣的:(OpenMv)