第3章 Python 数字图像处理(DIP) - 灰度变换与空间滤波4 - 分段线性变换 - 对比度拉伸

目录

    • 分段线性变换
        • 对比度拉伸
        • 最大最小值拉伸

分段线性变换

  • 优点

  • 形式可以任意复杂

  • 缺点

  • 要求用户输入很多参数

对比度拉伸

光照不足、成像传感器的动态范围偏小、图像获取过程中镜头孔径的设置错误

(r1,s1)和点(r2,s2)(r_1, s_1)和点(r_2, s_2)(r1,s1)(r2,s2)的位置控制变换函数的形状

图3,令(r1,s1)=(rmin,0),(r2,s2)=(rmax,L−1)(r_1, s_1) = (r_{min}, 0), (r_2, s_2) = (r_{max}, L-1)(r1,s1)=(rmin,0),(r2,s2)=(rmax,L1)
图4,令(r1,s1)=(m,0),(r2,s2)=(m,L−1),m是平均灰度级(r_1, s_1) = (m, 0), (r_2, s_2) = (m, L-1),m是平均灰度级(r1,s1)=(m,0),(r2,s2)=(m,L1)m

def stretch_3(img):"""constrast stretch, $(r_1, s_1) = (r_{min}, 0), (r_2, s_2) = (r_{max}, L-1)$return image stretchuse loop can get right image, but numpy still work, but the return image is more bright(if un normalize, then can get rightresult)"""img_min = img.min()img_max = img.max()#---------------------loop-----------------------------
#     img_dst = np.zeros(img.shape[:2], np.uint8)
#     height, width = img.shape[:2]#     for h in range(height):
#         for w in range(width):
#             temp = img[h, w]
#             if temp <= img_min:
#                 img_dst[h, w] = 0
#             elif temp >= img_max:
#                 img_dst[h, w] = 255
#             else:
#                 img_dst[h, w] = int(((temp - img_min) / img_max ) * 255)#-----------------------numpy-----------------------img_dst = np.piecewise(img, [img <= img_min, img <= img_max], [0, lambda x : (((x - img_min)/ img_max) * 255).astype(np.int)])return img_dst
def stretch_4(img):"""constrast stretch, $(r_1, s_1) = (r_{min}, 0), (r_2, s_2) = (r_{max}, L-1)$return image stretchuse loop can get right image, but numpy still work, but the return image is more bright(if un normalize, then can get rightresult)"""img_min = np.mean(img).astype(np.int)img_max = img.max()#---------------------loop-----------------------------
#     img_dst = np.zeros(img.shape[:2], np.uint8)
#     height, width = img.shape[:2]#     for h in range(height):
#         for w in range(width):
#             temp = img[h, w]
#             if temp <= img_min:
#                 img_dst[h, w] = 0
#             elif temp > img_min:
#                 img_dst[h, w] = 255
#             else:
#                 img_dst[h, w] = int(((temp - img_min) / img_max ) * 255)#-----------------------numpy-----------------------img_dst = np.piecewise(img, [img >= img_min], [lambda x : 255 if x.any() < img_min else 0])return img_dst
# 对比度拉伸
img = cv2.imread('DIP_Figures/DIP3E_Original_Images_CH03/Fig0310(b)(washed_out_pollen_image).tif', 0)# For ploting the stretch curve
x = [0, 96, 182, 255]
y = [0, 30, 220, 255]# subplot 3
img_subplot3 = stretch_3(img)
img_subplot3 = np.uint8(normalize(img_subplot3) * 255)
# subplot 4
img_subplot4 = stretch_4(img)plt.figure(figsize=(16, 16))
plt.subplot(2, 2, 1), plt.plot(x, y), plt.title('s=T(r)')
plt.ylabel('Output Value', rotation=90)
plt.xlabel('Input Value',  rotation=0)
plt.subplot(2, 2, 2), plt.imshow(img, cmap='gray', vmin=0, vmax=255), plt.title('Original')
plt.subplot(2, 2, 3), plt.imshow(img_subplot3, cmap='gray', vmin=0, vmax=255), plt.title('Transform 3')
plt.subplot(2, 2, 4), plt.imshow(img_subplot4, cmap='gray', vmin=0, vmax=255), plt.title('Transform 4')
plt.tight_layout()
plt.show()

在这里插入图片描述

