컴퓨터 비전

OpenCV 히스토그램

Jagbbum 2023. 10. 19. 11:56

히스토그램

- 영상의 픽셀 값 분포를 그래프의 현태로 표현한 것

- 픽셀 값의 위치 정보는 알 수 없음

 

정규화된 히스토그램

- 히스토그램으로 구한 각 픽셀의 개수를 영상 전체 픽셀 개수로 나눈 것

 

OpenCV 히스토그램 계산 함수

void calcHist( const Mat* images, int nimages,
                          const int* channels, InputArray mask,
                          SparseMat& hist, int dims,
                          const int* histSize, const float** ranges,
                          bool uniform = true, bool accumulate = false );
Mat calcGrayHist(const Mat& img)
{
	CV_Assert(img.type() == CV_8U);

	Mat hist;
	int channels[] = { 0 };
	int dims = 1;
	const int histSize[] = { 256 };
	float graylevel[] = { 0, 256 };
	const float* ranges[] = { graylevel };

	calcHist(&img, 1, channels, noArray(), hist, dims, histSize, ranges);

	return hist;
}

히스토그램 스트레칭

- 영상의 히스토그램이 그레이스케일 전 구간에서 걸쳐 나타나도록 변경하는 선형 변환 기법

변환 함수의 직선의 방정식

기울기 : 255/(최대 - 최소)

y 절편 : -(255*최소/(최대 - 최소))

dst(x,y) = 255*((src(x,y) - 최소) / (최대 - 최소))

OpenCV 함수 - normalize


히스토그램 평활화

- 히스토그램이 그레이스케일 전체 구간에서 균인할 분포로 나타나도록 변경하는 명암비 향상 기법, 곡선 방정식 사용

int hist[256] = {};
for (int y = 0; y < src.rows; y++)
	for (int x = 0; x < src.cols; x++)
		hist[src.at<uchar>(y, x)]++;

int size = (int)src.total();
float cdf[256] = {};
cdf[0] = float(hist[0]) / size;
for (int i = 1; i < 256; i++)
	cdf[i] = cdf[i - 1] + float(hist[i]) / size;

for (int y = 0; y < src.rows; y++) {
	for (int x = 0; x < src.cols; x++) {
		dst.at<uchar>(y, x) = uchar(cdf[src.at<uchar>(y, x)] * 255);
	}
}

OpenCV 히스토그램 평활화

void equalizeHist(InputArray src, OutputArray dst);

 

스트레칭

 

평활화