@Author:Runsen

由于CV需要熟练使用opencv,因此总结了opencv常见的APi和用法。

OpenCV(opensourcecomputervision)于1999年正式推出,它来自英特尔的一项倡议。

  • OpenCV的核心是用C++编写的。在Python中,我们只使用一个包装器,它在Python内部执行C++代码。

  • 它对于几乎所有的计算机视觉应用程序都非常有用,并且在Windows、Linux、MacOS、Android、iOS上受支持,并绑定到Python、Java和Matlab。

锐化

USM锐化的全称是:Unsharp Mask,译为「模糊掩盖锐化处理」,是一种胶片时代处理图片锐度的手法,延续到数码时代的产物。在胶片时代,我们通过将模糊的负片与正片叠加可产生边缘锐化的效果。

对,锐化的效果离不开模糊,甚至可以说,锐化的效果就是来源于模糊。USM的锐化实际上就是利用原图和模糊图产生的反差,来实现锐化图片的效果。

公式:(源图像– w*高斯模糊)/(1-w);其中w表示权重(0.1~0.9)。

我感觉我喜欢上,毕业前在学校自拍的照片

import numpy as np
import matplotlib.pyplot as plt
import cv2image = cv2.imread('demo.jpg')
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)plt.figure(figsize=(20, 20))
plt.subplot(1, 2, 1)
plt.title("Original")
plt.imshow(image)# Create our shapening kernel
# the values in the matrix sum to 1
kernel_sharpening = np.array([[-1,-1,-1], [-1,9,-1], [-1,-1,-1]])# 对输入图像应用不同的内核
sharpened = cv2.filter2D(image, -1, kernel_sharpening)plt.subplot(1, 2, 2)
plt.title("Image Sharpening")
plt.imshow(sharpened)plt.show()

阈值化、二值化

image = cv2.imread('demo.jpg', 0)plt.figure(figsize=(30, 30))
plt.subplot(3, 2, 1)
plt.title("Original")
plt.imshow(image)# 小于127的值变为0(黑色,大于等于255(白色)
ret,thresh1 = cv2.threshold(image, 127, 255, cv2.THRESH_BINARY)plt.subplot(3, 2, 2)
plt.title("Threshold Binary")
plt.imshow(thresh1)# 模糊图像,消除噪音
image = cv2.GaussianBlur(image, (3, 3), 0)#  adaptiveThreshold
thresh = cv2.adaptiveThreshold(image, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 3, 5) plt.subplot(3, 2, 3)
plt.title("Adaptive Mean Thresholding")
plt.imshow(thresh)_, th2 = cv2.threshold(image, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)plt.subplot(3, 2, 4)
plt.title("Otsu's Thresholding")
plt.imshow(th2)plt.subplot(3, 2, 5)
# 高斯滤波后的大津阈值法
blur = cv2.GaussianBlur(image, (5,5), 0)
_, th3 = cv2.threshold(blur, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
plt.title("Guassian Otsu's Thresholding")
plt.imshow(th3)
plt.show()

降噪

image = cv2.imread('demo.jpg')
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)plt.figure(figsize=(20, 20))
plt.subplot(3, 2, 1)
plt.title("Original")
plt.imshow(image)# Let's define our kernel size
kernel = np.ones((5,5), np.uint8)# Now we erode
erosion = cv2.erode(image, kernel, iterations = 1)plt.subplot(3, 2, 2)
plt.title("Erosion")
plt.imshow(erosion)dilation = cv2.dilate(image, kernel, iterations = 1)
plt.subplot(3, 2, 3)
plt.title("Dilation")
plt.imshow(dilation)# Opening - Good for removing noise
opening = cv2.morphologyEx(image, cv2.MORPH_OPEN, kernel)
plt.subplot(3, 2, 4)
plt.title("Opening")
plt.imshow(opening)# Closing - Good for removing noise
closing = cv2.morphologyEx(image, cv2.MORPH_CLOSE, kernel)
plt.subplot(3, 2, 5)
plt.title("Closing")
plt.imshow(closing)