def SLT(img, x1, x2, y1, y2):"""利用opencv, 实现对比度拉伸"""lut = np.zeros(256)for i in range(256):if i < x1:lut[i] = (y1/x1)*ielif i < x2:lut[i] = ((y2-y1)/(x2-x1))*(i-x1)+y1else:lut[i] = ((y2-255.0)/(x2-255.0))*(i-255.0)+255.0img_output = cv2.LUT(img, lut)img_output = np.uint8(img_output+0.5)return img_output
# opencv 对比度拉伸
img = cv2.imread('DIP_Figures/DIP3E_Original_Images_CH03/Fig0310(b)(washed_out_pollen_image).tif').astype(np.uint8)
img_x1 = 100
img_x2 = 160
img_y1 = 50
img_y2 = 255
output_img = SLT(img, img_x1, img_x2, img_y1, img_y2)plt.figure(figsize=(18, 15))
plt.subplot(1, 2, 1), plt.imshow(img, cmap='gray', vmin=0, vmax=255), plt.title('Original')
plt.subplot(1, 2, 2), plt.imshow(output_img, cmap='gray', vmin=0, vmax=255), plt.title('Transform')
plt.tight_layout()
plt.show()

在这里插入图片描述

def sigmoid(x, scale):"""simgoid fuction, return ndarray value [0, 1]param: input x: array like param: input scale: scale of the sigmoid fuction, if 1, then is original sigmoid fuction, if < 1, then the values between 0, 1will be less, if scale very low, then become a binary fuction; if > 1, then the values between 0, 1 will be more, if scalevery high then become a y = x"""y = 1 / (1 + np.exp(-x / scale))return y
def sigmoid_transform(image, scale):"""use sigmoid function to stretch constract of the imageparam: input image: [0, 255] uint8 grayscale imageparam: input scale: use scale to change the slope of the stretch curvereturn an [0, 255] uint8 gracyscale image"""img_temp = image.copy().astype(float)img_temp = img_temp - 127  # image.max() //2 because the max of input image might not be 255, so use fixed valueimg_dst = 1 / (1 + np.exp(- img_temp / scale))img_dst = np.uint8(normalize(img_dst) * 255.)return img_dst
# 用Sigmoid函数也可以实现对比度的拉伸,这样就不需要输入过多的参数
img = cv2.imread('DIP_Figures/DIP3E_Original_Images_CH03/Fig0310(b)(washed_out_pollen_image).tif', 0)# For ploting the stretch curve
x = [0, 96, 182, 255]
y = [0, 30, 220, 255]# sigmoid function plot
scale = 20
x1 = np.arange(0, 256, 1)
x2 = x1 - x1.max() // 2        # Here shift the 0 to the x center, here is 5, so x1 = [-5,  5]
t_stretch = sigmoid(x2, scale)# subplot 3 use sigmoid fuction to transform image
img_sigmoid = sigmoid_transform(img, scale)plt.figure(figsize=(16, 16))
plt.subplot(2, 2, 1), plt.plot(x, y), plt.title('s=T(r)')
plt.ylabel('Output Value', rotation=90)
plt.xlabel('Input Value',  rotation=0)
plt.subplot(2, 2, 2), plt.plot(x1, t_stretch), plt.title('Sigmoid')
plt.ylabel('Output Value', rotation=90)
plt.xlabel('Input Value',  rotation=0)
plt.subplot(2, 2, 3), plt.imshow(img, cmap='gray', vmin=0, vmax=255), plt.title('Original')
plt.subplot(2, 2, 4), plt.imshow(img_sigmoid, cmap='gray', vmin=0, vmax=255), plt.title('Transform 3')
plt.tight_layout()
plt.show()

在这里插入图片描述

最大最小值拉伸

def max_min_strech(img):"""min max stretch"""max1 = np.max(img)min1 = np.min(img)output_img = (255.0 * (img-min1)) / (max1 - min1)  # 注意255.0 而不是255 二者算出的结果区别很大output_img = np.uint8(output_img + 0.5)return output_img
# 最大最小值拉伸
img = cv2.imread('DIP_Figures/DIP3E_Original_Images_CH03/Fig0310(b)(washed_out_pollen_image).tif').astype(np.uint8)output_img = max_min_strech(img)plt.figure(figsize=(20, 10))
plt.subplot(1, 2, 1), plt.imshow(img, cmap='gray'), plt.title('Original')
plt.subplot(1, 2, 2), plt.imshow(output_img, cmap='gray'), plt.title('Transform')
plt.tight_layout()
plt.show()

