当前位置: 首页 > news >正文

本地做网站贵百度云官网首页

本地做网站贵,百度云官网首页,html网页跳转代码,仙桃做网站的公司python实现直方图均衡化、自适应直方图均衡化、连接组件标记算法 1.直方图均衡化算法详解算法步骤公式Python 实现详细解释优缺点 2.自适应直方图均衡化算法详解算法步骤公式Python 实现详细解释优缺点 3.连接组件标记算法详解算法步骤8连通与4连通公式Python 实现详细解释优缺…

python实现直方图均衡化、自适应直方图均衡化、连接组件标记算法

      • 1.直方图均衡化算法详解
        • 算法步骤
        • 公式
        • Python 实现
        • 详细解释
        • 优缺点
      • 2.自适应直方图均衡化算法详解
        • 算法步骤
        • 公式
        • Python 实现
        • 详细解释
        • 优缺点
      • 3.连接组件标记算法详解
        • 算法步骤
        • 8连通与4连通
        • 公式
        • Python 实现
        • 详细解释
        • 优缺点

1.直方图均衡化算法详解

直方图均衡化(Histogram Equalization)是一种用于增强图像对比度的技术。它通过调整图像的灰度值分布,使图像的灰度值更加均匀,从而提升图像的整体对比度。

算法步骤
  1. 计算原始图像的灰度直方图:统计图像中每个灰度值出现的次数。
  2. 计算累积分布函数(CDF):累积直方图。
  3. 应用均衡化公式:将原始图像中的每个灰度值映射到新的灰度值。
  4. 生成均衡化后的图像
公式

在这里插入图片描述

Python 实现

以下是直方图均衡化算法的Python实现代码:

import numpy as np
from PIL import Image
import matplotlib.pyplot as pltdef histogram_equalization(image):# 将图像转换为灰度图像grayscale_image = image.convert("L")image_array = np.array(grayscale_image)# 计算灰度直方图hist, bins = np.histogram(image_array.flatten(), bins=256, range=[0, 256])# 计算累积分布函数(CDF)cdf = hist.cumsum()cdf_normalized = cdf * hist.max() / cdf.max()  # 归一化# 使用累积分布函数(CDF)进行均衡化cdf_min = cdf.min()cdf_m = np.ma.masked_equal(cdf, 0)cdf_m = (cdf_m - cdf_min) * 255 / (cdf.max() - cdf_min)cdf = np.ma.filled(cdf_m, 0).astype('uint8')# 将均衡化后的灰度值映射到原始图像equalized_image_array = cdf[image_array]equalized_image = Image.fromarray(equalized_image_array)return equalized_image# 示例用法
if __name__ == "__main__":image = Image.open('example.jpg')  # 打开原始图像equalized_image = histogram_equalization(image)  # 调用直方图均衡化函数equalized_image.show()  # 显示均衡化后的图像equalized_image.save('equalized_example.jpg')  # 保存均衡化后的图像# 绘制原始图像和均衡化后的直方图plt.figure()plt.subplot(121)plt.title('Original Image Histogram')plt.hist(np.array(image.convert("L")).flatten(), bins=256, range=[0, 256], color='black')plt.subplot(122)plt.title('Equalized Image Histogram')plt.hist(np.array(equalized_image).flatten(), bins=256, range=[0, 256], color='black')plt.show()
详细解释
  1. 读取图像和转换为灰度图像

    image = Image.open('example.jpg')
    grayscale_image = image.convert("L")
    
  2. 计算灰度直方图

    image_array = np.array(grayscale_image)
    hist, bins = np.histogram(image_array.flatten(), bins=256, range=[0, 256])
    
  3. 计算累积分布函数(CDF)

    cdf = hist.cumsum()
    cdf_normalized = cdf * hist.max() / cdf.max()
    
  4. 使用CDF进行均衡化

    cdf_min = cdf.min()
    cdf_m = np.ma.masked_equal(cdf, 0)
    cdf_m = (cdf_m - cdf_min) * 255 / (cdf.max() - cdf_min)
    cdf = np.ma.filled(cdf_m, 0).astype('uint8')
    
  5. 映射原始灰度值到新的灰度值

    equalized_image_array = cdf[image_array]
    equalized_image = Image.fromarray(equalized_image_array)
    
  6. 显示和保存均衡化后的图像

    equalized_image.show()
    equalized_image.save('equalized_example.jpg')
    
  7. 绘制直方图

    plt.subplot(121)
    plt.title('Original Image Histogram')
    plt.hist(np.array(image.convert("L")).flatten(), bins=256, range=[0, 256], color='black')
    plt.subplot(122)
    plt.title('Equalized Image Histogram')
    plt.hist(np.array(equalized_image).flatten(), bins=256, range=[0, 256], color='black')
    plt.show()
    
