opencv计算机视觉_opencv是计算机视觉的至尊工具

opencv计算机视觉

什么是计算机视觉? (What is Computer Vision?)

Computer Vision is like imparting human intelligence and instincts to acomputer. In other words it is a field of computer science that works onenabling computers to see, identify and process images in the same waythat the human eye does.Let us say you and your family went on a vacation and you uploaded a fewpictures on Facebook. However, it takes time to find your parents’ facesand tag them in each picture, Facebook is intelligent enough to tag peoplefor you.But, how do you think this auto-tag feature works? It works throughcomputer vision.

计算机视觉就像赋予计算机以人类智慧和本能。 换句话说,这是计算机科学领域,它使计算机能够以与人眼相同的方式查看,识别和处理图像。让我们说您和您的家人去度假了,您在Facebook上上传了一些图片。 但是,查找父母的脸并在每张照片中标记它们需要花费时间,Facebook足够智能为您标记人物,但是您如何看待这种自动标记功能呢? 它通过计算机视觉起作用。

什么是OpenCV? (What is OpenCV?)

OpenCV was built to provide a common infrastructure for computer visionapplications and to accelerate the use of machine perception in commercialproducts. This library has more than 2500 algorithms used to detect andrecognize human faces, identify images, track moving objects, extract 3Dmodels of objects.

OpenCV构建旨在为计算机视觉应用程序提供通用的基础结构,并加速在商业产品中使用机器感知。 该库具有2500多种算法,可用于检测和识别人脸,识别图像,跟踪运动对象,提取对象的3D模型。

安装OpenCV (Installation of OpenCV)

To install OpenCV for python use the following code in terminal:

要在python中安装OpenCV,请在终端中使用以下代码:

$ python3 -m pip install opencv-python$ python3 -m pip install opencv-contrib-python

$ python3 -m pip安装opencv-python $ python3 -m pip安装opencv-contrib-python

计算机如何读取图像? (How does a computer read an image?)

Click from New York Square 从纽约广场点击

We can look at this image and figure it out that it belongs to New YorkSquare. But, computers cannot analyze it. It doesn’t have any intelligence.For any color image, there are 3 primary channels — red, green and blue.A matrix is formed for every primary color, and later, these matricescombine to provide a pixel value for the individual R, G, and B colors. Eachelement of the matrices provides data pertaining to the intensity of brightness of the pixel. It reads any image as a range of values between 0and 255.

我们可以查看一下这张图,并弄清楚它属于New YorkSquare。 但是,计算机无法对其进行分析。 它没有任何智能。对于任何彩色图像,都有3个原色通道-红色,绿色和蓝色。每种原色均形成一个矩阵,然后,这些矩阵组合起来为各个R,G提供像素值和B颜色。 矩阵的每个元素提供与像素的亮度强度有关的数据。 它读取任何图像作为0到255之间的值范围。

如何通过相机捕获图像和视频? (How to capture images and videos through a camera?)

import cv2

导入cv2

cap = cv2.VideoCapture(0)

上限= cv2.VideoCapture(0)

while True:

而True:

ret, frame = cap.read()

ret,frame = cap.read()

cv2.imshow(“Capturing”,frame)

cv2.imshow(“捕获”,帧)

if cv2.waitKey(1) & 0xFF == ord(‘q’):

如果cv2.waitKey(1)和0xFF == ord('q'):

break

打破

cap.release()

cap.release()

cv2.destroyAllWindows()

cv2.destroyAllWindows()

As seen in the above piece of code, we need to import the OpenCV library, cv2.VideoCapture() triggers the camera, cv2.imshow shows what the camera is capturing by opening a new window. cv2.waitKey makes the window static until the user presses a key.

如上面的代码所示,我们需要导入OpenCV库, cv2.VideoCapture() 触发摄像机, cv2.imshow 通过打开新窗口显示摄像机正在捕获的内容。 cv2.waitKey 使窗口保持静态,直到用户按下一个键。

OpenCV基本功能 (Basic functions of OpenCV)

Loading images using OpenCV and converting it into GrayScale:

使用OpenCV加载图像并将其转换为GrayScale:

import numpy as np

将numpy导入为np

import cv2

导入cv2

img = cv2.imread(‘Image123.png’,0) #write the name of an image

img = cv2.imread('Image123.png',0)#写入图片名称

cv2.imshow(‘image’,img)

cv2.imshow('image',img)

k = cv2.waitKey(0) & 0xFF

k = cv2.waitKey(0)和0xFF

if k == 27: # wait for ESC key to exit

如果k == 27:#等待ESC键退出

cv2.destroyAllWindows()

