欢迎访问 生活随笔!

凯发k8官方网

当前位置: 凯发k8官方网 > 人工智能 > 目标检测 >内容正文

目标检测

深度学习和目标检测系列教程 12-凯发k8官方网

发布时间:2024/10/8 目标检测 0 豆豆
凯发k8官方网 收集整理的这篇文章主要介绍了 深度学习和目标检测系列教程 12-300:常见的opencv的api和用法总结 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

@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)

总结

以上是凯发k8官方网为你收集整理的深度学习和目标检测系列教程 12-300:常见的opencv的api和用法总结的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得凯发k8官方网网站内容还不错,欢迎将凯发k8官方网推荐给好友。

网站地图