python opencv 회전 그림 사용 방법

7067 단어 opencv회전 그림
배경
이미지 처리 에서 어떤 때 는 이미지 에 대해 각도 회전 처 리 를 할 수 있다.특히 컴퓨터 시각 에서 이미지 확장,회전 각도 확장 사진 은 흔히 볼 수 있 는 처리 이다.이런 회전 사진 의 응용 장면 도 비교적 많다.예 를 들 어 사용자 가 사진 을 올 릴 때 세로 로 처리 하기 어렵 고 후속 알고리즘 처 리 를 위해 회전 을 해 야 한다.흔히 볼 수 있 는 회전 처 리 는 두 가지 방식 이 있 는데 하 나 는 numpy 매트릭스 로 전환 한 후에 numpy 매트릭스 에 대해 처리 하 는 것 이 고 다른 하 나 는 opencv 자체 의 함수 로 각종 변환 처 리 를 하여 회전 각 도 를 실현 하 는 결과 이다.
원본 그림:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Fo40V1TN-1592548330569)(C:\Users\DELL-3020\AppData\Roaming\Typora\typora-user-images\image-20200618180728108.jpg)]
opencv 함수
회전 에서 자주 사용 하 는 함 수 는 다음 과 같은 몇 가지 함수 가 있다.cv2.transpose:이미지 매트릭스 변환 처리

img = cv2.imread(origin_img_path)
img_transpose = cv2.transpose(img)
cv2.imshow('transpose', img_transpose)
cv2.waitKey(0)
在这里插入图片描述 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)
在这里插入图片描述 cv2.getRotationMatrix2D:회전 행렬 M 을 구축 하고 후속 회전 시 회전 행렬 과 곱 하기 만 하면 회전 작업 을 완성 할 수 있 습 니 다.
회전 행렬 M
img

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 도
시계 반대 방향
  • opencv 함수 의 전환 작업+반전 작업 을 사용 하여 회전 을 실현 합 니 다
  • numpy.rot 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
    
    在这里插入图片描述
    시계 바늘
    
    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
    
    在这里插入图片描述
    회전 180 도,270 도
    사용numpy.rot90회전 180 도,270 도 실현
    180 도
    
    img180 = np.rot90(img, 2)
    cv2.imshow("rotate", img180)
    cv2.waitKey(0)
    
    在这里插入图片描述
    270 도
    
    img270 = np.rot90(img, 3)
    cv2.imshow("rotate", img270)
    cv2.waitKey(0)
    
    在这里插入图片描述
    임의의 각도 로 회전 하여 임의의 색 값 으로 배경 을 채 웁 니 다.
    
    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 도
    在这里插入图片描述
    60 도
    在这里插入图片描述
    레 퍼 런 스
    cv2.getRotationMatrix2D 블 로그 소개
    cv2.warpAffine 블 로그 소개
    numpy.rot90
    임 의 각도 회전
    python opencv 회전 그림 의 사용 방법 에 관 한 이 글 은 여기까지 소개 되 었 습 니 다.더 많은 python opencv 회전 그림 내용 은 우리 의 이전 글 을 검색 하거나 아래 의 관련 글 을 계속 찾 아 보 세 요.앞으로 많은 응원 바 랍 니 다!

    좋은 웹페이지 즐겨찾기