计算机视觉基础 - OpenCV3 关键运行机制汇总

2018-07-05  本文已影响0人  拓季

之前在 Udacity 的计算机视觉工程师的课程里接触了不少 OpenCV 的操作,但还是想更加系统的学一下,所以在通过 《Learning OpenCV 3 Computer Vision with Python》和其他资源进一步熟悉 OpenCV,前面这本书在出版社 Packt 的主页上支持免费下载,所以喜欢看原版书的可以自行下载。在这里零散的记录一些容易被忽略的要点,以备查看:

imread Flags
if img is None:
    raise Exception("Please check the availability of the image file!")
cap = cv2.VideoCapture(0) # Here 0 is the index of the video camera

# Check if the camera opened successfully
if cap.isOpened() == False:
    print("Unable to read camera feed")

# Get the frame size information for output usage
frame_width = int(cap.get(3)) # frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
frame_height = int(cap.get(4)) # frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))

# Get the frame per second information
fps = cap.get(cv2.CAP_PROP_FPS)
# Create a binary thresholded image
retval, binary = cv2.threshold(gray, 225, 255, cv2.THRESH_BINARY_INV)
dilation = cv2.dilate(binary, kernel, iterations=1)
erosion = cv2.erode(binary, kernel, iterations=1)
Morphological operations - erosion & dilation
opening = cv2.morphologyEx(image, cv2.MORPH_OPEN, kernel)
closing = cv2.morphologyEx(img, cv2.MORPH_CLOSE, kernel)

直方统计

直方图 Histogram 这一统计工具由于操作简单而又可以提供很多的信息,因而在计算机视觉中被得以广泛的应用。例如:通过对于图片上不同通道或灰度图片的强度 Intensity 的直方图统计可以获得关于图片色彩分布和对比度情况的信息,在 HOG, Histogram of Oriented Gradient 算法中通过统计图片中各个子区域的梯度信息来完成特征构建。

Histograms Equalization

当通过直方图对图片强度信息统计后会经常发现图片本身的强度信息并不是均匀分布的,而是在不同的强度区域有所集中。为了增加图片的对比度 contrast,可以通过执行直方均衡来改善图片的质量。这一变换更为一般的关系为:

img = cv2.imread('moutain.jpg', 0)
# Global histogram equalization
equalized = cv2.equalizeHist(img)
combined = np.hstack((img, equalized))
plt.imshow(combined, cmap='gray')
plt.xticks([]), plt.yticks([]);

执行 Histogram Equalization 前后的图片效果如下:

Equalized histogram
CLAHE (Contrast Limited Adaptive Histogram Equalization)

前述 HE 操作是针对图片的全局信息进行操作的,在实际使用中,有很多情况下仅考虑局部信息的直方均衡会取得更好的效果。CLAHE 在使用中仅考虑预先设定的图片区域(默认 8x8)内的直方统计情况,并且当某一区域内的对比度超过算法的设定值(默认 40)时,会对造成这个区域过高对比度的像素强度执行裁剪 clip ,最后再通过对分块区域的边缘进行插值过渡。

img = cv2.imread('moutain',0)
# create a CLAHE object (Arguments are optional).
clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8,8))
clahed = clahe.apply(img)
CLAHE before and after

Gamma 校正

Gamma 校正通过对输入图片的像素施加非线性变换而修改图片像素的亮度,也正是由于这个非线性变换关系,使得 Gamma 校正相比于对比度调整更加不容易造成饱和现象,其变换关系为:

When γ < 1, the original dark regions will be brighter and the histogram will be shifted to the right whereas it will be the opposite with γ > 1.

参考阅读

  1. Understanding Camera Histograms - Tones and Contrast

  2. Understanding Camera Histograms - Luminosity & Color

  3. Histogram equalization explaination from Wikipedia

  4. Histograms in OpenCV

  5. Understanding Gamma Correction

  6. Basic Image Transformation in OpenCV

上一篇 下一篇

猜你喜欢

热点阅读