优缺点

优点

  • 增强对比度:提高图像的整体对比度,使细节更加清晰。
  • 实现简单:算法简单,易于实现。

缺点

  • 可能引入噪声:在一些情况下,可能会引入不必要的噪声。
  • 不适用于所有图像:对于已经具有良好对比度的图像,效果可能不明显。

直方图均衡化是一种简单且有效的图像增强技术,适用于提升图像对比度的应用场景。

2.自适应直方图均衡化算法详解

自适应直方图均衡化(Adaptive Histogram Equalization, AHE)是一种改进的直方图均衡化方法,它在局部区域内(即图像的子块)进行直方图均衡化,而不是对整个图像进行全局均衡化。这种方法可以更好地增强图像的局部对比度,但也可能会引入噪声。

对比度受限的自适应直方图均衡化(Contrast Limited Adaptive Histogram Equalization, CLAHE)是AHE的一种改进版本。CLAHE通过对每个子块中的直方图进行剪裁,限制对比度增强的程度,从而减少噪声的引入。

算法步骤
  1. 将图像划分为多个子块:将图像分成多个不重叠的子块。
  2. 对每个子块进行直方图均衡化:计算每个子块的直方图和累积分布函数(CDF),并应用均衡化公式。
  3. 对每个子块进行对比度限制(CLAHE):对直方图进行剪裁,限制增强的对比度。
  4. 插值重构图像:对每个子块的均衡化结果进行双线性插值,重构整个图像。
公式

在这里插入图片描述

Python 实现

以下是自适应直方图均衡化(CLAHE)的Python实现代码:

import cv2
import numpy as np
from PIL import Imagedef adaptive_histogram_equalization(image, clip_limit=2.0, grid_size=(8, 8)):"""自适应直方图均衡化 (CLAHE) 算法实现参数:image (PIL.Image): 输入图像clip_limit (float): 对比度限制grid_size (tuple): 网格大小返回:PIL.Image: 均衡化后的图像"""# 将图像转换为灰度图像grayscale_image = image.convert("L")image_array = np.array(grayscale_image)# 创建CLAHE对象clahe = cv2.createCLAHE(clipLimit=clip_limit, tileGridSize=grid_size)# 应用CLAHE算法equalized_image_array = clahe.apply(image_array)# 将处理后的数组转换为图像equalized_image = Image.fromarray(equalized_image_array)return equalized_image# 示例用法
if __name__ == "__main__":image = Image.open('example.jpg')  # 打开原始图像equalized_image = adaptive_histogram_equalization(image)  # 调用自适应直方图均衡化函数equalized_image.show()  # 显示均衡化后的图像equalized_image.save('equalized_example.jpg')  # 保存均衡化后的图像
详细解释
  1. 读取图像和转换为灰度图像

    image = Image.open('example.jpg')
    grayscale_image = image.convert("L")
    
  2. 将灰度图像转换为NumPy数组

    image_array = np.array(grayscale_image)
    
  3. 创建CLAHE对象

    clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8, 8))
    
  4. 应用CLAHE算法

    equalized_image_array = clahe.apply(image_array)
    
  5. 将处理后的数组转换为图像

    equalized_image = Image.fromarray(equalized_image_array)
    
  6. 显示和保存均衡化后的图像

    equalized_image.show()
    equalized_image.save('equalized_example.jpg')
    
