📜  Python中的图像处理(缩放、旋转、移动和边缘检测)

📅  最后修改于: 2022-05-13 01:54:51.296000             🧑  作者: Mango

Python中的图像处理(缩放、旋转、移动和边缘检测)

拍照只是一个点击的问题,所以为什么玩它应该不仅仅是几行代码。似乎不是Python的情况。 Python中有很多很好的库可以用来处理图像,比如open-cv、Pillow等。在本文中,我们将使用Open CV,一个用于计算机视觉的开源库。它具有可用的 C++、 Python和Java接口。它针对计算机视觉领域的实时应用程序进行了高度优化(用 C/C++ 编写)。

让我们从一个简单的开始,即缩放图像。

缩放图像:-

缩放操作增加/减小图像的大小。

import cv2
import numpy as np
  
FILE_NAME = 'volleyball.jpg'
try:
    # Read image from disk.
    img = cv2.imread(FILE_NAME)
  
    # Get number of pixel horizontally and vertically.
    (height, width) = img.shape[:2]
  
    # Specify the size of image along with interploation methods.
    # cv2.INTER_AREA is used for shrinking, whereas cv2.INTER_CUBIC
    # is used for zooming.
    res = cv2.resize(img, (int(width / 2), int(height / 2)), interpolation = cv2.INTER_CUBIC)
  
    # Write image back to disk.
    cv2.imwrite('result.jpg', res)
  
except IOError:
    print ('Error while reading files !!!')

输出:

旋转图像:-
图像可以顺时针或以其他方式旋转到任意角度。我们只需要定义旋转矩阵,列出旋转点、旋转度数和缩放因子。

import cv2
import numpy as np
  
FILE_NAME = 'volleyball.jpg'
try:
    # Read image from the disk.
    img = cv2.imread(FILE_NAME)
  
    # Shape of image in terms of pixels.
    (rows, cols) = img.shape[:2]
  
    # getRotationMatrix2D creates a matrix needed for transformation.
    # We want matrix for rotation w.r.t center to 45 degree without scaling.
    M = cv2.getRotationMatrix2D((cols / 2, rows / 2), 45, 1)
    res = cv2.warpAffine(img, M, (cols, rows))
  
    # Write image back to disk.
    cv2.imwrite('result.jpg', res)
except IOError:
    print ('Error while reading files !!!')

输出:

翻译图像:-
翻译图像意味着在给定的参考框架内移动它。

import cv2
import numpy as np
  
FILE_NAME = 'volleyball.jpg'
# Create translation matrix.
# If the shift is (x, y) then matrix would be
# M = [1 0 x]
#     [0 1 y]
# Let's shift by (100, 50).
M = np.float32([[1, 0, 100], [0, 1, 50]])
  
try:
  
    # Read image from disk.
    img = cv2.imread(FILE_NAME)
    (rows, cols) = img.shape[:2]
  
    # warpAffine does appropriate shifting given the
    # translation matrix.
    res = cv2.warpAffine(img, M, (cols, rows))
  
    # Write image back to disk.
    cv2.imwrite('result.jpg', res)
  
except IOError:
    print ('Error while reading files !!!')

输出:

图像中的边缘检测:-
图像检测的过程包括检测图像中的锐利边缘。这种边缘检测在图像识别或对象定位/检测的上下文中是必不可少的。由于其广泛的适用性,有几种用于检测边缘的算法。我们将使用一种称为 Canny Edge Detection 的算法。

import cv2
import numpy as np
  
FILE_NAME = 'volleyball.jpg'
try:
    # Read image from disk.
    img = cv2.imread(FILE_NAME)
  
    # Canny edge detection.
    edges = cv2.Canny(img, 100, 200)
  
    # Write image back to disk.
    cv2.imwrite('result.jpg', edges)
except IOError:
    print ('Error while reading files !!!')

输出:

更多详情请参考 Github。