cv2.destroyAllWindows()

elif k == ord(‘s’): # wait for ‘s’ key to save and exit

elif k == ord('s'):#等待's'键保存并退出

cv2.imwrite(‘Firefox_wallpapergray.png’,img)

cv2.imwrite('Firefox_wallpapergray.png',img)

cv2.destroyAllWindows()

cv2.destroyAllWindows()

cv2.imread reads the selected image and the 0 parameter turns the image into grayscale, cv2.imshow shows the converted image.

cv2.imread 读取所选图像, 0 参数将图像变为灰度, cv2.imshow 显示转换后的图像。

Drawing and writing on an Image:

在图像上绘图和书写:

import numpy as np

将numpy导入为np

import cv2

导入cv2

img = cv2.imread(‘black.jpg’,cv2.IMREAD_COLOR)

img = cv2.imread('black.jpg',cv2.IMREAD_COLOR)

cv2.line(img,(0,0),(511,511),(255,0,0),5)

cv2.line(img,(0,0),(511,511),(255,0,0),5)

cv2.rectangle(img,(384,0),(510,128),(0,255,0),3)

cv2.rectangle(img,(384,0),(510,128),(0,255,0),3)

cv2.circle(img,(447,63), 63, (0,0,255), -1)

cv2.circle(img,(447,63),63,(0,0,255),-1)

font = cv2.FONT_HERSHEY_SIMPLEX

字体= cv2.FONT_HERSHEY_SIMPLEX

cv2.putText(img,’OpenCV’,(10,500), font, 4,(255,255,255),2,cv2.LINE_AA)

cv2.putText(img,'OpenCV',(10,500),字体,4,(255,255,255),2,cv2.LINE_AA)

cv2.imshow(‘image’,img)

cv2.imshow('image',img)

cv2.waitKey(0)

cv2.waitKey(0)

cv2.destroyAllWindows()

cv2.destroyAllWindows()

cv2.line draws the line with given coordinates on the image, cv2.rectangle, cv2.circle draws a rectangle and circle respectively and cv2.putText writes the given text, here we have used Hershey Simpex font.

cv2.line 在图像上绘制具有给定坐标的线, cv2.rectangle cv2.circle 分别绘制矩形和圆形, cv2.putText 编写给定的文本,这里我们使用了Hershey Simpex字体。

Output:

输出:

Output after drawing and writing on an image. 在图像上绘制和写入后输出。

Feature and Template Matching:

功能和模板匹配:

Template matching is basically the portion of one image matching another image.

模板匹配基本上是一个图像与另一个图像匹配的部分。

import cv2

导入cv2

import numpy as np

将numpy导入为np

img_bgr=cv2.imread(‘sc1.png’)

img_bgr = cv2.imread('sc1.png')

img_gray=cv2.cvtColor(img_bgr, cv2.COLOR_BGR2GRAY)

img_gray = cv2.cvtColor(img_bgr,cv2.COLOR_BGR2GRAY)

template=cv2.imread(‘sc2.png’,0)

template = cv2.imread('sc2.png',0)

w,h=template.shape[::-1]

w,h = template.shape [::-1]

res=cv2.matchTemplate(img_gray,template,cv2.TM_CCOEFF_NORMED)

res = cv2.matchTemplate(img_gray,template,cv2.TM_CCOEFF_NORMED)

threshold=0.8

阈值= 0.8

loc=np.where(res>=threshold)

loc = np.where(res> =阈值)

for pt in zip(*loc[::-1]):

用于zip(* loc [::-1])中的pt:

cv2.rectangle(img_bgr, pt, (pt[0]+w, pt[1]+h), (0,255,255),2)

cv2.rectangle(img_bgr,pt,(pt [0] + w,pt [1] + h),(0,255,255),2)

cv2.imshow(‘detected’,img_bgr)

cv2.imshow('检测到',img_bgr)

cv2.waitKey()

cv2.waitKey()

cv2.imread reads the selected image, cv2.cvtColor converts the image into Grayscale, w and h variables are the positions of x and y-axis, cv2.matchTemplate helps to match the common area of two images with threshold 80%, cv2.rectangle marks the area which is matching in the image.

cv2.imread 读取选定的图像, cv2.cvtColor 将图像转换为灰度,w和h变量是x和y轴的位置, cv2.matchTemplate 有助于将阈值80% cv2 的两个图像的公共区域进行匹配 。矩形 标记图像中匹配的区域。

Output:

输出:

Output of Template matching. 输出模板匹配。

Gradients and Edge Detection:

渐变和边缘检测:

import cv2

导入cv2

import numpy as np

