Hello,
I was amazed by how quickly and easily a little OpenMV cam find blobs, so that I decided to see if I can put those cams into my project.
I try to find dimensions on that kind of object:
After finding blobs, I want to erode(4) and dilate(4) to remove the white wire. But that quickly slow down the process. On the following code that is the computing time I measured:
- Convert to binary: 1.73ms
- Find blob (in grayscale): 1.04ms
- Crop (from 34808px to 18696px): 0.82ms
- img.erode(4): 9.59ms
- img.dilate(4): 9.53ms
- Find blob (in binary): 0.94ms
- Mesure: 0.76ms
A snapshot takes the camera 13.8ms. Then the total cycle is about 38.2ms and half is dedicated to the erode/dilate process.
In other terms:
- without erode/dilate=52 FPS
- with erode/dilate=26 FPS
Is there something I can do to filter my image at higher speed? Or erode/dilate is slow by its nature.
import sensor, image, time, math
streamname = "stream-c1.bin"
stream = image.ImageIO(streamname, "r")
px_threshold = 1641
px_Stride = 23
thresholdGS = [(40, 255)]
thresholdBIN = [(1,1)]
opening_px = 4
total_compute_time = 0
n = 1
while(True):
img = stream.read(copy_to_fb=True, loop=True, pause=True)
aftersnapshottime = time.ticks_us()
# Binary image, find the blob, crop and image opening
img.binary(thresholdGS,to_bitmap=True)
for blob in img.find_blobs(thresholdBIN, pixels_threshold=px_threshold, x_stride=px_Stride, y_stride=px_Stride, merge=True):
blobx, bloby, blobw, blobh = blob.rect()
blobroi = (blobx,bloby,blobw,blobh)
break
img = img.crop(roi=blobroi)
img.erode(opening_px)
img.dilate(opening_px)
# Measure blob axis
for blob in img.find_blobs(thresholdBIN, pixels_threshold=px_threshold, x_stride=px_Stride, y_stride=px_Stride, merge=True):
maja = blob.major_axis_line()
lmaja = round(math.sqrt((maja[2]-maja[0])**2 + (maja[3]-maja[1])**2), 1)
mina = blob.minor_axis_line()
lmina = round(math.sqrt((mina[2]-mina[0])**2 + (mina[3]-mina[1])**2), 1)
print("BlobGS |" + " P:" + str(blob.pixels()) + " L:" + str(lmaja) + " l:" + str(lmina))
break
total_compute_time = total_compute_time + time.ticks_diff(time.ticks_us(), aftersnapshottime)
average_ellapsed_compute_time = total_compute_time / n
print("CoMS: " + str(round(average_ellapsed_compute_time/1000,2)))
n = n + 1
stream-c1.zip (746 KB)