图像处理之点运算(python实现)opencv


点运算 是指按照某种灰度变换关系,逐像素地对图像中的每个像素的灰度值进⾏变换的⽅法。
在这里插入图片描述

点运算定义

设输入图像的灰度为f(x,y),输出图像的灰度为g(x, y), 则点运算可以表示为:
g(x, y) = T[f(x, y)]
灰度变换函数T[ ],即点运算是⼀种像素的逐点运算,是灰度到灰度的映射过程, 故称为灰度变换函数。
若令f(x, y) 和 g(x, y)在任意点 (x, y) 的灰度级分别为 r 和 s,则灰度变换函数可简化表示为:
s = T[r]

线性点运算

s=ar+b
在这里插入图片描述

灰度反转(a=-1, b=0)

在这里插入图片描述

# -- coding: utf-8 --
import cv2
import matplotlib.pyplot as plt
import numpy as np
img=cv2.imread("1.jpg",0)

# 获取图像高度和宽度
height = img.shape[0]
width = img.shape[1]

#创建一幅图像, uint8是专门用于存储各种图像的(包括RGB,灰度图像等),范围是从0–255
result = np.zeros((height, width), np.uint8)

# 图像灰度反转
for i in range(height):
    for j in range(width):
        gray = -(img[i, j])+255
        result[i, j] = np.uint8(gray)

# 显示图形
plt.figure(num='comparison')
titles = ['gray Image', 'gray scale inversion']
images = [img, result]
for i in range(2):
    plt.subplot(1, 2, i + 1)
    plt.imshow(images[i], 'gray')
    plt.title(titles[i])
    plt.xticks([]), plt.yticks([])
plt.show()
灰度上移(b=50,a=1)

在这里插入图片描述

# -- coding: utf-8 --
import cv2
import matplotlib.pyplot as plt
import numpy as np
from math import log10
img=cv2.imread("1.jpg",0)

# 获取图像高度和宽度
height = img.shape[0]
width = img.shape[1]

#创建一幅图像, uint8是专门用于存储各种图像的(包括RGB,灰度图像等),范围是从0–255
result = np.zeros((height, width), np.uint8)
result1 = np.zeros((height, width), np.uint8)
# 图像灰度反转
for i in range(height):
    for j in range(width):
        if (img[i, j])+50>255:
            gray = 255
        else:
            gray = (img[i, j])+50
        result[i, j] = np.uint8(gray)

# 显示图形
plt.figure(num='comparison')
titles = ['gray Image', 'brightness increased']
images = [img, result]
for i in range(2):
    plt.subplot(1, 2, i + 1)
    plt.imshow(images[i], 'gray')
    plt.title(titles[i])
    plt.xticks([]), plt.yticks([])
plt.show()

非线性点运算

对数变换(加亮减暗)

s=c*lg(1+r)

在这里插入图片描述

# -- coding: utf-8 --
import cv2
import matplotlib.pyplot as plt
import numpy as np
from math import log
img=cv2.imread("1.jpg",0)

img=np.double(img)
result=np.log10(img+1)
result= np.uint8(result*255/np.max(result))

plt.figure(num='comparison')
titles = ['gray Image', 'log']
images = [img, result]
for i in range(2):
    plt.subplot(1, 2, i + 1)
    plt.imshow(images[i], 'gray')
    plt.title(titles[i])
    plt.xticks([]), plt.yticks([])
plt.show()
幂次变换

在这里插入图片描述

r=0.5 , c=1

在这里插入图片描述

# -- coding: utf-8 --
import cv2
import matplotlib.pyplot as plt
import numpy as np
from math import log10
img=cv2.imread("1.jpg",0)

img=np.double(img)
result=img**0.5
result= np.uint8(result*255/np.max(result))

# 显示图形
plt.figure(num='comparison')
titles = ['gray Image', 'r=0.5']
images = [img, result]
for i in range(2):
    plt.subplot(1, 2, i + 1)
    plt.imshow(images[i], 'gray')
    plt.title(titles[i])
    plt.xticks([]), plt.yticks([])
plt.show()
r=2 , c=1

在这里插入图片描述

# -- coding: utf-8 --
import cv2
import matplotlib.pyplot as plt
import numpy as np
img=cv2.imread("1.jpg",0)

img=np.double(img)
result=img**2
result= np.uint8(result*255/np.max(result))
print(result)

plt.figure(num='comparison')
titles = ['gray Image', 'r=2']
images = [img, result]
for i in range(2):
    plt.subplot(1, 2, i + 1)
    plt.imshow(images[i], 'gray')
    plt.title(titles[i])
    plt.xticks([]), plt.yticks([])
plt.show()




  • 3
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wp猿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值