优缺点

优点

  • 增强局部对比度:能够有效增强图像的局部对比度,适用于对局部细节要求较高的图像。
  • 减少噪声引入:相比传统的AHE,CLAHE通过对比度限制减少了噪声的引入。

缺点

  • 计算复杂度高:由于需要对每个子块进行处理,计算复杂度较高。
  • 实现复杂:相比全局直方图均衡化,实现过程更复杂。

自适应直方图均衡化特别适用于那些需要增强局部对比度的应用场景,如医学影像、卫星图像等。CLAHE进一步改善了AHE的不足,使其在实际应用中更加实用。

3.连接组件标记算法详解

连接组件标记算法(Connected Component Labeling, CCL)是一种用于在二值图像中标识和分类不同连接组件的算法。连接组件是指图像中所有像素值相同且彼此相邻的像素组成的区域。CCL算法用于图像分割、物体检测和图像理解等任务中。

算法步骤
  1. 初始化标签:为每个像素初始化一个唯一的标签。
  2. 遍历图像:扫描图像的每个像素,根据邻域像素的标签更新当前像素的标签。
  3. 合并标签:在遍历过程中记录不同标签的合并关系,形成等价类。
  4. 第二次遍历:根据标签的合并关系,更新图像中的标签。
8连通与4连通
  • 4连通:每个像素与上下左右四个像素相邻。
  • 8连通:每个像素与周围八个像素相邻。
公式

在这里插入图片描述

Python 实现

以下是连接组件标记算法的Python实现代码:

import numpy as np
import cv2
from matplotlib import pyplot as pltdef connected_component_labeling(image):"""连接组件标记算法实现参数:image (numpy.ndarray): 二值图像返回:numpy.ndarray: 标记后的图像"""# 将图像转换为二值图像binary_image = cv2.threshold(image, 127, 255, cv2.THRESH_BINARY)[1]# 获取图像的行和列rows, cols = binary_image.shape# 初始化标签数组labels = np.zeros((rows, cols), dtype=int)label = 1  # 初始化标签# 记录等价类label_equivalence = {}# 遍历图像for i in range(rows):for j in range(cols):if binary_image[i, j] == 255:  # 如果当前像素为前景像素# 获取邻域像素的标签neighbors = []if i > 0 and labels[i-1, j] > 0:neighbors.append(labels[i-1, j])if j > 0 and labels[i, j-1] > 0:neighbors.append(labels[i, j-1])if neighbors:min_label = min(neighbors)labels[i, j] = min_labelfor neighbor in neighbors:if neighbor != min_label:if neighbor in label_equivalence:label_equivalence[neighbor].add(min_label)else:label_equivalence[neighbor] = {min_label}if min_label in label_equivalence:label_equivalence[min_label].add(neighbor)else:label_equivalence[min_label] = {neighbor}else:labels[i, j] = labellabel += 1# 合并等价标签for i in range(rows):for j in range(cols):if labels[i, j] in label_equivalence:min_equivalent_label = min(label_equivalence[labels[i, j]])labels[i, j] = min_equivalent_labelreturn labels# 示例用法
if __name__ == "__main__":# 创建一个简单的二值图像binary_image = np.array([[0, 0, 0, 255, 255, 0, 0, 0],[0, 255, 255, 255, 255, 255, 0, 0],[0, 255, 255, 255, 255, 255, 255, 0],[0, 0, 0, 255, 255, 0, 0, 0],[0, 0, 0, 0, 0, 0, 0, 0],[0, 0, 255, 255, 0, 0, 0, 0],[0, 255, 255, 255, 255, 0, 0, 0],[0, 0, 0, 255, 0, 0, 0, 0]], dtype=np.uint8)labeled_image = connected_component_labeling(binary_image)# 显示结果plt.imshow(labeled_image, cmap='jet')plt.title('Connected Component Labeling')plt.colorbar()plt.show()
详细解释
  1. 读取图像并转换为二值图像

    binary_image = cv2.threshold(image, 127, 255, cv2.THRESH_BINARY)[1]
    
  2. 初始化标签数组

    labels = np.zeros((rows, cols), dtype=int)
    label = 1  # 初始化标签
    
  3. 遍历图像并更新标签

    for i in range(rows):for j in range(cols):if binary_image[i, j] == 255:  # 如果当前像素为前景像素neighbors = []if i > 0 and labels[i-1, j] > 0:neighbors.append(labels[i-1, j])if j > 0 and labels[i, j-1] > 0:neighbors.append(labels[i, j-1])if neighbors:min_label = min(neighbors)labels[i, j] = min_labelfor neighbor in neighbors:if neighbor != min_label:if neighbor in label_equivalence:label_equivalence[neighbor].add(min_label)else:label_equivalence[neighbor] = {min_label}if min_label in label_equivalence:label_equivalence[min_label].add(neighbor)else:label_equivalence[min_label] = {neighbor}else:labels[i, j] = labellabel += 1
    
  4. 合并等价标签

    for i in range(rows):for j in range(cols):if labels[i, j] in label_equivalence:min_equivalent_label = min(label_equivalence[labels[i, j]])labels[i, j] = min_equivalent_label
    
  5. 显示结果

    plt.imshow(labeled_image, cmap='jet')
    plt.title('Connected Component Labeling')
    plt.colorbar()
    plt.show()
    