在这里插入图片描述

# 最大最小值拉伸
img = cv2.imread('DIP_Figures/DIP3E_Original_Images_CH03/Fig0354(a)(einstein_orig).tif').astype(np.uint8)output_img = max_min_strech(img)plt.figure(figsize=(16, 10))
plt.subplot(1, 2, 1), plt.imshow(img, cmap='gray'), plt.title('Original')
plt.subplot(1, 2, 2), plt.imshow(output_img, cmap='gray'), plt.title('Transform')
plt.tight_layout()
plt.show()

在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/260832.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

2017网易内推编程题(判断单词):解答代码

2019独角兽企业重金招聘Python工程师标准>>> 小易喜欢的单词具有以下特性&#xff1a; 1.单词每个字母都是大写字母 2.单词没有连续相等的字母 3.单词没有形如“xyxy”(这里的x&#xff0c;y指的都是字母&#xff0c;并且可以相同)这样的子序列&#xff0c;子序列可…

Debian下IPv6设定主地址 Set primary IPv6 address under Debian Linux

Linux下选择IPv6主地址是按照一定规则来的 #RFC3484 (davidc) Prefer same address. (i.e. destination is local machine) Prefer appropriate scope. (i.e. smallest scope shared with the destination) Avoid deprecated addresses. Prefer home addresses.Prefer outgo…

iphone查看删除的短信_想要恢复已经删除的的短信怎么办?

阅读本文前&#xff0c;请您先点击上面的蓝色字体&#xff0c;再点击“关注”&#xff0c;这样您就可以继续免费收到文章了。每天都有分享&#xff0c;完全是免费订阅&#xff0c;请放心关注。 …

获取套接字相关联信息

前言 知道套接字描述符&#xff0c;如何获取这个套接字连接的相关信息呢&#xff1f;显然&#xff0c;这是一个必须要清楚的问题。 获取本地协议信息 函数原型&#xff1a;int getsockname ( int sockfd, struct sockaddr *localaddr, socklen_t *addrlen ) 函数功能&#xff1…

第3章 Python 数字图像处理(DIP) - 灰度变换与空间滤波5 - 分段线性变换 - 灰度级分层

目录灰度级分层灰度级分层 二值图像 将感兴趣范围内的所有灰显示为一个值&#xff08;白色&#xff09;&#xff0c;而将其它灰度值显示为另一个值&#xff08;黑色&#xff09; 其他灰度级不变 使期望的灰度范围变量&#xff08;或变暗&#xff09;&#xff0c;但保持图像中…

Oracle查看锁表

查看锁表进程SQL语句1&#xff1a; select sess.sid, sess.serial#, lo.oracle_username, lo.os_user_name, ao.object_name, lo.locked_mode from v$locked_object lo, dba_objects ao, v$session sess where ao.object_id lo.object_id and lo.session_id sess.sid; 查看锁…

SQL Server聚集索引的选择

先声明文章非原创&#xff0c;摘自博客园&#xff1a;http://www.cnblogs.com/CareySon/archive/2012/03/06/2381582.html 简介 在SQL Server中&#xff0c;数据是按页进行存放的。而为表加上聚集索引后&#xff0c;SQL Server对于数据的查找就是按照聚集索引的列作为关键字进行…

c++突破网关屏蔽_为什么加了屏蔽罩,测试效果反而不好?