边缘检测与图像梯度

image = cv2.imread('demo.jpg')
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)height, width,_ = image.shape# Extract Sobel Edges
sobel_x = cv2.Sobel(image, cv2.CV_64F, 0, 1, ksize=5)
sobel_y = cv2.Sobel(image, cv2.CV_64F, 1, 0, ksize=5)plt.figure(figsize=(20, 20))plt.subplot(3, 2, 1)
plt.title("Original")
plt.imshow(image)plt.subplot(3, 2, 2)
plt.title("Sobel X")
plt.imshow(sobel_x)plt.subplot(3, 2, 3)
plt.title("Sobel Y")
plt.imshow(sobel_y)sobel_OR = cv2.bitwise_or(sobel_x, sobel_y)plt.subplot(3, 2, 4)
plt.title("sobel_OR")
plt.imshow(sobel_OR)laplacian = cv2.Laplacian(image, cv2.CV_64F)plt.subplot(3, 2, 5)
plt.title("Laplacian")
plt.imshow(laplacian)## 提供两个值:threshold1和threshold2。任何大于threshold2的梯度值。低于threshold1的任何值都不被视为边。
# threshold1和threshold2之间的值可以根据其大小分类为边或非边
# 在这种情况下,低于60的任何渐变值都被视为非边
# 而大于120的任何值都被视为边。
# The first threshold gradient
canny = cv2.Canny(image, 50, 120)plt.subplot(3, 2, 6)
plt.title("Canny")
plt.imshow(canny)

透视变换

image = cv2.imread('scan.jpg')
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)plt.figure(figsize=(20, 20))plt.subplot(1, 2, 1)
plt.title("Original")
plt.imshow(image)# 原始图像四个角的坐标
points_A = np.float32([[320,15], [700,215], [85,610], [530,780]])# 所需输出的4个角的坐标
# 使用A4纸的比例是1:1.41
points_B = np.float32([[0,0], [420,0], [0,594], [420,594]])# 使用两组四个点进行计算
# 透视变换矩阵,M
M = cv2.getPerspectiveTransform(points_A, points_B)warped = cv2.warpPerspective(image, M, (420,594))plt.subplot(1, 2, 2)
plt.title("warpPerspective")
plt.imshow(warped)

缩放、重新调整大小和插值

使用cv2.resize函数可以很容易地重新调整大小,它的参数有:cv2.resize(image,dsize(output image size),x scale,y scale,interpolation)

image = cv2.imread('demo.jpg')
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)plt.figure(figsize=(20, 20))plt.subplot(2, 2, 1)
plt.title("Original")
plt.imshow(image)# Let's make our image 3/4 of it's original size
image_scaled = cv2.resize(image, None, fx=0.75, fy=0.75)plt.subplot(2, 2, 2)
plt.title("Scaling - Linear Interpolation")
plt.imshow(image_scaled)# Let's double the size of our image
img_scaled = cv2.resize(image, None, fx=2, fy=2, interpolation = cv2.INTER_CUBIC)plt.subplot(2, 2, 3)
plt.title("Scaling - Cubic Interpolation")
plt.imshow(img_scaled)# Let's skew the re-sizing by setting exact dimensions
img_scaled = cv2.resize(image, (900, 400), interpolation = cv2.INTER_AREA)plt.subplot(2, 2, 4)
plt.title("Scaling - Skewed Size")
plt.imshow(img_scaled)

影像金字塔

在目标检测中缩放图像时非常有用。

image = cv2.imread('demo.jpg')
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)plt.figure(figsize=(20, 20))plt.subplot(2, 2, 1)
plt.title("Original")
plt.imshow(image)smaller = cv2.pyrDown(image)
larger = cv2.pyrUp(image)plt.subplot(2, 2, 2)
plt.title("Smaller")
plt.imshow(smaller)plt.subplot(2, 2, 3)
plt.title("Larger")
plt.imshow(larger)

