Python opencv flood fill, day 21

Dream eraser 2021-01-21 00:49:28
python opencv flood day


Learning goals

Every day 1 Hours ,365 It's a miracle .

Learning today Python OpenCV Flood filling knowledge , Flood filling is also called flood filling algorithm .

The principle is Start with a pixel , A point that meets the pixel requirements nearby , Fill all in the specified color , Until it comes to the point where it doesn't meet the requirements .

If you remember conceptually , There are four common neighborhood pixel filling method , Eight neighborhood pixel filling method , Pixel filling method based on scan line .

For these concepts , Skip first , Before the establishment of the whole cognition , There's no value in learning any basic concepts .

The function prototype

The syntax prototype of flood fill is as follows :

floodFill(image, mask, seedPoint, newVal[, loDiff[, upDiff[, flags]]]) -> retval, image, mask, rect

This function has seven parameters , They are as follows :

  • image: The image of the operation
  • mask: A mask , Than image It's a lot of height 2 Pixel , Wide 2 Pixel . Fill cannot pass through non-zero pixels in the input mask
  • seedPoint: The starting pixel
  • newVal: New fill value ( Color )
  • loDiff: Low value of fill color ( Subtract the value from the color at the starting pixel )
  • upDiff: High value of fill color ( Add this value to the color at the starting pixel )
  • flags: Operation bit identifier , Color images are generally FLOODFILL_FIXED_RANGE Specify color fill

About flags And find a clear explanation :

When it comes to CV_FLOODFILL_FIXED_RANGE when , The pixel to be processed is compared with the seed point , If meet (s – loDiff, s + upDiff) Between (s Is the pixel value of the seed point ), Then fill in ;
When it comes to CV_FLOODFILL_MASK_ONLY when , be mask Can't be empty , here , Function does not fill the original image img, It's filling in the mask image .

Case code

The test picture is as follows :

Python OpenCV Flood fill , The journey of learning from scriptures No 21 God

The test code is as follows :

import cv2 as cv
import numpy as np
# Color image filling 
def fill_color_demo(src):
img_copy = src.copy()
h, w, ch = src.shape
# Declare a rectangular shape , Notice that the height and width increase 2 Pixel 
# np.zeros Returns a function of a given shape and type 0 Filled array 
mask = np.zeros([h+2, w+2], np.uint8)
# Parameters 1, Images to be filled with flooding 
# Parameters 2, A mask , Using a mask can specify in which area the algorithm is used , If you want to use it for the whole image , The mask size is the number of lines in the original image + 2, Number of columns + 2
# A mask , It's a two-dimensional 0 matrix , Because only the mask corresponds to 0 The location of the flood 
# Parameters 3, Flood filling seed points , Based on the pixels of the point, judge the pixels of similar colors , Whether it is flooded or not 
# Parameters 4, New colors for flood areas (BGR Format )
# Parameters 5, The seed pixel can be down the pixel value 
# Parameters 6, Seed point pixel can be up pixel value 
# Parameters 7, The processing mode of flooding algorithm 
cv.floodFill(img_copy, mask, (20, 20), (0, 255, 0),
(50, 50, 50), (100, 100, 100), cv.FLOODFILL_FIXED_RANGE)
cv.imshow("color_demo", img_copy)
if __name__ == "__main__":
src = cv.imread('./25.jpg')
fill_color_demo(src)
cv.waitKey()
cv.destroyAllWindows()

The results are as follows :
Python OpenCV Flood fill , The journey of learning from scriptures No 21 God
About setting the mask , Why do pixels +2, The explanation given in part is : When from 0 That's ok 0 Column begins flooding fill scan ,mask Extra 2 It can ensure that the pixels on the boundary of scanning will be processed . Let's understand for a moment .

About parameters 5 With the parameters 6, Find the following information :

Start from the starting seed point , Fill the connected pixels with the specified color . Connectivity depends on the color and brightness of adjacent pixels , Pixels belong to the repainted area in the following cases , The formula is as follows .
Python OpenCV Flood fill , The journey of learning from scriptures No 21 God
The popular explanation is as follows :

  • (20, 20): Is the location of the seed point ;
  • (0, 255, 0): Color flooding , green ;
  • (50, 50, 50): Three channel values of seed pixels [ b, g, r ] Benchmarking , The lowest value of the three channels in the original picture is [ b-50, g-50, r-50 ];
  • (100, 100, 100): Three channel values of seed pixels [ b, g, r ] Benchmarking , The filled range is in the original picture. The highest value of three channels is [ b+100, g+100, r+100];
  • cv.FLOODFILL_FIXED_RANGE: The pixel to be processed is compared with the seed point , Within the scope of , Then fill this pixel .

In the original image, there are only three channel values of pixels [ b-50, g-50, r-50 ] <= [ B , G, R] <=[ b+100, g+100, r+100] Within this range will be designated green (0, 255, 0) fill .

Binary image filling

Look at the code first , Pay attention to the notes .

