基于OpenCV的图像强度操作
重磅干货,第一时间送达
更改任何通道中的像素值
对图像的数学运算
亮度变化
对比度变化
伽玛操纵
直方图均衡
图像预处理中的滤波等增强
使用OpenCV加载图像
import numpy as np
import cv2
from matplotlib import pyplot as plt
img = cv2.imread(folder_path + "imgs/chapter3/man.jpg", 0);
plt.imshow(img, cmap = "gray");
plt.show()
使用Opencv向输入图像添加常数
img = cv2.imread(folder_path + "imgs/chapter3/man.jpg", 0);
##########################FOCUS############################
img = cv2.add(img, 120);
###########################################################
plt.imshow(img, cmap = "gray");
plt.show()
使用Opencv减去常数以输入图像
img = cv2.imread(folder_path + "imgs/chapter3/man.jpg", 0);
##########################FOCUS############################
img = cv2.subtract(img, 120);
####################################################################
plt.imshow(img, cmap = "gray");
plt.show()
方法1
将图像拆分为其通道。
对于每个通道,计算其平均值。
从该通道中的每个像素中减去均值
方法2(用于深度学习)
将所有图像分割成各自的通道,对于所有图像的每个通道:
为每个图像找到该通道的均值。
查找所有计算出的均值的均值。
应用领域1:批标准化的一部分
# Grayscale image
import numpy as np
import cv2
from matplotlib import pyplot as plt
img = cv2.imread(folder_path + "imgs/chapter3/man.jpg", 0); # read the image as grayscale
# cv2.subtract
mean = np.mean(img);
img_out = cv2.subtract(img, mean);
f = plt.figure(figsize=(15,15))
f.add_subplot(1, 2, 1).set_title('Original Image');
plt.imshow(img, cmap = "gray");
f.add_subplot(1, 2, 2).set_title('Mean subtracted image');
plt.imshow(img_out, cmap = "gray");
plt.show()
应用领域1:负片
灰度负片
# negative of grayscale
import numpy as np
import cv2
from matplotlib import pyplot as plt
img = cv2.imread(folder_path + "imgs/chapter3/man.jpg", 0); # read the image as grayscale
#cv2.subtract
img_out = cv2.subtract(255, img);
f = plt.figure(figsize=(15,15))
f.add_subplot(1, 2, 1).set_title('Original Image');
plt.imshow(img, cmap = "gray");
f.add_subplot(1, 2, 2).set_title('Negative image');
plt.imshow(img_out, cmap = "gray");
plt.show()
RGB图像负片
# negative of rgb images
import numpy as np
import cv2
from matplotlib import pyplot as plt
img = cv2.imread(folder_path + "imgs/chapter3/castle.png", -1);
img_out = 255 - img;
f = plt.figure(figsize=(15,15))
f.add_subplot(1, 2, 1).set_title('Original Image');
plt.imshow(img[:,:,::-1]);
f.add_subplot(1, 2, 2).set_title('Negative image');
plt.imshow(img_out[:,:,::-1]);
plt.show()
直接融合
# Direct adding
import numpy as np
import cv2
from matplotlib import pyplot as plt
img1 = cv2.imread(folder_path + "imgs/chapter3/castle.png", -1);
img1 = cv2.resize(img1, (256, 256));
img2 = cv2.imread(folder_path + "imgs/chapter3/tessellate.jpg", -1);
img2 = cv2.resize(img2, (256, 256));
img_out = cv2.add(img1, img2);
f = plt.figure(figsize=(8,8))
plt.imshow(img_out[:,:,::-1]);
plt.show()
加权融合
# cv2.addWeighted
import numpy as np
import cv2
from matplotlib import pyplot as plt
img1 = cv2.imread(folder_path + "imgs/chapter3/castle.png", -1);
img1 = cv2.resize(img1, (256, 256));
img2 = cv2.imread(folder_path + "imgs/chapter3/tessellate.jpg", -1);
img2 = cv2.resize(img2, (256, 256));
# Like a faint watermark
img_out = cv2.addWeighted(img1,0.7,img2,0.3,0);
f = plt.figure(figsize=(8,8))
plt.imshow(img_out[:,:,::-1]);
plt.show()
import numpy as np
import cv2
from matplotlib import pyplot as plt
img1 = cv2.imread(folder_path + "imgs/chapter3/background.png", -1);
img2 = cv2.imread(folder_path + "imgs/chapter3/foreground.png", -1);
img_out = cv2.subtract(img2, img1);
f = plt.figure(figsize=(15,10))
f.add_subplot(2, 2, 1).set_title('Background Image');
plt.imshow(img1[:, :, ::-1])
f.add_subplot(2, 2, 2).set_title('Background image with some object');
plt.imshow(img2[:, :, ::-1])
f.add_subplot(2, 2, 3).set_title('Subtracted image');
plt.imshow(img_out[:, :, ::-1])
plt.show()
发光或反射光的质量或状态
亮度是一个相对术语。这取决于您的视觉感知。
亮度可以定义为光源相对于我们所比较的光源输出的能量。
import numpy as np
import cv2
from matplotlib import pyplot as plt
img = cv2.imread(folder_path + 'imgs/chapter3/outdoor.jpg')
value = 100 #Increase or decrease brightness by this value
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
hsv[:,:,2] = cv2.add(hsv[:,:,2], value)
img_out = cv2.cvtColor(hsv, cv2.COLOR_HSV2BGR)
f = plt.figure(figsize=(15,15))
f.add_subplot(1, 2, 1).set_title('Original Image');
plt.imshow(img[:, :, ::-1])
f.add_subplot(1, 2, 2).set_title('Brightness manipulated image');
plt.imshow(img_out[:, :, ::-1])
plt.show()
对比度是使对象(或其在图像或显示器中的表示形式)与众不同的亮度或颜色差异。
可视化为图像中最大和最小像素强度之间的差异。
对比度由同一视野内物体的颜色和亮度差异决定。
from PIL import Image, ImageEnhance
from matplotlib import pyplot as plt
img = Image.open(folder_path + "imgs/chapter3/outdoor.jpg");
enhancer = ImageEnhance.Contrast(img)
out = enhancer.enhance(factor=1.25)
f = plt.figure(figsize=(15,15))
f.add_subplot(2, 1, 1).set_title('Original Image');
plt.imshow(img)
f.add_subplot(2, 1, 2).set_title('Contrast manipulated image');
plt.imshow(out)
plt.show()
伽玛校正是一种用于对亮度进行编码和解码的非线性操作。
所有彩色和灰度数字图像文件都包含伽玛数据。
Gamma涉及在数字灵敏度和人眼灵敏度之间进行转换,一方面提供了许多优势,另一方面却增加了复杂性。
伽玛可优化中间调的对比度和亮度。
import numpy as np
import cv2
from matplotlib import pyplot as plt
img = cv2.imread(folder_path + 'imgs/chapter3/indoor.jpg')
# Apply Gamma=2.2 on the normalised image and then multiply by scaling constant (For 8 bit, c=255)
# Normalize image, pixels to 0-1
# Multiple by gamma
# Re-factor the image pixels to have value from 0-255
img_out = np.array(255*(img/255)**2.2,dtype='uint8')
f = plt.figure(figsize=(15,15))
f.add_subplot(2, 1, 1).set_title('Original Image');
plt.imshow(img[:, :, ::-1])
f.add_subplot(2, 1, 2).set_title('Gamma manipulated image');
plt.imshow(img_out[:, :, ::-1])
plt.show()
直方图
直方图显示事物发生频率的图表。
图像像素直方图表示具有特定强度值的像素的频率。
直方图均衡
直方图均衡用于增强对比度。
此方法增加了图像的整体对比度。
import numpy as np
import cv2
from matplotlib import pyplot as plt
img = cv2.imread(folder_path + "imgs/chapter3/outdoor.jpg", 0)
# Ravel falttens the 2d array to 1d array
# 256 - total number of bins
plt.hist(img.ravel(),256,[0,256])
plt.title("Histogram for original image")
plt.show()
import numpy as np
import cv2
from matplotlib import pyplot as plt
img = cv2.imread(folder_path + "imgs/chapter3/outdoor.jpg", 0)
# cv2.equalizeHist
img_out = cv2.equalizeHist(img)
f = plt.figure(figsize=(15,15))
f.add_subplot(2, 1, 1).set_title('Original Image');
plt.imshow(img, cmap="gray")
f.add_subplot(2, 1, 2).set_title('Histogram equalized image');
plt.imshow(img_out, cmap="gray")
plt.show()
代码链接:https://github.com/Tessellate-Imaging/monk_v1/blob/master/study_roadmaps/3_image_processing_deep_learning_roadmap/1_image_processing_basics/3) Image Intensity manipulation.ipynb