如今,圖像編輯變得越來越流行,因為手機有內置的功能,可以讓你裁剪、旋轉
和更多的操作你的圖像。
這篇文章中,我們將探索和學習這些圖像編輯技術。具體來說,我們將學習如何:
- 旋轉圖像
- 移動圖像
基本圖像變換操作
圖像旋轉和平移是圖像編輯中最基本的操作之一。兩者都屬于廣義的仿射變換。因此,在學習更復雜的轉換之前,你應該首先學習旋轉和平移圖像,使用OpenCV中可用的函數(shù)??纯聪旅娴膱D片,我們將在這里的所有轉換示例中使用它。
讓我們先看看下面的代碼,它將用于使用OpenCV執(zhí)行圖像旋轉。
Python
import cv2
# Reading the image
image = cv2.imread('image.jpg')
# dividing height and width by 2 to get the center of the image
height, width = image.shape[:2]
# get the center coordinates of the image to create the 2D rotation matrix
center = (width/2, height/2)
# using cv2.getRotationMatrix2D() to get the rotation matrix
rotate_matrix = cv2.getRotationMatrix2D(center=center, angle=45, scale=1)
# rotate the image using cv2.warpAffine
rotated_image = cv2.warpAffine(src=image, M=rotate_matrix, dsize=(width, height))
cv2.imshow('Original image', image)
cv2.imshow('Rotated image', rotated_image)
# wait indefinitely, press any key on keyboard to exit
cv2.waitKey(0)
# save the rotated image to disk
cv2.imwrite('rotated_image.jpg', rotated_image)
C++
#include <iostream>
#include<opencv2/opencv.hpp>
using namespace cv;
int main(int, char**)
{
Mat image = imread("image.jpg");
imshow("image", image);
waitKey(0);
double angle = 45;
// get the center coordinates of the image to create the 2D rotation matrix
Point2f center((image.cols - 1) / 2.0, (image.rows - 1) / 2.0);
// using getRotationMatrix2D() to get the rotation matrix
Mat rotation_matix = getRotationMatrix2D(center, angle, 1.0);
// we will save the resulting image in rotated_image matrix
Mat rotated_image;
// rotate the image using warpAffine
warpAffine(image, rotated_image, rotation_matix, image.size());
imshow("Rotated image", rotated_image);
// wait indefinitely, press any key on keyboard to exit
waitKey(0);
// save the rotated image to disk
imwrite("rotated_im.jpg", rotated_image);
return 0;
}
使用OpenCV進行圖像旋轉
通過定義一個變換矩陣M
,你可以將圖像旋轉一個特定的角度θ。這個矩陣通常是這樣的:
OpenCV提供了為圖像定義旋轉中心的能力,以及一個縮放因子來調整圖像的大小。在這種情況下,變換矩陣如下。
在上述矩陣中:
其中
c
x
c_x
cx?和
c
y
c_y
cy?是圖像旋轉所沿的坐標。
OpenCV
提供了getRotationMatrix2D()
函數(shù)來創(chuàng)建上面的轉換矩陣。
以下是創(chuàng)建2D旋轉矩陣的語法:
getRotationMatrix2D(center, angle, scale)
getRotationMatrix2D()
函數(shù)接受以下參數(shù):
- center:圖像的旋轉中心:
- angle: 旋轉角度:
- scale :一個各向同性的比例因子,根據提供的值將圖像向上或向下縮放
如果angle
是正的,圖像將逆時針方向旋轉。如果你想順時針旋轉圖像相同的量,那么角度需要是負的。
旋轉分為三步操作:
1. 首先,你需要得到旋轉的中心。這通常是你要旋轉的圖像的中心。
2. 接下來,創(chuàng)建2d旋轉矩陣。OpenCV提供了我們在上面討論過的getRotationMatrix2D()
函數(shù)。
3. 最后,使用在上一步中創(chuàng)建的旋轉矩陣對圖像應用仿射變換。OpenCV
中的warpAffine()
函數(shù)完成這項工作。
warpAffine()
函數(shù)的作用是:對圖像應用一個仿射變換。在進行仿射變換后,原圖像中所有的平行線在輸出圖像中也保持平行。
warpAffine()
的完整語法如下所示
warpAffine(src, M, dsize[, dst[, flags[, borderMode[, borderValue]]]])
下面是函數(shù)的參數(shù):
- src:原圖
- M:變換矩陣
- dsize:輸出圖像的大小
- dst:輸出圖像
- flags: 插值方法的組合如INTER_LINEAR或INTER_NEAREST
- borderMode:像素擴展方法
- borderValue:在邊界不變的情況下使用的值,默認值為0
注意:你可以在這里了解更多關于OpenCV仿射轉換的信息
旋轉后的圖片
使用OpenCV平移圖像
在計算機視覺中,圖像的平移意味著沿著x軸和y軸移動指定數(shù)量的像素。設圖像需要移動的像素為tx和ty,則可以定義一個平移矩陣M:
現(xiàn)在,在通過tx和ty值移動圖像時,有幾點你應該記住。
- 為tx提供正值將使圖像向右移動,而負值將使圖像向左移動。
- 同樣,ty值為正值時,圖像會向下平移,而ty值為負值時,圖像會向上平移。
按照以下步驟使用OpenCV平移圖片:
- 首先讀取圖像,獲得圖像的寬度和高度。
- 接下來,像旋轉一樣,創(chuàng)建一個變換矩陣,這是一個2D數(shù)組。這個矩陣包含沿x軸和y軸移動圖像所需的信息。
- 同樣,在旋轉中,在最后一步中使用
warpAffine()
函數(shù)來應用仿射轉換。
仔細閱讀這段代碼,看看它有多簡單:
Python
import cv2
import numpy as np
# read the image
image = cv2.imread('image.jpg')
# get the width and height of the image
height, width = image.shape[:2]
C++
#include "opencv2/opencv.hpp"
using namespace cv
// read the image
Mat image = imread("image.jpg");
// get the height and width of the image
int height = image.cols;
int width = image.rows;
在上面的代碼塊中,您讀取圖像并獲得其高度和寬度。
接下來,創(chuàng)建平移矩陣
。
Python
# get tx and ty values for translation
# you can specify any value of your choice
tx, ty = width / 4, height / 4
# create the translation matrix using tx and ty, it is a NumPy array
translation_matrix = np.array([
[1, 0, tx],
[0, 1, ty]
], dtype=np.float32)
C++
// get tx and ty values for translation
float tx = float(width) / 4;
float ty = float(height) / 4;
// create the translation matrix using tx and ty
float warp_values[] = { 1.0, 0.0, tx, 0.0, 1.0, ty };
Mat translation_matrix = Mat(2, 3, CV_32F, warp_values);
對于平移矩陣,如上所述,您將需要tx和ty。在本例中,您使用寬度和高度的四分之一作為平移值。我們建議您嘗試不同的值并研究它們的輸出。
現(xiàn)在,使用warpAffine()函數(shù)將平移矩陣應用到圖像上,就像對旋轉所做的那樣。
Python
# apply the translation to the image
translated_image = cv2.warpAffine(src=image, M=translation_matrix, dsize=(width, height))
C++
// save the resulting image in translated_image matrix
Mat translated_image;
// apply affine transformation to the original image using the translation matrix
warpAffine(image, translated_image, translation_matrix, image.size());
注意:warpAffine()
是一個通用函數(shù),可以用于對圖像應用任何類型的仿射變換。只要適當?shù)囟x矩陣M。
最后的代碼塊將顯示轉換后的圖像,并將其寫入磁盤。
Python
# display the original and the Translated images
cv2.imshow('Translated image', translated_image)
cv2.imshow('Original image', image)
cv2.waitKey(0)
# save the translated image to disk
cv2.imwrite('translated_image.jpg', translated_image)
C++文章來源:http://www.zghlxwxcb.cn/news/detail-454267.html
//display the original and the Translated images
imshow("Translated image", translated_image);
imshow("Original image", image);
waitKey(0);
// save the translated image to disk
imwrite("translated_image.jpg", translated_image);
下圖顯示了平移后的圖像。文章來源地址http://www.zghlxwxcb.cn/news/detail-454267.html
到了這里,關于Opencv 基礎(四):使用OpenCV進行圖像旋轉和平移的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網!