Python图像分割之区域增长法
原文链接:https://blog.csdn.net/sgzqc/article/details/119682864
一、简介
区域增长法是一种已受到计算机视觉界十分关注的图像分割方法。它是以区域为处理对象的,它考虑到区域内部和区域之间的同异性,尽量保持区域中像素的临近性和一致性的统一。这样就可以更好地分辨图像真正的边界。

基于区域的分割方法的关键在于定义一个一致性准则,用来判断两个邻接的区域是否可以合并,一致则将两区域合并,直到不能合并为止。区域增长的方法是在图像上选定一个种子点,记录下该点的灰度值,作为一致性判断的标准阈值,此外还需要定义一个标准差。
算法的主要过程是,依次用图像的每一个像素的灰度值和标准阈值相减,判断结果是否小于标准差,是则将该点和种子点合并,不是则保持像素点的灰度值不变。这样处理后的图像就是用区域分割法处理后的边缘分割图像。
二、实例
下面我们通过一个例子来进行详细的解释

上图示意的是区域增长的过程,图中的方格表示图像的像素点,方格中的数值表示像素点的灰度值。(a)表示开始选取的生长点,在生长的过程中,每个生长点都将本身上下左右4个像素点和初试选取的生长点比较灰度值,如果灰度值的差的绝对值在设定的阈值内,则认为这些点是属于相同区域并将其合并,否则将灰度差大于设定阈值的点删除,重复检查区域内的像素点,直到没有像素点可以合并位置。不妨设上图的阈值为2,(b)中4个点和初始点的灰度差都不大于2,所以合并;(c)中只有部分满足条件,所以只合并满足条件的像素点,并且(c)区域周围邻域中没有点再满足条件,因此生长结束。
三、算法步骤
通过上述示例,我们可以总结出区域增长法的一般步骤如下:
1)对图像自上而下,自左而右扫描,找到第1个还没有访问过的像素, 设该像素为(x0, y0);
2)以(x0, y0)为中心, 考虑(x0, y0)的8邻域像素(x, y),如果其邻域满足生长准则, 将(x, y)与(x0, y0)合并(在同一区域内), 同时将(x, y)压入堆栈;
3)从堆栈中取出一个像素, 把它当作(x0, y0)返回到上一步骤;
4)当堆栈为空时 返回到步骤1;
5)重复步骤1 - 4直到图像中的每个点都被访问过时,算法结束。
四、代码实现
1 读入彩色图像
img_name = "test.jpg"
img = cv2.imread(img_name)
结果如下:

2 灰度化
gray_img = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
结果如下:

3 二值化
def get_binary_img(img):
# gray img to bin image
bin_img = np.zeros(shape=(img.shape), dtype=np.uint8)
h = img.shape[0]
w = img.shape[1]
for i in range(h):
for j in range(w):
bin_img[i][j] = 255 if img[i][j] > 127 else 0
return bin_img
# 调用
bin_img = get_binary_img(gray_img)
结果如下:

4 选取初始种子点
这里选择三个种子点作为初始点,种子点的坐标如下图紫色十字所示。
out_img = np.zeros(shape=(bin_img.shape), dtype=np.uint8)
# 选择初始3个种子点
seeds = [(176,255),(229,405),(347,165)]
for seed in seeds:
x = seed[0]
y = seed[1]
out_img[y][x] = 255

5 区域增长结果
# 8 邻域
directs = [(-1,-1), (0,-1), (1,-1), (1,0), (1,1), (0,1),(-1,1),(-1,0)]
visited = np.zeros(shape=(bin_img.shape), dtype=np.uint8)
while len(seeds):
seed = seeds.pop(0)# 将元素从列表中删弹出,默认最后一个
x = seed[0]
y = seed[1]
# visit point (x,y)
visited[y][x] = 1
for direct in directs:
cur_x = x + direct[0]
cur_y = y + direct[1]
# 非法
if cur_x <0 or cur_y<0 or cur_x >= w or cur_y >=h :
continue
# 没有访问过且属于同一目标
if (not visited[cur_y][cur_x]) and (bin_img[cur_y][cur_x]==bin_img[y][x]) :
out_img[cur_y][cur_x] = 255
visited[cur_y][cur_x] = 1
seeds.append((cur_x,cur_y))
结果如下:

6 获取目标
以上步得到的二值图作为mask,去彩色图中取对应的部分即可。代码如下:
bake_img = img.copy()
h = bake_img.shape[0]
w = bake_img.shape[1]
for i in range(h):
for j in range(w):
if out_img[i][j] != 255:
bake_img[i][j][0] = 0
bake_img[i][j][1] = 0
bake_img[i][j][2] = 0
结果如下:

