logo

二值化+拍照 得到的是灰度图,不是二值化处理后的图。

2021-05-07 17:37
N
bthw
# 颜色二值化滤波例子
#
# 这个脚本展示了二值图像滤波。
# 您可以传递二进制任意的阈值来分割图像。

import sensor, image, time

sensor.reset()
sensor.set_framesize(sensor.QVGA)
sensor.set_pixformat(sensor.GRAYSCALE)


#设置颜色阈值,如果是rgb图像,六个数字分别为(minL, maxL, minA, maxA, minB, maxB);
#如果是灰度图,则只需设置(min, max)两个数字即可。
first_threshold = (0,64) # L A B
second_threshold = (65,128) # L A B
third_threshold = (129,192) # L A B
sensor.skip_frames(time = 2000)
clock = time.clock()

#使用工具 - >机器视觉 - >阈值编辑器选择更好的阈值。
while(True):

    #测试0-64阈值
    for i in range(1):
        clock.tick()
        img = sensor.snapshot()
        img.binary([first_threshold],invert = 1)
        sensor.snapshot().save("huidu1.bmp") # or "example.bmp" (or others)
        image.grayscale_to_binary(0)
        print(clock.fps())
        #image.binary(thresholds, invert=False)此函数将在thresholds内的
        #图像部分的全部像素变为1白,将在阈值外的部分全部像素变为0黑。invert将图像
        #的0 1(黑 白)进行反转,默认为false不反转。

    #测试65-128阈值
    for i in range(1):
        clock.tick()
        img = sensor.snapshot()
        img.binary([second_threshold],invert = 1)
        image.binary_to_grayscale(0)
        sensor.snapshot().save("huidu2.bmp")
        print(clock.fps())

想要把不同的灰度色阶处理成二值化图像,然后保存成二值化处理后的图片,但是打开图片总是 正常的灰度图

2021-05-07 20:41
I
网红小智智 星瞳科技

二值化是白色为255,黑色是0.

2021-05-11 17:02
N
bthw
b32a5709-09a1-49d3-aa98-59b9a158b1c3.png
db12ee3d-f9ae-4ed0-bc3d-13cd9285224a.png

已经改了 但是想得到黑白,拍照后还是灰度。

# 人脸追踪例程
#
# 这个例子展示了使用OpenMV Cam的关键点功能来跟踪一个被Haar Cascade 
# 检测到的人脸。 该脚本的第一部分使用前面Haar Cascade在图像中找到一张
# 脸。 之后,脚本使用关键点功能自动学习你的脸并跟踪它。 关键点可以
# 用来自动追踪任何东西。
import sensor, time, image,pyb

RED_LED_PIN = 1
BLUE_LED_PIN = 3

# Reset sensor
sensor.reset()
sensor.set_contrast(3)
sensor.set_gainceiling(16)
sensor.set_framesize(sensor.VGA)
sensor.set_windowing((320, 240))
sensor.set_pixformat(sensor.GRAYSCALE)

# 跳过几帧,让感光元件稳定下来,使设置生效
sensor.skip_frames(time = 2000)

# 加载Haar算子
# 默认情况下,这将使用所有阶段,较低的阶段更快但不太准确。
face_cascade = image.HaarCascade("frontalface", stages=25)
print(face_cascade)

# First set of keypoints
# 第一组关键点
kpts1 = None

# Find a face!
# 找一张脸!
while (kpts1 == None):
    img = sensor.snapshot()
    img.draw_string(0, 0, "Looking for a face...")
    # Find faces
    objects = img.find_features(face_cascade, threshold=0.5, scale=1.25)
    if objects:
        # 在每个方向上将ROI扩大31个像素
        face = (objects[0][0]-31, objects[0][1]-31,objects[0][2]+31*2, objects[0][3]+31*2)
        # 使用检测面大小作为ROI提取关键点
        kpts1 = img.find_keypoints(threshold=10, scale_factor=1.1, max_keypoints=100, roi=face)
        # 在第一个人脸周围画一个矩形
        img.draw_rectangle(objects[0])

# Draw keypoints
print(kpts1)
img.draw_keypoints(kpts1, size=24)
img = sensor.snapshot()
time.sleep_ms(2000)

# FPS clock
first_threshold = (0,64) # L A B
second_threshold = (65,128) # L A B
third_threshold = (129,192) # L A B
sensor.skip_frames(time = 2000)
clock = time.clock()

while (True):
    clock.tick()
    img = sensor.snapshot()
    # 从整个帧中提取关键点
    kpts2 = img.find_keypoints(threshold=10, scale_factor=1.1, max_keypoints=100, normalized=True)

    if (kpts2):
        # 将第一组关键点与第二组关键点匹配
        c=image.match_descriptor(kpts1, kpts2, threshold=85)
        match = c[6] # C[6] contains the number of matches. C[6]包含匹配的数量。
        if (match>5):
            img.draw_rectangle(c[2:6])
            img.draw_cross(c[0], c[1], size=10)
            pyb.LED(RED_LED_PIN).on()
            sensor.skip_frames(time = 2000) # 给用户一个时间来准备
            for i in range(1):
                    clock.tick()
                    img = sensor.snapshot()
                    img.binary([first_threshold])
                    image.grayscale_to_binary(32)
                    print(clock.fps())
                    #image.binary(thresholds, invert=False)此函数将在thresholds内的
                    #图像部分的全部像素变为1白,将在阈值外的部分全部像素变为0黑。invert将图像
                    #的0 1(黑 白)进行反转,默认为false不反转。
                    print("You're on camera!")
                    sensor.snapshot().save("huidu0.bmp") # or "example.bmp" (or others)
                #测试65-128阈值
            for i in range(1):
                    clock.tick()
                    img = sensor.snapshot()
                    img.binary([second_threshold])
                    image.binary_to_grayscale(0)
                    print(clock.fps())
                    print("You're on camera!")
                    sensor.snapshot().save("huidu1.bmp") # or "example.bmp" (or others)
                #测试蓝色阈值
            for i in range(1):
                    clock.tick()
                    img = sensor.snapshot()
                    img.binary([third_threshold])
                    image.binary_to_grayscale(0)
                    print(clock.fps())
                    print("You're on camera!")
                    sensor.snapshot().save("huodu2.bmp") # or "example.bmp" (or others)
            
          
            
            pyb.LED(BLUE_LED_PIN).off()
            print("Done! Reset the camera to see the saved image.")
            print(kpts2, "matched:%d dt:%d"%(match, c[7]))
            break
            
    # Draw FPS
    img.draw_string(0, 0, "FPS:%.2f"%(clock.fps()))

代码用人脸识别+上拍照的

2021-05-11 20:07
I
网红小智智 星瞳科技

你的代码有问题 。 sensor.snapshot().save("huodu2.bmp") 你每次保存的都是从sensor重新获取的,当然不对。

改为img.save("huodu2.bmp")