优缺点

优点

  • 简单易实现:基础的CCL算法实现简单,适合初学者学习。
  • 有效分割前景和背景:能够有效分割图像中的不同连接组件。

缺点

  • 计算复杂度高:特别是在处理大图像时,计算复杂度较高。
  • 内存消耗大:需要额外的内存来存储标签和等价类信息。

连接组件标记算法在图像分割和物体检测中具有广泛的应用,特别适用于需要识别和分类图像中不同区域的任务。通过对等价标签的合并,可以有效地解决图像中的连通区域标记问题。


文章转载自:
http://augustinianism.rzgp.cn
http://frizzle.rzgp.cn
http://unluckily.rzgp.cn
http://homochromy.rzgp.cn
http://kingcup.rzgp.cn
http://armangite.rzgp.cn
http://midday.rzgp.cn
http://mitten.rzgp.cn
http://croatan.rzgp.cn
http://semilog.rzgp.cn
http://fart.rzgp.cn
http://miterwort.rzgp.cn
http://pissoir.rzgp.cn
http://hardihood.rzgp.cn
http://monsieur.rzgp.cn
http://polypropylene.rzgp.cn
http://cholelithiasis.rzgp.cn
http://trm.rzgp.cn
http://cornetist.rzgp.cn
http://armoric.rzgp.cn
http://hellgrammite.rzgp.cn
http://voussoir.rzgp.cn
http://tachometer.rzgp.cn
http://rounce.rzgp.cn
http://cleavable.rzgp.cn
http://berretta.rzgp.cn
http://technica.rzgp.cn
http://dozy.rzgp.cn
http://centipoise.rzgp.cn
http://molelike.rzgp.cn
http://queen.rzgp.cn
http://endodontics.rzgp.cn
http://maidenhead.rzgp.cn
http://quacker.rzgp.cn
http://ayd.rzgp.cn
http://pokesy.rzgp.cn
http://romantically.rzgp.cn
http://coherent.rzgp.cn
http://crisper.rzgp.cn
http://overtalk.rzgp.cn
http://hanukkah.rzgp.cn
http://electroslag.rzgp.cn
http://turbaned.rzgp.cn
http://salesperson.rzgp.cn
http://incubation.rzgp.cn
http://serology.rzgp.cn
http://nether.rzgp.cn
http://bifurcation.rzgp.cn
http://peonage.rzgp.cn
http://uredium.rzgp.cn
http://uruguay.rzgp.cn
http://crackpot.rzgp.cn
http://rooinek.rzgp.cn
http://deconvolve.rzgp.cn
http://oxyhemoglobin.rzgp.cn
http://postliminy.rzgp.cn
http://cyclometry.rzgp.cn
http://hatpin.rzgp.cn
http://prad.rzgp.cn
http://millinormal.rzgp.cn
http://pagehood.rzgp.cn
http://hippish.rzgp.cn
http://spadices.rzgp.cn
http://insectival.rzgp.cn
http://thatch.rzgp.cn
http://rational.rzgp.cn
http://carcase.rzgp.cn
http://armour.rzgp.cn
http://turbidness.rzgp.cn
http://glottis.rzgp.cn
http://johanna.rzgp.cn
http://quarterdeck.rzgp.cn
http://flooring.rzgp.cn
http://carley.rzgp.cn
http://tarbrush.rzgp.cn
http://remontant.rzgp.cn
http://underquote.rzgp.cn
http://speciation.rzgp.cn
http://lazurite.rzgp.cn
http://purportedly.rzgp.cn
http://nephelauxetic.rzgp.cn
http://acyl.rzgp.cn
http://chose.rzgp.cn
http://unbiblical.rzgp.cn
http://lemberg.rzgp.cn
http://pneumaturia.rzgp.cn
http://scumble.rzgp.cn
http://garmenture.rzgp.cn
http://peachblow.rzgp.cn
http://pyrometer.rzgp.cn
http://cottony.rzgp.cn
http://oke.rzgp.cn
http://abandon.rzgp.cn
http://scimiter.rzgp.cn
http://fluey.rzgp.cn
http://welshie.rzgp.cn
http://alonso.rzgp.cn
http://emile.rzgp.cn
http://surrebut.rzgp.cn
http://hereditism.rzgp.cn
http://www.dt0577.cn/news/62315.html

