怎么用识别不同颜色的圆历程,同时也能统计圆的个数
import sensor, image, time
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QQVGA)
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()
while(True):
clock.tick()
img = sensor.snapshot().lens_corr(1.8)
红色圆们 = []
for c in img.find_circles(threshold = 3500, x_margin = 10, y_margin = 10, r_margin = 10,
r_min = 2, r_max = 100, r_step = 2):
area = (c.x()-c.r(), c.y()-c.r(), 2*c.r(), 2*c.r())
#area为识别到的圆的区域,即圆的外接矩形框
statistics = img.get_statistics(roi=area)#像素颜色统计
print(statistics)
#(0,100,0,120,0,120)是红色的阈值,所以当区域内的众数(也就是最多的颜色),范围在这个阈值内,就说明是红色的圆。
#l_mode(),a_mode(),b_mode()是L通道,A通道,B通道的众数。
if 0<statistics.l_mode()<100 and 0<statistics.a_mode()<127 and 0<statistics.b_mode()<127:#if the circle is red
img.draw_circle(c.x(), c.y(), c.r(), color = (255, 0, 0))#识别到的红色圆形用红色的圆框出来
红色圆们.push(c)
else:
img.draw_rectangle(area, color = (255, 255, 255))
#将非红色的圆用白色的矩形框出来
print(len(红色圆们))
可以在边缘检测程序之后再加识别矩形程序吗 加了以后报错
# 边缘检测例子:
#
# 这个程序示范了在图像上使用morph函数来进行边缘检测。
# 然后在进行阈值和滤波
import sensor, image, time
#设置核函数滤波,核内每个数值值域为[-128,127],核需为列表或元组
kernel_size = 1 # kernel width = (size*2)+1, kernel height = (size*2)+1
kernel = [-1, -1, -1,\
-1, +8, -1,\
-1, -1, -1]
# 这个一个高通滤波器。见这里有更多的kernel
# http://www.fmwconcepts.com/imagemagick/digital_image_filtering.pdf
thresholds = [(100, 255)] # grayscale thresholds设置阈值
sensor.reset() # 初始化 sensor.
sensor.set_pixformat(sensor.GRAYSCALE) # or sensor.RGB565
sensor.set_framesize(sensor.QQVGA) # or sensor.QVGA (or others)
sensor.skip_frames(10) # 让新的设置生效
clock = time.clock() # 追踪FPS
# 在OV7725 sensor上, 边缘检测可以通过设置sharpness/edge寄存器来增强。
# 注意:这个会edge detection can be enhanced
# significantly by setting the sharpness/edge registers.
# Note: This will be implemented as a function later.
if (sensor.get_id() == sensor.OV7725):
sensor.__write_reg(0xAC, 0xDF)
sensor.__write_reg(0x8F, 0xFF)
while(True):
clock.tick() # Track elapsed milliseconds between snapshots().
img = sensor.snapshot() # Take a picture and return the image.
img.morph(kernel_size, kernel)
#morph(size, kernel, mul=Auto, add=0),morph变换,mul根据图像对比度
#进行调整,mul使图像每个像素乘mul;add根据明暗度调整,使得每个像素值加上add值。
#如果不设置则不对morph变换后的图像进行处理。
img.binary(thresholds)
#利用binary函数对图像进行分割
# Erode pixels with less than 2 neighbors using a 3x3 image kernel
img.erode(1, threshold = 2)
#侵蚀函数erode(size, threshold=Auto),去除边缘相邻处多余的点。threshold
#用来设置去除相邻点的个数,threshold数值越大,被侵蚀掉的边缘点越多,边缘旁边
#白色杂点少;数值越小,被侵蚀掉的边缘点越少,边缘旁边的白色杂点越多。
print(clock.fps()) # Note: Your OpenMV Cam runs about half as fast while
# connected to your computer. The FPS should increase once disconnected.
for r in img.find_rects(threshold = 10000):
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))
print(r)
print("FPS %f" % clock.fps())
SyntaxError: can't assign to expression出错误报出如何解决,求大神帮忙
img.get_pixel(x,y)=0
get_pixel返回的具体的数值,不能直接赋值,要用set_pixel赋值。
fx,fy指相机在x轴和y轴上的焦距,cx,cy是相机的光圈中心,这组参数是摄像头生产制作之后就固定的
是这几个值吗
#OV5640 VGA 的像素大小
f_x = (2.8 / 3.6736) * 640 # 默认值
f_y = (2.8 / 2.7384) * 480 # 默认值
#图像的中心位置 分辨率的一半
c_x = 640 * 0.5 # 默认值(image.w * 0.5)
c_y = 480 * 0.5 # 默认值(image.h * 0.5)
根据这个值算出来的世界3D坐标系为啥不准?
这个blob.elongation()到底啥意思呀。
这个值和圆度相加是1。
越像圆,这个值越小,越不像圆,这个值越大。
如何单独一次输出色块中心坐标
import sensor, image, time
import math
from pyb import UART
import json
下面的阈值, 可能需要大家手动调节
thresholds = [(35, 40,55, 62, 20, 51), # 一般情况下的红色阈值
(43, 55, -45, -32, 2, 20), # 一般情况下的绿色阈值
(45, 65, -20, 30, -60, -20)] # 一般情况下的蓝色阈值
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()
uart = UART(3, 115200)
QVGA: 320x240
WINDOW_CENTER_X = 160
WINDOW_CENTER_Y = 120
标记当前识别的颜色
def markCurColor(img, cName, cRgb):
img.draw_string(0, 0, cName, color = cRgb)
如果是红色,执行此函数
def doWithRed(img):
markCurColor(img, "RED", (255, 0, 0))
如果是绿色,执行此函数
def doWithGreen(img):
markCurColor(img, "GREEN", (0, 255, 0))
如果是绿色,执行此函数
def doWithBlue(img):
markCurColor(img, "BLUE", (0, 0, 255))
颜色代码与所对应需要执行函数的映射
colorCodeMapFunc = {
1: doWithRed,
2: doWithGreen,
4: doWithBlue
}
测算色块的中心与中心的距离
def distanceToCenter(blob):
winCx = WINDOW_CENTER_X
winCy = WINDOW_CENTER_Y
x = blob.x()
y = blob.y()
return math.sqrt(math.pow(winCx - x, 2) + math.pow(winCy - y, 2))
比较两个色块距离的函数
def compareBlob(blob1, blob2):
# 这里我们选择了pixels作为指标比对二者的代码
# 你也可以换用其它指标 例如 blob.area()
tmp = distanceToCenter(blob1) - distanceToCenter(blob2)
if tmp == 0:
return 0;
elif tmp > 0:
return 1;
else:
return -1;
while(True):
clock.tick()
bigBlob = None #最大的色块
img = sensor.snapshot()
blobs = img.find_blobs( thresholds, pixels_threshold=200, area_threshold=200, merge=True)
# 判断是否有色块检测到
if len(blobs) == 0:
continue
bigBlob = blobs[0]
for blob in blobs:
# 如果此色块比最大的色块还大
if compareBlob(bigBlob, blob) == 1:
bigBlob = blob
# 标注其他色块
img.draw_rectangle(blob.rect())
# 标记识别到的最大色块
img.draw_cross(bigBlob.cx(), bigBlob.cy())
# 根据最大色块的颜色调用不同的函数
img.draw_cross(WINDOW_CENTER_X, WINDOW_CENTER_Y, color=(255, 0, 0))
img.draw_line((WINDOW_CENTER_X, WINDOW_CENTER_Y, bigBlob.cx(), bigBlob.cy()), color=(255, 0, 0))
if bigBlob.code() in colorCodeMapFunc:
colorCodeMapFunc[bigBlob.code()](img)
output_str="[%d,%d]" % (bigBlob.cx(),bigBlob.cy()) #方式1
#output_str=json.dumps([bigBlob.cx(),bigBlob.cy()]) #方式2
print('you send:',output_str)
uart.write(output_str+'\r\n')
else:
print("code not match" )
目前是连续输出坐标
有时两种不同的颜色测出来的阈值会差不多甚至一样?
A最小值和最大值,不对。反了。
从图像底部160列向左右分别寻找黑色点,遇到第一个黑色点停止并记录,但是返回的结果好像不正确
@kidswong999 屏幕上显示是白色的点,但获取的像素点二值化值确实黑色(值为0)
自动增益和自动白平衡有什么作用?
现在我遇到的问题是在进行颜色识别,室内光线也不暗,识别黑色,但发现图片很暗,不能识别黑色的物体,我想问问自动增益和自动白平衡有没有帮助,而且不同距离图片的光线不同,黑色的阈值也不同有没有好的解决办法?
自动增益和自动白平衡有什么作用?
代码就是例程里的,我想识别黑色,在黑色被子外面画矩形框,就是根据历程改了一下阈值,实际情况是被子和周围环境有明显差别的,但是你看我的摄像头采集的图像,黑色被子和后面的黑色很近,就是采集的整幅图画要比实际情况要暗很多,发浑似的