7 完整代码
通过区域增长法来提取前景目标
# -*- coding:utf-8 -*-
import cv2
import numpy as np def get_binary_img(img):
# gray img to bin image
bin_img = np.zeros(shape=(img.shape), dtype=np.uint8)
h = img.shape[0]
w = img.shape[1]
for i in range(h):
for j in range(w):
bin_img[i][j] = 255 if img[i][j] > 80 else 0
return bin_img
img_name = "test.jpg"
img = cv2.imread(img_name)
h = img.shape[0]
w = img.shape[1]
gray_img = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) # 调用
bin_img = get_binary_img(gray_img)
out_img = np.zeros(shape=(bin_img.shape), dtype=np.uint8)
# 选择初始3个种子点
seeds = [(176,255),(229,205),(307,165)]
for seed in seeds:
x = seed[0]
y = seed[1]
out_img[y][x] = 255
# 8 邻域
directs = [(-1,-1), (0,-1), (1,-1), (1,0), (1,1), (0,1),(-1,1),(-1,0)]
visited = np.zeros(shape=(bin_img.shape), dtype=np.uint8)
while len(seeds):
seed = seeds.pop(0)
x = seed[0]
y = seed[1]
# visit point (x,y)
visited[y][x] = 1
for direct in directs:
cur_x = x + direct[0]
cur_y = y + direct[1]
# 非法
if cur_x <0 or cur_y<0 or cur_x >= w or cur_y >=h :
continue
# 没有访问过且属于同一目标
if (not visited[cur_y][cur_x]) and (bin_img[cur_y][cur_x]==bin_img[y][x]) :
out_img[cur_y][cur_x] = 255
visited[cur_y][cur_x] = 1
seeds.append((cur_x,cur_y))
bake_img = img.copy()
h = bake_img.shape[0]
w = bake_img.shape[1]
for i in range(h):
for j in range(w):
if out_img[i][j] != 255:
bake_img[i][j][0] = 0
bake_img[i][j][1] = 0
bake_img[i][j][2] = 0 cv2.imshow('image',img)
cv2.imshow('rowgrow',bake_img)
cv2.waitKey(0)
cv2.destroyAllWindows()
通过设置阈值来控制生长条件
# -*- coding:utf-8 -*-
import cv2
import numpy as np def get_binary_img(img):
# gray img to bin image
bin_img = np.zeros(shape=(img.shape), dtype=np.uint8)
h = img.shape[0]
w = img.shape[1]
for i in range(h):
for j in range(w):
bin_img[i][j] = 255 if img[i][j] > 150 else 0
return bin_img
img_name = "test.jpg"
img = cv2.imread(img_name)
h = img.shape[0]
w = img.shape[1]
gray_img = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
print(gray_img ) # 调用
# bin_img = get_binary_img(gray_img)
out_img = np.zeros(shape=(gray_img.shape), dtype=np.uint8)
# 选择初始3个种子点
seeds = [(276,155)]
for seed in seeds:
x = seed[0]
y = seed[1]
out_img[y][x] = 255
# 8 邻域
directs = [(-1,-1), (0,-1), (1,-1), (1,0), (1,1), (0,1),(-1,1),(-1,0)]
visited = np.zeros(shape=(gray_img.shape), dtype=np.uint8)
while len(seeds):
seed = seeds.pop(0)
x = seed[0]
y = seed[1]
# visit point (x,y)
visited[y][x] = 1
for direct in directs:
cur_x = x + direct[0]
cur_y = y + direct[1]
# 非法
if cur_x <0 or cur_y<0 or cur_x >= w or cur_y >=h :
continue
# 没有访问过且属于同一目标
if (not visited[cur_y][cur_x]) and (abs(int(gray_img[cur_y][cur_x])-int(gray_img[y][x]))<5.0) :
out_img[cur_y][cur_x] = 255
visited[cur_y][cur_x] = 1
seeds.append((cur_x,cur_y))
bake_img = img.copy()
h = bake_img.shape[0]
w = bake_img.shape[1]
for i in range(h):
for j in range(w):
if out_img[i][j] != 255:
bake_img[i][j][0] = 0
bake_img[i][j][1] = 0
bake_img[i][j][2] = 0 cv2.imshow('rowgrow',bake_img)
cv2.waitKey(0)
cv2.destroyAllWindows()
Python图像分割之区域增长法的更多相关文章
- python操作mysql数据库增删改查的dbutils实例
python操作mysql数据库增删改查的dbutils实例 # 数据库配置文件 # cat gconf.py #encoding=utf-8 import json # json里面的字典不能用单引 ...
- python字典dict的增、删、改、查操作
## python字典dict的增.删.改.查操作dict = {'age': 18, 'name': 'jin', 'sex': 'male', }#增# dict['heigh'] = 185 # ...
- ARCGIS API for Python进行城市区域提取
ArcGIS API for Python主要用于Web端的扩展和开发,提供简单易用.功能强大的Python库,以及大数据分析能力,可轻松实现实时数据.栅格数据.空间数据等多源数据的接入和GIS分析 ...
- 使用Python实现子区域数据分类统计
目录 前言 geopandas简介 子区域数据分类统计 总结 一.前言 最近碰到一个需求,需要统计某省内的所有市的某数据分布情况信息.现有该省的数据分布情况以及该省的行政区划数据.我通过 ...
- python学习(list增删改查、及常用方法)
1.Python多条件判断: 多条件判断if: passelse: pass循环 while for i = 0 while i > 1: print('hello') else: print( ...
- python 为什么没有自增自减符
>>> b = 5 >>> a = 5 >>> id(a) 162334512 >>> id(b) 162334512 > ...
- python中字典的‘增、删、改、查’
python 字典的'增.删.改.查' 用 ipython 运行情况如下: #新建字典 In [1]: infos = {'name':'dongge','age':18} In [2]: infos ...
- Python之注册表增删改查(干货)
在Windows平台下,对注册表的增删改查的需求比较多,微软提供了很多用于访问,修改注册表等的API,我们可以使用诸如bat,或者C++等各种方式去访问修改注册表.无所不能的python下如何完成这些 ...
- Python实现mysql数据库增删改查
利用python操作mysql数据库用法简单,环境配置容易,本文将实现对库增.删.改.查的简易封装! 1. 环境配置 安装第三方包 ,导入模块 mysql.connector pip inst ...
随机推荐
- SickOs1.2靶机
仅供个人娱乐 靶机信息 靶机下载地址:https://www.vulnhub.com/entry/sickos-12,144/一.主机发现 arp-scan -l 二.端口扫描 1. masscan ...
- 零基础涂鸦智能面板SDK开发记录(一)
前言 本人基础背景:在学校学了点JS,在blbl上看过几节node.js视频,现在是一名Android开发工程师,因公司需要学习涂鸦面板SDK开发.说真的除了官方的一些文档外,我真的找不到其他的资料. ...
- 15 道超经典大厂 Java 面试题!重中之重
从超高频的后端面试题出发,指明学习方向 大家好,我是鱼皮. 还记得我的老弟小阿巴么?他目前正值大一暑假,在家自学编程(刷短视频)中. 他整个大一期间基本都在学习前端.后来,我带他写了一次后端,结果就崩 ...
- 三层交换机和VLAN
目录 一.VLAN的概述及优势 二.VLAN的种类 三.静态VLAN的配置 四.Trunk介绍与配置 五.三层交换机转发原理 一.VLAN的概述及优势 分割广播域 物理分割 逻辑分割 VLAN的优势: ...
- Github连接远程仓库详细操作
首先 咱们需要配置ssh密钥 如何生成密钥呢 我们先来看下命令 在桌面新建一个文件夹,命名规范尽量不要使用中文,然后在文件夹内 新建测试文本: 如图 在文件夹内空白处右击进入GIt b ...
- Map的putAll方法验证
下面的程序验证了Map的putAll方法的行为特性,代码如下: import java.util.HashMap; public class Map_putAllTest { public stati ...
- Greenplum数仓监控解决方案(开源版本)
Greenplum监控解决方案 基于Prometheus+Grafana+greenplum_exporter+node_exporter实现 关联图 一.基本概念 1.Prometheus Pr ...
- 用Python爬取《王者荣耀》英雄皮肤数据并可视化分析,用图说话
大家好,我是辰哥~ 今天辰哥带大家分析一波当前热门手游<王者荣耀>英雄皮肤,比如皮肤上线时间.皮肤类型(勇者:史诗:传说等).价格. 1.获取数据 数据来源于<王者荣耀官方网站> ...
- 【笔记】scikit-learn中的PCA(真实数据集)
sklearn中的PCA(真实的数据集) (在notebook中) 加载好需要的内容,手写数字数据集 import numpy as np import matplotlib.pyplot as pl ...
- 使用PageFactory类封装页面元素,并实现简单的登录
1.新建页面对象类LoginPage import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; impo ...