裁剪

image = cv2.imread('demo.jpg')
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)plt.figure(figsize=(20, 20))plt.subplot(2, 2, 1)
plt.title("Original")
plt.imshow(image)height, width = image.shape[:2]# Let's get the starting pixel coordiantes (top  left of cropping rectangle)
start_row, start_col = int(height * .25), int(width * .25)# Let's get the ending pixel coordinates (bottom right)
end_row, end_col = int(height * .75), int(width * .75)# Simply use indexing to crop out the rectangle we desire
cropped = image[start_row:end_row , start_col:end_col]plt.subplot(2, 2, 2)
plt.title("Cropped")
plt.imshow(cropped)

模糊

image = cv2.imread('demo.jpg')
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)plt.figure(figsize=(20, 20))plt.subplot(2, 2, 1)
plt.title("Original")
plt.imshow(image)# Creating our 3 x 3 kernel
kernel_3x3 = np.ones((3, 3), np.float32) / 9# We use the cv2.fitler2D to conovlve the kernal with an image
blurred = cv2.filter2D(image, -1, kernel_3x3)plt.subplot(2, 2, 2)
plt.title("3x3 Kernel Blurring")
plt.imshow(blurred)# Creating our 7 x 7 kernel
kernel_7x7 = np.ones((7, 7), np.float32) / 49blurred2 = cv2.filter2D(image, -1, kernel_7x7)plt.subplot(2, 2, 3)
plt.title("7x7 Kernel Blurring")
plt.imshow(blurred2)

Contours

# Let's load a simple image with 3 black squares
image = cv2.imread('demo.jpg')
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)plt.figure(figsize=(20, 20))plt.subplot(2, 2, 1)
plt.title("Original")
plt.imshow(image)# Grayscale
gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)# Find Canny edges
edged = cv2.Canny(gray, 30, 200)plt.subplot(2, 2, 2)
plt.title("Canny Edges")
plt.imshow(edged)# Finding Contours
# Use a copy of your image e.g. edged.copy(), since findContours alters the image
contours, hierarchy = cv2.findContours(edged, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)plt.subplot(2, 2, 3)
plt.title("Canny Edges After Contouring")
plt.imshow(edged)print("Number of Contours found = " + str(len(contours)))# Draw all contours
# Use '-1' as the 3rd parameter to draw all
cv2.drawContours(image, contours, -1, (0,255,0), 3)plt.subplot(2, 2, 4)
plt.title("Contours")
plt.imshow(image)

