想了解Python中用于图像处理的OpenCV基础知识吗?

161 阅读3分钟

导语

OpenCV称为开源计算机视觉,是Python中用于计算机视觉和图像处理任务的库。

它有一个模块化的结构,包括几个共享的和静态的库。OpenCV可以应用于Python、C++、Java等语言中。

OpenCV的一些应用包括边缘检测、人脸检测、目标检测、人脸识别等。

​​

使用OpenCV,我们可以处理图像,从它们中提取所需的信息。OpenCV使用Numpy(Numpy),这是一个高度优化的数值操作库。

conda install -c anaconda opencv

正文

(1)阅读图片:

## Import OpenCV library
import cv2  
import numpy as np
# Load an image
img = cv2.imread(r"dogs.jpg", 1)
# Show image
cv2.imshow('image',img)  #1st arg - window name, 2nd - our image
cv2.waitKey(0)
cv2.destroyAllWindows()

​​

图像形式

旗子

 1

Cv2.IMREAD灰度

   灰度加载图像

 0

 以这样的方式加载图像,包括alpha通道

 -1

Cv2.warkey()是一个键盘绑定函数。它的参数是以毫秒为单位的时间。该函数等待任何键盘事件的指定毫秒。如果您在这段时间内按下任何键,程序将继续进行。如果0被传递,它将无限期地等待键笔画。

(2)获取图像细节:

print("Shape of image : ",img.shape) # returns a tuple of number of rows,columns, and channels
print("Sizeof Image : ",img.size) # returns total number of pixels is accessed
print("Datatype of image : ",img.dtype) # returns image dtype obtained

(3)调整大小和缩小图像:显示它需要很多处理:

import cv2 
# Load an image
img = cv2.imread(r"dogs.jpg", 1)

def rescaleFrame(frame,scale = 0.75):   # Rescale Image
    width = int(frame.shape[1] * scale)
    height = int(frame.shape[0] * scale)
    dimensions = (width, height)
    return cv2.resize(frame,dimensions,interpolation= cv2.INTER_AREA)  # Resize Image
frame_resized = rescaleFrame(img)
# Show image
cv2.imshow('Original image',img)
cv2.imshow('Resized image', frame_resized)
cv2.waitKey(0)
cv2.destroyAllWindows()

​​(4)绘制形状并向图像添加文本:

import numpy as np
import cv2
img = np.zeros([512, 512, 3], np.uint8) # draw a blank image
img = cv2.rectangle(img, (384, 0), (510, 128), (0, 0, 255),thickness= 4)  # Draw a rectangle
font = cv2.FONT_HERSHEY_SIMPLEX
cv2.putText(img, 'OpenCV', (10, 500), font, 4, (255, 255, 255), 2, cv2.LINE_AA) # Add text in image
cv2.imshow("shapes", img)
cv2.waitKey(0)
cv2.destroyAllWindows()

默认情况下,使用np.zeros()绘制的图像会绘制一个空白图像。

函数接受以下参数来绘制矩形。

2.cv2.putText()函数在图像中添加文本。它采用以下参数。

同样也可以画一个圆、月食、线和多条线。这里是他们的描述。

在本节中,我们将研究一些通常应用于图像数据的函数。这是建立机器学习和深度学习模型的预处理步骤。

(5)图像膨胀:图像膨胀会增加物体的面积。

img = cv2.imread(r"dog_1.jpg", 1)
# Blur Image
blur = cv2.GaussianBlur(img,(5,5),cv2.BORDER_DEFAULT)
# Edge Cascade
cascade = cv2.Canny(img,125,175)
# Edge cascade of Blurred image
canny = cv2.Canny(blur,125,175)
# Dilating the image
dilated = cv2.dilate(canny, (7,7), iterations = 4) # (7,7) - kernel size & iterations can be changed 

cv2.imshow('Original image',img)
cv2.imshow('Edge cascade',cascade)
cv2.imshow('Blurred image Edge cascade',canny)
cv2.imshow('Dilated image',dilated)
cv2.waitKey(0)
cv2.destroyAllWindows()

图像变换
平移基本上是图像沿x和y轴移动。
(6)翻动
翻转是镜像图像。我们可以垂直(0),水平(1),垂直和水平(-1)翻转图像。

import cv2 as cv
import numpy as np
img = cv.imread(r"dog_1.jpg", 1)
cv.imshow('Dog', img)

# Translation
def translate(img, x, y): # x,y specify the axis for translation
    transMat = np.float32([[1,0,x],[0,1,y]]) # translation matrix
    dimensions = (img.shape[1], img.shape[0]) # image width, height 
    return cv.warpAffine(img, transMat, dimensions)
translated = translate(img, -50, 50)
cv.imshow('Translated', translated)

# Rotation
def rotate(img, angle, rotPoint=None):  # angle of roation, rotation point as input
    (height,width) = img.shape[:2]
    if rotPoint is None:
        rotPoint = (width//2,height//2) # rotate around center
    rotMat = cv.getRotationMatrix2D(rotPoint, angle, 1.0)
    dimensions = (width,height)
    return cv.warpAffine(img, rotMat, dimensions)
rotated = rotate(img, -45) # clockwise rotation
cv.imshow('Rotated', rotated)
rotated_rotated = rotate(img, -90) # rotated image can again be rotated
cv.imshow('Rotated Rotated', rotated_rotated)

# Flipping
flip = cv.flip(img, -1)
cv.imshow('Flip', flip)
cv.waitKey(0)

总结

好啦!本次文章结束~

制作不易,记得一键三连哦!! 

如果需要新手安装包激活码、配套完整项目+源码笔记、更多Python资料 !

源码基地:【私信小编06】即可免费领取哦!!