基于阈值方法分割彩色图像

作者: 陨星落云 | 来源:发表于2020-02-17 12:25 被阅读0次

图像阈值分割

1.查看图像直方图

2.选择合适的阈值

3.利用形态学操作,填补局部空洞

4.掩膜,得到图像结果

查看图像直方图,确定阈值
from skimage import io,img_as_ubyte
import numpy as np
import matplotlib.pyplot as plt

img = img_as_ubyte(io.imread('macro-photography-of-strawberry-934066.jpg',as_gray=True))
# print(img)

plt.figure(figsize=(10,8),dpi=80)
plt.subplot(221)
plt.imshow(img,cmap="gray")
plt.xlabel("原图",fontproperties='SimHei')
plt.subplot(222)
# 直方图显示
plt.hist(img.flat,bins=100,range=(0,225))
plt.xlabel("直方图",fontproperties='SimHei')
segm1 = (img<=180)
segm2 = (img>180)
plt.subplot(223)
plt.imshow(segm1,cmap="gray")
plt.xlabel("前景",fontproperties='SimHei')
plt.subplot(224)
plt.imshow(segm2,cmap="gray")
plt.xlabel("背景",fontproperties='SimHei')
plt.show()

输出结果:

查看直方图,设置阈值
阈值分割彩色图像
from skimage import io,img_as_ubyte
from skimage.color import rgb2gray
from skimage.morphology import opening,disk
import numpy as np
import matplotlib.pyplot as plt

# 读取彩色图像,并将图像类型设置为8位(0-255)
img = img_as_ubyte(io.imread('macro-photography-of-strawberry-934066.jpg'))
# 转换成灰度图像(类型自动转为0~1或-1~1),并将图像类型设置为8位
img_gray = img_as_ubyte(rgb2gray(img))

# 读取灰度图像的高h和宽w
h,w = img_gray.shape
# print(img_gray)

# 设置阈值
segm1 = (img_gray>180)
# print(segm1)

# 开运算
kernel = disk(10)   
img_opening = opening(segm1,kernel)
# 将单通道阈值,转为RGB通道的阈值
segm = np.tile(img_opening.reshape(h,w,1),3)

# 复制一份彩色图像
img1 = img.copy()
# 掩膜操作
img1[segm] = 0

# 显示图像
plt.figure(figsize=(10,8),dpi=80)
plt.subplot(121)
plt.imshow(img)
plt.xlabel("原图像",fontproperties='SimHei')
plt.subplot(122)
plt.imshow(img1)
plt.xlabel("阈值分割结果",fontproperties='SimHei')
plt.show()

输出结果:

阈值分割彩色图像.png

相关文章

  • 基于阈值方法分割彩色图像

    图像阈值分割 1.查看图像直方图 2.选择合适的阈值 3.利用形态学操作,填补局部空洞 4.掩膜,得到图像结果 查...

  • LabVIEW彩色图像分割(基础篇—14)

    基于目标颜色的彩色图像分割常包括色彩阈值处理(Color Threshold)和色彩分割(Color Segmen...

  • 三 (3.2 imgproc) 图像阈值操作

    阈值操作原理: 什么是阈值? 最简单的图像分割的方法。 应用举例:从一副图像中利用阈值分割出我们需要的物体部分(当...

  • Opencv之图像分割

    1、阈值分割 1.1 简介 图像阈值化分割是一种传统的最常用的图像分割方法,因其实现简单、计算量小、性能较稳定而成...

  • 图像分割

    1、阈值分割 1.1 简介 图像阈值化分割是一种传统的最常用的图像分割方法,因其实现简单、计算量小、性能较稳定而成...

  • Task05 图像分割/二值化

    5.1 简介 该部分的学习内容是对经典的阈值分割算法进行回顾,图像阈值化分割是一种传统的最常用的图像分割方法,因其...

  • 基于遗传算法和大津阈值分割法实现的图像分割

    一、简述 本实验采用遗传算法和大津阈值分割法确定图像分割的最佳阈值,从而对图像进行二值化分割。 二、大津阈值分割法...

  • openCV:图像的阈值处理

    阈值处理 定义 阈值处理即图像二值化。是图像分割的一种最简单的方法。二值化可以把灰度图像转换成二值图像。把大于某个...

  • 阈值处理

    阈值处理 阈值处理即图像二值化。是图像分割的一种最简单的方法。二值化可以把灰度图像转换成二值图像。把大于某个临界灰...

  • 分割

    1.阈值化2.基于边缘的分割 边缘图像阈值化 边缘松弛法 .边界跟踪 . 作为图搜索的边缘跟踪 .作为动态规划的边...

网友评论

    本文标题:基于阈值方法分割彩色图像

    本文链接:https://www.haomeiwen.com/subject/ildbfhtx.html