C# OpenCV 강좌 : 제 10강 - 그레이스케일
그레이스케일(GrayScale)
영상이나 이미지의 색상을
흑백
색상으로 변환하기 위해서 사용합니다.
원본(Source, src)
를 영상이나 이미지를 사용하면 됩니다.
영상 사용하기
: 3강 바로가기
이미지 사용하기
: 4강 바로가기
Class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OpenCvSharp;
namespace test
{
class OpenCV : IDisposable
{
IplImage gray;
public IplImage GrayScale(IplImage src)
{
gray = new IplImage(src.Size, BitDepth.U8, 1);
Cv.CvtColor(src, gray, ColorConversion.BgrToGray);
return gray;
}
public void Dispose()
{
if (gray != null) Cv.ReleaseImage(gray);
}
}
}
Class Code
public IplImage GrayScale(IplImage src)
{
...
}
GrayScale
에서 영상을 흑백
으로 변하게 됩니다.gray = new IplImage(src.Size, BitDepth.U8, 1);
gray
에 메모리 확보를 위하여 선언합니다. IplImage(크기, 정밀도, 채널)
을 입력합니다. 단색일 경우 채널은 1
이며, 다색일 경우 채널은 3
입니다.
- Tip :
GrayScale
은 단색이기 때문에 채널은1
입니다.
Cv.CvtColor(src, gray, ColorConversion.BgrToGray);
Cv.CvtColor()
을 이용하여 변환될 색상
을 설정합니다. Cv.CvtColor(원본, 결과, 변환)
을 의미합니다. ColorConversion.BgrToGray
를 이용하여 Bgr색상
을 Gray색상
으로 변환합니다.
- Tip :
ColorConversion.*
을 이용하여 다른 변환도 가능합니다.
Main Code
using (OpenCV Convert = new OpenCV())
{
pictureBoxIpl2.ImageIpl = Convert.GrayScale(src)) ;
}
GrayScale
은 흑백
의 이미지를 표시합니다.
Result
GrayScale
BgrToCrcb (채널 : 3)
BgrToLab (채널 : 3)
BgrToLuv (채널 : 3)
BgrToXyz (채널 : 3)
BgrToYuv (채널 : 3)
도움이 되셨다면 광고 클릭 부탁드립니다.