点运算是指将图像中每个像素点的灰度值按照一定的灰度变换关系逐像素变换的方法。
点操作定义
设输入图像的灰度为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()
文章出处登录后可见!
已经登录?立即刷新