C# OpenCV 강좌 : 제 6강 - 회전
회전(Rotate)

영상이나 이미지를 회전시켜 띄울 수 있습니다.
90°, 45°, -45° 등 다양한 각도로 회전이 가능합니다.
원본(Source, src)은 영상이나 이미지를 사용합니다.
클래스 코드
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OpenCvSharp;
namespace Project
{
class OpenCV : IDisposable
{
IplImage rotate;
public IplImage Rotate(IplImage src, int angle)
{
rotate = new IplImage(src.Size, BitDepth.U8, 3);
CvMat matrix = Cv.GetRotationMatrix2D(Cv.Point2D32f(src.Width / 2, src.Height / 2), angle, 1);
Cv.WarpAffine(src, rotate, matrix, Interpolation.Linear, CvScalar.ScalarAll(0));
return rotate;
}
public void Dispose()
{
if (rotate != null) Cv.ReleaseImage(rotate);
}
}
} 세부 코드
public IplImage Rotate(IplImage src, int angle)
{
...
} Rotate()에서 영상을 대칭하게 됩니다.
rotate = new IplImage(src.Size, BitDepth.U8, 3);rotate에 메모리 확보를 위하여 선언합니다. IplImage(크기, 정밀도, 채널)을 입력합니다. 단색일 경우 채널은 1이며, 다색일 경우 채널은 3입니다.
CvMat matrix = Cv.GetRotationMatrix2D(Cv.Point2D32f(src.Width / 2, src.Height / 2), angle, 1);Cv.GetRotationMatrix2D()을 이용하여 화면을 회전합니다.
Cv.GetRotationMatrix2D(중심점(x, y), 각도, 스케일)을 설정합니다.
중심점은 영상이나 이미지를 회전시킬 좌표를 의미합니다.
스케일은 영상이나 이미지를 확대, 축소 시킬 크기를 의미합니다.
Cv.WarpAffine(src, rotate, matrix, Interpolation.Linear, CvScalar.ScalarAll(0));Cv.WarpAffine()을 이용하여 회전 결과를 생성합니다.
Cv.WarpAffine(원본, 결과, 배열, 보간법, 여백색상)을 의미합니다.
-
Tip :
Interpolation.Linear은 영상이나 이미지의 보간을 위해 보편적으로 사용되는 보간법입니다. -
Tip :
CvScalar.ScalarAll(0)는 여백을 검은색으로 채웁니다.
메인 코드
using (OpenCV Convert = new OpenCV())
{
pictureBoxIpl2.ImageIpl = Convert.Rotate(src, 90);
}Rotate()는 src 이외에도 angle 값을 추가로 받습니다. 영상이나 이미지를 회전시킬 각도를 입력합니다.
영상이나 이미지는 가로와 세로의 크기가 다른 경우가 많습니다. 이 경우 회전시킬시에 생기는 공백은 검은색으로 처리됩니다.
출력 결과
원본

45°

90°

-45°

공유하기
Kakao
Naver
Twitter
LinkedIn
Facebook
댓글 남기기