python opencv 회전 그림 사용 방법
이미지 처리 에서 어떤 때 는 이미지 에 대해 각도 회전 처 리 를 할 수 있다.특히 컴퓨터 시각 에서 이미지 확장,회전 각도 확장 사진 은 흔히 볼 수 있 는 처리 이다.이런 회전 사진 의 응용 장면 도 비교적 많다.예 를 들 어 사용자 가 사진 을 올 릴 때 세로 로 처리 하기 어렵 고 후속 알고리즘 처 리 를 위해 회전 을 해 야 한다.흔히 볼 수 있 는 회전 처 리 는 두 가지 방식 이 있 는데 하 나 는 numpy 매트릭스 로 전환 한 후에 numpy 매트릭스 에 대해 처리 하 는 것 이 고 다른 하 나 는 opencv 자체 의 함수 로 각종 변환 처 리 를 하여 회전 각 도 를 실현 하 는 결과 이다.
원본 그림:
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Fo40V1TN-1592548330569)(C:\Users\DELL-3020\AppData\Roaming\Typora\typora-user-images\image-20200618180728108.jpg)]](https://s1.md5.ltd/image/e2aaf7fd3ff55851b1246a8c3633e16d.jpg)
opencv 함수
회전 에서 자주 사용 하 는 함 수 는 다음 과 같은 몇 가지 함수 가 있다.
cv2.transpose
:이미지 매트릭스 변환 처리
img = cv2.imread(origin_img_path)
img_transpose = cv2.transpose(img)
cv2.imshow('transpose', img_transpose)
cv2.waitKey(0)
![在这里插入图片描述](https://s1.md5.ltd/image/946d8b2154c93f16cb234bba82072c79.jpg)
cv2.flip
:이미지 매트릭스 에 대해 반전 처 리 를 하고 매개 변 수 는 1,0,-1 로 설정 할 수 있 으 며 각각 수평 반전,수직 반전,수평 수직 반전 에 대응 합 니 다.
img = cv2.imread(origin_img_path)
img_flip = cv2.flip(img, 1)
cv2.imshow('flip', img_flip)
cv2.waitKey(0)
![在这里插入图片描述](https://s1.md5.ltd/image/4c130e96e2087ea0e9c6a9b4d456a81b.jpg)
cv2.getRotationMatrix2D
:회전 행렬 M 을 구축 하고 후속 회전 시 회전 행렬 과 곱 하기 만 하면 회전 작업 을 완성 할 수 있 습 니 다.회전 행렬 M
![img](https://s1.md5.ltd/image/9b8374fc0e72cf229f94429ac9361eb8.jpg)
img = cv2.imread(origin_img_path)
rows, cols = img.shape
# , ,
# ,
M = cv2.getRotationMatrix2D((cols/2,rows/2),45,0.6)
cv2.warpAffine
:그림 을 모방 하여 변환 합 니 다.보통 이동 또는 회전 작업 을 합 니 다.
img = cv2.imread(origin_img_path)
cv2.warpAffine(img, M,(lengh,lengh),borderValue=(255,255,255)) # M
numpy 함수numpy 회전 실현 은 일반적으로
numpy.rot90
그림 을 90 도 배수 로 회전 합 니 다.공식 소개:
numpy.rot90
(m, k=1, axes=(0, 1))[source]Rotate an array by 90 degrees in the plane specified by axes.
Rotation direction is from the first towards the second axis.
k: Number of times the array is rotated by 90 degrees.
관건 적 인 매개 변수
k
회전 90 도의 배 수 를 나타 내 고 k 의 수 치 는 보통 1,2,3 이 며 각각 회전 90 도,180 도,270 도 를 나타 낸다.k 도 마이너스,-1,-2,-3 을 취 할 수 있다.k.정 수 를 취하 면 시계 반대 방향 으로 회전 하고 음 수 를 취하 면 시계 방향 으로 회전 하 는 것 을 나타 낸다.회전 90 도
시계 반대 방향
def rotateAntiClockWise90(img_file): # 90
img = cv2.imread(img_file)
trans_img = cv2.transpose(img)
img90 = cv2.flip(trans_img, 0)
cv2.imshow("rotate", img90)
cv2.waitKey(0)
return img90
def totateAntiClockWise90ByNumpy(img_file): # np.rot90(img, -1) 90
img = cv2.imread(img_file)
img90 = np.rot90(img, -1)
cv2.imshow("rotate", img90)
cv2.waitKey(0)
return img90
![在这里插入图片描述](https://s1.md5.ltd/image/12ba9095d3ddeafd58f342de27809f3b.jpg)
시계 바늘
def rotateClockWise90(self, img_file):
img = cv2.imread(img_file)
trans_img = cv2.transpose( img )
img90 = cv2.flip(trans_img, 1)
cv2.imshow("rotate", img90)
cv2.waitKey(0)
return img90
def totateClockWise90ByNumpy(img_file): # np.rot90(img, 1) 90
img = cv2.imread(img_file)
img90 = np.rot90(img, 1)
cv2.imshow("rotate", img90)
cv2.waitKey(0)
return img90
![在这里插入图片描述](https://s1.md5.ltd/image/06757bfabc718462776e3896b8d5e84e.jpg)
회전 180 도,270 도
사용
numpy.rot90
회전 180 도,270 도 실현180 도
img180 = np.rot90(img, 2)
cv2.imshow("rotate", img180)
cv2.waitKey(0)
![在这里插入图片描述](https://s1.md5.ltd/image/3e533c7455e8d45f19e5fa011ade2db5.jpg)
270 도
img270 = np.rot90(img, 3)
cv2.imshow("rotate", img270)
cv2.waitKey(0)
![在这里插入图片描述](https://s1.md5.ltd/image/275fe36623958c01a159b34516faa433.jpg)
임의의 각도 로 회전 하여 임의의 색 값 으로 배경 을 채 웁 니 다.
import cv2
from math import *
import numpy as np
# angle , (255, 255, 255)
def rotate_bound_white_bg(image, angle):
# grab the dimensions of the image and then determine the
# center
(h, w) = image.shape[:2]
(cX, cY) = (w // 2, h // 2)
# grab the rotation matrix (applying the negative of the
# angle to rotate clockwise), then grab the sine and cosine
# (i.e., the rotation components of the matrix)
# -angle ; 1.0 scale ( ), 0.75
M = cv2.getRotationMatrix2D((cX, cY), -angle, 1.0)
cos = np.abs(M[0, 0])
sin = np.abs(M[0, 1])
# compute the new bounding dimensions of the image
nW = int((h * sin) + (w * cos))
nH = int((h * cos) + (w * sin))
# adjust the rotation matrix to take into account translation
M[0, 2] += (nW / 2) - cX
M[1, 2] += (nH / 2) - cY
# perform the actual rotation and return the image
# borderValue , ,
return cv2.warpAffine(image, M, (nW, nH),borderValue=(255,255,255))
# borderValue , (0, 0 , 0)
# return cv2.warpAffine(image, M, (nW, nH))
img = cv2.imread("dog.png")
imgRotation = rotate_bound_white_bg(img, 45)
cv2.imshow("img",img)
cv2.imshow("imgRotation",imgRotation)
cv2.waitKey(0)
45 도![在这里插入图片描述](https://s1.md5.ltd/image/a290d097285cf6dbc28d5d70c88996f9.jpg)
60 도
![在这里插入图片描述](https://s1.md5.ltd/image/671462413a0bf9a14078f4b05b8c1355.jpg)
레 퍼 런 스
cv2.getRotationMatrix2D 블 로그 소개
cv2.warpAffine 블 로그 소개
numpy.rot90
임 의 각도 회전
python opencv 회전 그림 의 사용 방법 에 관 한 이 글 은 여기까지 소개 되 었 습 니 다.더 많은 python opencv 회전 그림 내용 은 우리 의 이전 글 을 검색 하거나 아래 의 관련 글 을 계속 찾 아 보 세 요.앞으로 많은 응원 바 랍 니 다!
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
ip camera android에 액세스하고 java를 사용하여 모니터에 표시그런 다음 PC에서 다운로드 폴더를 추출해야 합니다 그런 다음 프로젝트 폴더에 다운로드한 javacv 라이브러리를 추가해야 합니다. 먼저 라이브러리 폴더를 마우스 오른쪽 버튼으로 클릭한 다음 jar/폴더 추가를 선택...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.