将numpy导入为np

cap=cv2.VideoCapture(0)

cap = cv2.VideoCapture(0)

while True:

而True:

_, frame=cap.read()

_,frame = cap.read()

a= cv2.Laplacian(frame,cv2.CV_64F)

a = cv2.Laplacian(frame,cv2.CV_64F)

x= cv2.Sobel(frame,cv2.CV_64F,1 ,0, ksize=5)

x = cv2.Sobel(frame,cv2.CV_64F,1,0,ksize = 5)

y= cv2.Sobel(frame,cv2.CV_64F,0 ,1, ksize=5)

y = cv2.Sobel(frame,cv2.CV_64F,0,1,ksize = 5)

edge= cv2.Canny(frame, 100, 200)

edge = cv2.Canny(frame,100,200)

cv2.imshow(‘original’,frame)

cv2.imshow('原始',帧)

cv2.imshow(‘laplacian’,a)

cv2.imshow('laplacian',a)

cv2.imshow(‘sobelx’,x)

cv2.imshow('sobelx',x)

cv2.imshow(‘sobely’,y)

cv2.imshow('sobely',y)

cv2.imshow(‘edge’,edge)

cv2.imshow('edge',edge)

k=cv2.waitKey(5) & 0xFF

k = cv2.waitKey(5)和0xFF

if k == 27:

如果k == 27:

break

打破

cv2.destroyAllWindows()

cv2.destroyAllWindows()

cap.release()

cap.release()

cv2.Laplacian converts the image into the gradient, we use cv2.CV_64F as a standard label, cv2.Sobel converts into a horizontal and vertical gradient.

cv2.Laplacian 将图像转换为渐变,我们使用 cv2.CV_64F 作为标准标签 cv2.Sobel 转换为水平和垂直渐变。

Output:

输出:

Laplacian 拉普拉斯人
Vertical Gradient 垂直渐变
Edge Detection 边缘检测
Horizontal Gradient 水平渐变
Fingerprint matching and Edge detection of embryo from mother’s womb. 母亲子宫中的胚胎的指纹匹配和边缘检测。

Edge detection is pervasive in many applications such as fingerprint matching, medical diagnosis & license plate detection. These applications basically highlight the areas where image intensity changes drastically and ignore everything else.

边缘检测在许多应用中非常普遍,例如指纹匹配,医学诊断和车牌检测。 这些应用程序基本上突出了图像强度急剧变化的区域,而忽略了其他所有内容。

Edge Detection is also used in Self Driving Cars for lane detection:

边缘检测还用于自动驾驶汽车的车道检测:

Land detection of roads using OpenCV. 使用OpenCV对道路进行土地检测。

Other features of OpenCV: Motion Detection, Intrusion Detection, Haar Cascades, MOG background reduction, Homography, Corner Detection, Color Filtering, Thresholding, Image Arithmetics etc..

OpenCV其他功能:运动检测,入侵检测,Haar级联,MOG背景减少,单应性,角点检测,颜色过滤,阈值处理,图像算法等。

Statistical machine learning libraries used by OpenCV: Naive Bayes classifier, K-nearest neighbour algorithm, Decision tree learning, Meta- Algorithm, Random forest, Support vector machine, Deep & Convolutional neural networks.

OpenCV使用的统计机器学习库:朴素贝叶斯分类器,K近邻算法,决策树学习,元算法,随机森林,支持向量机,深度和卷积神经网络。

Some popular applications of OpenCV:

OpenCV一些流行应用程序:

Driver drowsiness detection (using a camera in a car) alerts the car driver through buzz or alarm.

驾驶员睡意检测(在汽车中使用摄像头)通过嗡嗡声或警报提醒汽车驾驶员。

Vehicle counting on highways (can be segregated into buses, cars, trucks) along with their speeds.

指望公路上的车辆(可以分为公共汽车,小汽车,卡车)及其速度。

Anomaly detection in the manufacturing process (the odd defective products).

生产过程中的异常检测(奇数次品)。

The ANPR called automatic number plate recognition is used to control vehicle tracing and counting number of people.

ANPR被称为自动车牌识别,用于控制车辆追踪和人数统计。

OpenCV is also used in imaging the data and provides better prediction and treatment to diseases like blood flow, x-ray images that are interpreted by humans.

OpenCV还用于对数据进行成像,并为人类解释的诸如血流,X射线图像等疾病提供更好的预测和治疗。

翻译自: https://medium.com/the-art-of-data/opencv-a-supreme-tool-for-computer-vision-f9c1b488bb81

opencv计算机视觉

你可能感兴趣的:(opencv,计算机视觉,python)