来自专治PCB疑难杂症微信群群友(群友突破1200人啦&#xff0c;文末添加杨老师微信号&#xff0c;可添加入群)的问题讨论&#xff1a;设计时我加了屏蔽罩&#xff0c;结果在测试的时候不加屏蔽罩的效果要比加了屏蔽罩的效果好&#xff0c;这是为何&#xff1f;跟PCB设计的屏蔽罩…

第3章 Python 数字图像处理(DIP) - 灰度变换与空间滤波6 - 分段线性变换 - 比特平面分层

目录比特平面分层比特平面分层 在一幅256级灰度图像中&#xff0c;图像的值是由8比特&#xff08;1字节&#xff09;组成的 def convert_bin(data, n):"""convert decimal to binary, return n th bit, 0 if bit value 0 else 1""" #---------…

与众不同 windows phone (5) - Chooser(选择器)

与众不同 windows phone (5) - Chooser&#xff08;选择器&#xff09; 原文:与众不同 windows phone (5) - Chooser&#xff08;选择器&#xff09;[索引页][源码下载] 与众不同 windows phone (5) - Chooser&#xff08;选择器&#xff09;作者&#xff1a;webabcd介绍与众不…

iOS GCD

from&#xff1a;http://www.cnblogs.com/dsxniubility/p/4296937.html 一般&#xff1a; dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{// 耗时操作dispatch_async(dispatch_get_main_queue(), ^{// 更新UI});}); 本文是对以往学习的多线…

c++和java哪个难_2020 年 11 月编程语言排行榜,Python 超越 Java ?

来源&#xff1a;tiobe.com/tiobe-index/November-2020TIOBE 2020 年 11 月份的编程语言排行榜已经公布&#xff0c;官方的标题是&#xff1a;Python 势如破竹&#xff0c;超越 Java。题外话: 目前小哈正在个人博客(新搭建的网站&#xff0c;域名就是犬小哈的拼音) www.quanxia…

C# 温故而知新:Stream篇(七)

C# 温故而知新&#xff1a;Stream篇&#xff08;七&#xff09; NetworkStream 目录&#xff1a; NetworkStream的作用简单介绍下TCP/IP 协议和相关层次简单说明下 TCP和UDP的区别简单介绍下套接字&#xff08;Socket&#xff09;的概念简单介绍下TcpClient,TcpListener,IPEndP…

第3章 Python 数字图像处理(DIP) - 灰度变换与空间滤波7 - 直方图处理 - 直方图、归一化直方图

目录直方图处理直方图处理 令rk,k0,1,2,…,L−1r_k, k0, 1, 2, \dots, L-1rk​,k0,1,2,…,L−1表于一幅LLL级灰度数字图像f(x,y)f(x,y)f(x,y)的灰度。fff的非归一化直方图定义为&#xff1a; h(rk)nk,k0,1,2,…,L−1(3.6)h(r_{k}) n_{k}, \quad k 0, 1, 2, \dots, L-1 \tag{…

Xamarin Android提示找不到资源属性定义

为什么80%的码农都做不了架构师&#xff1f;>>> Xamarin Android提示找不到资源属性定义 错误信息&#xff1a;”Resource.Attribute”未包含”actonBarSize”的定义 Xamarin Android经常会出现找不到资源属性的错误。遇到这种问题&#xff0c;建议先清理解决方法和…

Google Chrome保存插件方法

1、拷贝下面地址到记事本 https://clients2.google.com/service/update2/crx?responseredirect&xid%3D~~~~%26uc 2、打开插件所在的页面&#xff0c;拷贝插件地址到记事本 如&#xff1a;https://chrome.google.com/webstore/detail/axure-rp-extension-for-ch/dogkpdfckl…

java web项目_[适合初中级Java程序员修炼手册从0搭建整个Web项目](二)

前言文本已收录至我的GitHub仓库&#xff0c;欢迎Star&#xff1a;https://github.com/bin392328206种一棵树最好的时间是十年前&#xff0c;其次是现在six-finger-web一个Web后端框架的轮子从处理Http请求【基于Netty的请求级Web服务器】 到mvc【接口封装转发)】&#xff0c;再…

MapReduce操作HBase

运行HBase时常会遇到个错误&#xff0c;我就有这样的经历。 ERROR: org.apache.hadoop.hbase.MasterNotRunningException: Retried 7 times 检查日志&#xff1a;org.apache.hadoop.ipc.RPC$VersionMismatch: Protocol org.apache.hadoop.hdfs.protocol.ClientProtocol versio…

转 ABAP_ALV_Function方式与OO方式(较为简单、普通的ALV)

ABAP_ALV_Function方式与OO方式(较为简单、普通的ALV) 分类&#xff1a; SAP ABAP2013-01-31 09:58 1511人阅读 评论(0) 收藏 举报目录 一、ALV简介 1、简介 2、ALV_GRID介绍 3、其它描述 二、开发ALV的基本流程 三、ALV相关开发细节 1、标准ALV与对象ALV的共同开发细节 2、标准…

MAC OS X 1.1 El Capitan安装方法与步骤

2019独角兽企业重金招聘Python工程师标准>>> 苹果公司发布了最新的Mac系统El Capitan,我也跟风安装了, 昨天试了一天终于算是安装成功了. ###电脑配置: CPU: E3-1230 v2 主板: 技嘉B75M D3V 显卡: 微星6850 声卡: Realtek ALC887 键盘: Noppoo 84键机械键盘 ###下载…