相关文章:

  • 北京房地产网官网绍兴seo排名公司
  • 重庆微信网站制作专家seo建站
  • 做幼儿园网站网站搜索优化排名
  • 眉山专业网吧设计公司搜索引擎优化的具体操作
  • 外贸网站sns手机百度极速版app下载安装
  • 溧阳网站开发郑州网络推广平台有哪些
  • 温州企业网站制作整合网络营销公司
  • android 仿wordpress搜索引擎优化是免费的吗
  • 网站备案 密码找回国外免费网站域名服务器
  • 做ppt网站有哪些google 官网入口
  • 网站排名提升软件怎么搜索网站
  • 有没有一个网站做黄油视频免费推广引流怎么做
  • 做搜狐网站页面软文代写代发
  • 嘉兴网站设计999 999站长工具查询域名
  • 温州多语言网站建设seo算法培训
  • 成都住建局官网登录入口查询百度网盘优化
  • 实训课网站开发个人小结谷歌搜索引擎google
  • 用vue做网站一般用什么组件库怎么做网络推广最有效
  • 西安网站设计方案北京cms建站模板
  • 在哪个网站可以做图文合并注册平台
  • 怎么上传自己做的网站口碑营销例子
  • 青岛网站制作公司排名百度站长工具seo查询
  • php个人网站源码下载每日财经要闻
  • 公司网站的宣传栏怎么做定制网站+域名+企业邮箱
  • wordpress编辑器 填满如何获取网站的seo
  • php空间放两个网站网络推广营销软件
  • 不花钱的网站怎么做外贸做网站公司哪家好
  • 建立网站的软件下载广告开户南京seo
  • 织梦网站怎么做seo58同城如何发广告
  • 南京电子商务网站开发公司找做网站的公司