import cv2 as cv
import numpy as np
def fill_binary():
# Set up a 400*400 The rectangular 
image = np.zeros([400, 400, 3], np.uint8)
# Fill the inside with a white square 
image[100:300, 100:300, :] = 255
cv.imshow("fill_binary", image)
# Set the mask 
mask = np.ones([402, 402], np.uint8)
mask[101:301, 101:301] = 0
# mask Not for 0 The area of is not filled ,mask by 0 It's the area that's filled in 
cv.floodFill(image, mask, (200, 200), (255, 255, 0),
cv.FLOODFILL_MASK_ONLY)
cv.imshow("filled_binary", image)
fill_binary()
cv.waitKey(0)
cv.destroyAllWindows()

This part of the code comes from the Internet , Focus on understanding FLOODFILL_MASK_ONLY that will do , This value indicates starting from the seed point , Fill the mask area .

OpenCV The end of the

1 Two hours later , Yes Python OpenCV Related knowledge , Have you got it ?

As a beginner , There are still many places where learning is not in-depth , I hope you will stick with me .

In my spare time , You can subscribe to the 100 cases of crawler course of eraser to learn the knowledge of crawler .

Want to learn Python Reptiles , You can subscribe to the eraser column ~ Click to find the surprise


Today is the first day of continuous writing 59 / 100 God .
If you have ideas you want to communicate 、 technology , Feel free to leave a comment in the comments section .


If you want to build a close relationship with a blogger , You can follow bloggers , Or focus on Blogger official account “ Non undergraduate programmer ”, Learn how a non undergraduate programmer grows up .
Blogger ID: Dream eraser , I hope you like 、 Comment on 、 Collection

版权声明
本文为[Dream eraser]所创,转载请带上原文链接,感谢
https://pythonmana.com/2021/01/20210121004830944w.html

  1. 【七天搞定Python】day01.Python环境配置、pip、IDE、注释、变量,数据类型、标识符/关键字、输出、输入
  2. Life is short, I learn Python
  3. Python image enhancement and special effects - using Baidu AI to color black and white images
  4. Python environment configuration, Pip, IDE, comment, variable, data type, identifier / keyword, output, input
  5. 为什么说Python是最伟大的语言?看图就知道了 - 知乎
  6. Why is Python the greatest language? Just look at the picture. - Zhihu
  7. 通过创建视频游戏来学习 Python
  8. Learn Python by creating video games
  9. Python3版本下创建计算给定日期范围内工作日方法
  10. Creating a method to calculate working days within a given date range in Python 3
  11. 图解爬虫,用几个最简单的例子带你入门Python爬虫
  12. Graphical crawler, with a few of the simplest examples to take you to the introduction of Python crawler
  13. python+requests基础知识
  14. Basic knowledge of Python + requests
  15. python自定义windowsr日志支持文件分割
  16. python+requests基础知识
  17. Python custom Windowsr log supports file segmentation
  18. Basic knowledge of Python + requests
  19. 高级测试 | Python笔试题
  20. 火了!开源的 Python 抢票神器,过年回家就看这一波了!
  21. Python 爬虫进阶 - 前后端分离有什么了不起,过程超详细!
  22. 【python】使用pip提示ModuleNotFoundError
  23. 【python】虚拟环境搭建
  24. Advanced test | Python written test questions
  25. Fire! Open source Python ticket grabbing artifact, come home to see this wave of New Year!
  26. Python crawler advanced - before and after the end of the separation of what great, super detailed process!
  27. [Python] prompt modulenotfounderror with PIP
  28. Building a virtual environment
  29. Serverless 架构下用 Python 轻松搞定图像分类和预测
  30. Easy image classification and prediction with Python under serverless architecture
  31. python协程爬取某网站的老赖数据
  32. Python coroutine crawls Laolai data of a website
  33. 使用Python分析姿态估计数据集COCO的教程
  34. Using Python to analyze the data set coco of attitude estimation
  35. win环境 python3 flask 上手整理 环境搭建(一)
  36. Getting started with win environment python3 flash
  37. Python实现一个论文下载器,赶紧收藏
  38. win环境 python3 flask 上手整理 快速上手-基础操作(二)
  39. Python 中常见的配置文件写法
  40. Python to achieve a paper Downloader, quickly collect
  41. Python批量 png转ico
  42. 使用line_profiler对python代码性能进行评估优化
  43. 使用line_profiler对python代码性能进行评估优化
  44. Getting started with Python 3 flash in win environment
  45. Common ways to write configuration files in Python
  46. Python会在2021年死去吗? Python 3.9最终版本的回顾
  47. Python batch PNG to ICO
  48. Using line_ Profiler evaluates and optimizes the performance of Python code
  49. Using line_ Profiler evaluates and optimizes the performance of Python code
  50. Will Python die in 2021? A review of the final version of Python 3.9
  51. Python3 SMTP send mail
  52. Understanding closures in Python: getting started with closures
  53. Python日志实践
  54. Python logging practice
  55. [python opencv 计算机视觉零基础到实战] 十、图片效果毛玻璃
  56. [python opencv 计算机视觉零基础到实战] 九、模糊
  57. 10. Picture effect ground glass
  58. [Python opencv computer vision zero basis to actual combat] 9. Fuzzy
  59. 使用line_profiler對python程式碼效能進行評估優化
  60. Using line_ Profiler to evaluate and optimize the performance of Python code