提高计算机视觉任务的图像质量
AI算法与图像处理
共 6462字,需浏览 13分钟
·
2021-10-26 16:24
点击下方“AI算法与图像处理”,一起进步!
重磅干货,第一时间送达
二值化/阈值化 降噪 纠偏 重新缩放 形态学操作 为了尝试这些操作,我们将使用Python3语言及其两个库, Pillow 和OpenCV。
二值化
## import dependencies
import cv2
from PIL import Image
import matplotlib.pyplot as plt
## reading image
img = cv2.imread('text_document.jpg',0)
## apply binary thresholding
ret,thresh1 = cv2.threshold(img,170,255,cv2.THRESH_BINARY)
## plot original and binarised image
titles = ['Original Image', 'Binary Thresholding']
images = [img, thresh1]
for i in range(2):
plt.figure(figsize=(20,20))
plt.subplot(2,3,i+1),plt.imshow(images[i],'gray',vmin=0,vmax=255)
plt.title(titles[i])
plt.xticks([]),plt.yticks([])
自适应阈值均值:阈值是平均值附近区域减去固定的Ç。 自适应高斯阈值:阈值是邻域值减去常数C的高斯加权总和。
## import dependencies
import cv2
from PIL import Image
import matplotlib.pyplot as plt
## reading image
img = cv2.imread('lighting_conditions.jpg', 0)
## apply adaptive thresholding
## adaptive mean thresholding
th1 = cv2.adaptiveThreshold(img,255,cv2.ADAPTIVE_THRESH_MEAN_C,
cv2.THRESH_BINARY,11,2)
## adaptive gaussian thresholding
th2 = cv2.adaptiveThreshold(img,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
cv2.THRESH_BINARY,11,2)
## plot original and binarised image
titles = ['Original Image', 'Adaptive Mean Thresholding', 'Adaptive Gaussian Thresholding']
images = [img, th1, th2]
plt.figure(figsize=(20,20))
for i in range(3):
plt.subplot(2,3,i+1),plt.imshow(images[i],'gray',vmin=0,vmax=255)
plt.title(titles[i])
plt.xticks([]),plt.yticks([])
## import dependencies
import cv2
from PIL import Image
import matplotlib.pyplot as plt
## reading image
img = cv2.imread('lighting_conditions.jpg', 0)
## apply Otru's thresholding
ret3,th1 = cv2.threshold(img,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
## plot original and binarised image
titles = ["Original Image", "Binary Otsu's Thresholding"]
images = [img, th1]
plt.figure(figsize=(20,20))
for i in range(2):
plt.subplot(2,3,i+1),plt.imshow(images[i],'gray',vmin=0,vmax=255)
plt.title(titles[i])
plt.xticks([]),plt.yticks([])
降噪
## import dependencies
import cv2
from PIL import Image
import matplotlib.pyplot as plt
## reading image
img = cv2.imread('noisy_image.jpg')
## apply image denoising
dst = cv2.fastNlMeansDenoisingColored(img,None,10,10,7,21)
## plot original and denoised image
titles = ["Original Image", "Denoised Image"]
images = [img, dst]
plt.figure(figsize=(20,20))
for i in range(2):
plt.subplot(2,3,i+1),plt.imshow(images[i],'gray',vmin=0,vmax=255)
plt.title(titles[i])
plt.xticks([]),plt.yticks([])
纠偏
重新缩放
## import dependencies
import cv2
from PIL import Image
import numpy as np
import matplotlib.pyplot as plt
## reading image
img = Image.open('noisy_image.jpg')
## apply image rescaling and making image 300x300 (downscaling)
dst = img.resize((50,50))
## plot original and downscaled image
titles = ["Original Image", "Rescaled Image"]
images = [np.asarray(img), np.asarray(dst)]
plt.figure(figsize=(20,20))
for i in range(2):
plt.subplot(2,3,i+1),plt.imshow(images[i],'gray',vmin=0,vmax=255)
plt.title(titles[i])
plt.xticks([]),plt.yticks([])
形态学操作
## import dependencies
import cv2
from PIL import Image
import matplotlib.pyplot as plt
## reading image
img = cv2.imread('text_document.jpg', 0)
## apply erosion
kernel = np.ones((5,5),np.uint8)
erosion = cv2.erode(img,kernel,iterations = 1)
## plot original and eroded image
titles = ["Original Image", "Eroded Image"]
images = [img, erosion]
plt.figure(figsize=(20,20))
for i in range(2):
plt.subplot(2,3,i+1),plt.imshow(images[i],'gray',vmin=0,vmax=255)
plt.title(titles[i])
plt.xticks([]),plt.yticks([])
## import dependencies
import cv2
from PIL import Image
import matplotlib.pyplot as plt
## reading image
img = cv2.imread('text_document.jpg', 0)
## apply dilation
kernel = np.ones((5,5),np.uint8)
dilation = cv2.dilate(img,kernel,iterations = 1)
## plot original and dilated image
titles = ["Original Image", "Dilated Image"]
images = [img, dilation]
plt.figure(figsize=(20,20))
for i in range(2):
plt.subplot(2,3,i+1),plt.imshow(images[i],'gray',vmin=0,vmax=255)
plt.title(titles[i])
plt.xticks([]),plt.yticks([])
结论
交流群
欢迎加入公众号读者群一起和同行交流,目前有美颜、三维视觉、计算摄影、检测、分割、识别、医学影像、GAN、算法竞赛等微信群
个人微信(如果没有备注不拉群!) 请注明:地区+学校/企业+研究方向+昵称
下载1:何恺明顶会分享
在「AI算法与图像处理」公众号后台回复:何恺明,即可下载。总共有6份PDF,涉及 ResNet、Mask RCNN等经典工作的总结分析
下载2:终身受益的编程指南:Google编程风格指南
在「AI算法与图像处理」公众号后台回复:c++,即可下载。历经十年考验,最权威的编程规范!
下载3 CVPR2021 在「AI算法与图像处理」公众号后台回复:CVPR,即可下载1467篇CVPR 2020论文 和 CVPR 2021 最新论文
评论