深度学习和目标检测系列教程 12-300:常见的opencv的APi和用法总结相关推荐

  1. 深度学习和目标检测系列教程 1-300:什么是对象检测和常见的8 种基础目标检测算法

    @Author:Runsen 由于毕业入了CV的坑,在内卷的条件下,我只好把别人卷走. 对象检测 对象检测是一种计算机视觉技术,用于定位图像或视频中的对象实例.对象检测算法通常利用机器学习或深度学习来 ...

  2. 深度学习和目标检测系列教程 3-300:了解常见的目标检测的开源数据集

    @Author:Runsen 计算机视觉中具有挑战性的主题之一,对象检测,可帮助组织借助数字图片作为输入来理解和识别实时对象.大量的论文基于常见的目标检测的开源数据集而来,因此需要了解常见的目标检测的 ...

  3. 深度学习和目标检测系列教程 2-300:小试牛刀,使用 ImageAI 进行对象检测

    @Author:Runsen 对象检测是一种属于更广泛的计算机视觉领域的技术.它处理识别和跟踪图像和视频中存在的对象.目标检测有多种应用,如人脸检测.车辆检测.行人计数.自动驾驶汽车.安全系统等.Im ...

  4. 深度学习和目标检测系列教程 5-300:早期的目标检测RCNN架构

    @Author:Runsen 最早期的目标检测基于RCNN的算法,下面介绍RCNN的架构 RCNN架构 R-CNN 的目标是获取图像,并正确识别图片中的主要对象(通过边界框)的位置. 输入:图像: 输 ...

  5. 深度学习和目标检测系列教程 22-300:关于人体姿态常见的估计方法

    @Author:Runsen 姿态估计是计算机视觉中的一项流行任务,比如真实的场景如何进行人体跌倒检测,如何对手语进行交流. 作为人工智能(AI)的一个领域,计算机视觉使机器能够以模仿人类视觉为目的来 ...

  6. 深度学习和目标检测系列教程 7-300:先进的目标检测Faster R-CNN架构

    @Author:Runsen Faster R-CNN 由于Fast R-CNN 过程中仍然存在一个瓶颈,即ROI Projection.众所周知,检测对象的第一步是在对象周围生成一组潜在的边界框.在 ...

  7. 深度学习和目标检测系列教程 15-300:在 Python 中使用 OpenCV 执行 YOLOv3 对象检测

    @Author:Runsen 上次讲了yolov3,这是使用yolov3的模型通过opencv的摄像头来执行YOLOv3 对象检测. 导入所需模块: import cv2 import numpy a ...

  8. 深度学习和目标检测系列教程 9-300:TorchVision和Albumentation性能对比,如何使用Albumentation对图片数据做数据增强

    @Author:Runsen 上次对xml文件进行提取,使用到一个Albumentation模块.Albumentation模块是一个数据增强的工具,目标检测图像预处理通过使用"albume ...

  9. 深度学习和目标检测系列教程 21-300:deepsorts测试小车经过的时间和速度

    @Author:Runsen deepDeepSort DeepSort是一种用于跟踪目标的模型,为每个目标分配 ID,为每一个不同的类别分配label. 在DeepSort 中,过程如下. 使用YO ...

最新文章

  1. AutoCAD 2D与3D大师班学习教程 AutoCAD 2D and 3D Masterclass
  2. 四大传值详解:属性传值,单例传值,代理传值,block传值
  3. MySQL error 1477_mysql_error.md
  4. 深入mysql语言_MySQL对数据操作的一些深入语法
  5. C++:获取图片文件信息-图片名称、类型、像素宽高
  6. python语言的特点强制可读_python程序语言设计第二讲(笔记)
  7. 学习前端的阶段性总结
  8. R:应用时间序列分析--基于R(1)第一章 时间序列分析简介
  9. 解决github上的提交说明中文乱码的问题
  10. bzoj2101: [Usaco2010 Dec]Treasure Chest 藏宝箱
  11. JZOJ 5442 荒诞
  12. hp 服务器硬盘背板 供电线,100元淘了一个HP 6位硬盘笼+背板+线,准备用旧机改一个DSM。...
  13. pycharm远程连接服务器,同步代码,使用GPU
  14. 我的天呢!支付宝杀疯了!领取现金红包,太给力了吧,天天可领取!
  15. ubuntu 双显示屏设置 一个正常 一个不行的问题
  16. 小龙虾炒菜机器人_开挖掘机炒小龙虾?机器人餐厅?吃饭竟也能如此炫酷!
  17. 计算机网络(三十二)网络管理
  18. 智能制造、工业互联网、数字化转型哪家强?请收好这份榜单!
  19. STM32 串口下载hex 串口下载程序 CH340串口
  20. C++——虚函数、虚析构函数、纯虚函数、抽象类

热门文章

  1. TCP/IP / IP 头
  2. 启明云端分享|ESP32学习笔记参考GPIO口操作
  3. java大型wms架构设计_Java生鲜电商平台-库存管理设计与架构
  4. 职高学会计电算化好还是学计算机好,会计电算化主要学什么?
  5. 2019.02.07 bzoj4316: 小C的独立集(仙人掌+树形dp)
  6. php提交表单时判断 if($_POST[submit])与 if(isset($_POST[submit])) 的区别
  7. sqlserver中GUID的默认值设置
  8. libgdx学习记录27——线段与线段相交检测
  9. hust 1605 bfs
  10. MSSSQL 脚本收藏