logo

如何在色块识别的框的范围内进行颜色替换?

2019-06-18 13:46
W
lcpdeb
99ac22b1-c877-4172-be13-d931e8874def.png
1d91c625-3038-4d08-9450-13c937ebf481.png
004e9625-7c35-48c8-83a6-36d4383d19d4.png

思路是先识别色块,然后在框内进行flood_fill的操作

我想完成图1的效果,但是经常出现图2图3的样子

我试过将flood_fill的(x,y)替换成色块识别的中心点,但是这样就没法填充了

以下是代码:

# 洪水填充
#
# 此示例显示图像中的洪水填充区域。
import sensor, image, time

sensor.reset()
sensor.set_pixformat(sensor.RGB565) # or GRAYSCALE...
sensor.set_framesize(sensor.QVGA) # or QQVGA...
sensor.skip_frames(time = 2000)
sensor.set_auto_whitebal(False);


print("Learning thresholds...")
threshold = (58, 91, 20, 60, -15, 18)


def find_max(blobs):
    max_size=0
    for blob in blobs:
        if blob[2]*blob[3]>max_size:
            max_blob=blob
            max_size=blob[2]*blob[3]
    return max_blob

while(True):

    # seed_threshold controls the maximum allowed difference between
    # the initial pixel and any filled pixels. It's important to
    # set this such that flood fill doesn't fill the whole image.

    # floating_threshold controls the maximum allowed difference
    # between any two pixels. This can easily fill the whole image
    # with even a very low threshold.

    # flood_fill will fill pixels that both thresholds.

    # You can invert what gets filled with "invert" and clear
    # everything but the filled area with "clear_background".

    #从中心点开始寻找,(x,y)即为Initial pixel
    x = sensor.width()//2
    y = sensor.height()//2
    img = sensor.snapshot()#从摄像头snapshot图像
    blobs=img.find_blobs([threshold], pixels_threshold=100, area_threshold=100, merge=True, margin=10)
    if blobs:
        #寻找最大物体
        max_blob=find_max(blobs)
        #设定ROI范围为最大物体的框
        roi=img.draw_rectangle(max_blob.rect())
        #在框中心点画十字
        img.draw_cross(max_blob.cx(), max_blob.cy())
        #x=max_blob.cx()
        #y=max_blob.cy()
        #在ROI中进行填充
        roi.flood_fill(x, y, seed_threshold=0.2, floating_thresholds=1, color=(255, 255, 0), invert=False, clear_background=False)
2019-06-19 12:26
I
网红小智智 星瞳科技
max_blob=find_max(blobs)
img.flood_fill(max_blob.cx(), max_blob.cy(), seed_threshold=0.2, floating_thresholds=1, color=(255, 255, 0), invert=False, clear_background=False)
2019-06-19 15:46
W
lcpdeb

@kidswong999 在 [如何在色块识别的框的范围内进行颜色替换?](/post/11848) 中说:

>

> ```

> max_blob=find_max(blobs)

> img.flood_fill(max_blob.cx(), max_blob.cy(), seed_threshold=0.2, floating_thresholds=1, color=(255, 255, 0), invert=False, clear_background=False)

> ```

7cf75438-00bc-44d1-8f1d-1ad6f7cc53b3.png

这我也试过了,这样就只有中心点是一个黄色的点,我是想把整个红色瓶盖换成别的颜色

2019-06-20 15:45
I
网红小智智 星瞳科技

img.flood_fill要在你画线画框函数的上面。看我的代码。

2019-06-20 17:22
W
lcpdeb

@kidswong999 在 [如何在色块识别的框的范围内进行颜色替换?](/post/11864) 中说:

> img.flood_fill要在你画线画框函数的上面。看我的代码。

搞定,谢谢