๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

๐Ÿค– Computer Vision

์ปดํ“จํ„ฐ ๋น„์ „ ์˜์ƒ์ฒ˜๋ฆฌ - ์˜ค์ธ„ ์•Œ๊ณ ๋ฆฌ์ฆ˜(Otsu Algorithm) ์‹ค์Šต ์ฝ”๋“œ C์–ธ์–ด, C++

์ด๋ก  

2023.07.25 - [๐Ÿค– Computer Vision] - ์˜์ƒ์ฒ˜๋ฆฌ - ์ด์ง„ํ™”์™€ ์˜ค์ธ„ ์•Œ๊ณ ๋ฆฌ์ฆ˜ (Otsu Algorithm)

 

์˜์ƒ์ฒ˜๋ฆฌ - ์ด์ง„ํ™”์™€ ์˜ค์ธ„ ์•Œ๊ณ ๋ฆฌ์ฆ˜ (Otsu Algorithm)

์ด์ง„ํ™” ๋ช…์•” ์˜์ƒ์„ ํ‘๊ณผ ๋ฐฑ์œผ๋กœ๋งŒ ์ด๋ฃจ์–ด์ง„ ์ด์ง„ ์˜์ƒ์œผ๋กœ ๋ฐ˜ํ™˜ํ•œ๋‹ค. T๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™์œผ๋ฉด 1(๋ฐฑ), ์ž‘์œผ๋ฉด 0(ํ‘)์œผ๋กœ ํ•ด์„œ ํ‘๋ฐฑ์˜์ƒ์„ ๋งŒ๋“ ๋‹ค. (์ด์ง„ํ™”๋ฅผ ์‹œํ‚จ๋‹ค) ์ž„๊ณ„๊ฐ’ ๋ฐฉ๋ฒ• ๋‘ ๋ด‰์šฐ๋ฆฌ ์‚ฌ์ด์˜ ๊ณ„

mcrkgus.tistory.com

 

 

 

์˜ค์ธ„ ์•Œ๊ณ ๋ฆฌ์ฆ˜ (Otsu Alhorithm) C์–ธ์–ด ์‹ค์Šต ์ฝ”๋“œ

์ „์ฒด ์ฝ”๋“œ : https://github.com/mcrkgus/Computer-Vision/blob/main/otsu.c

 

 

 #include <stdio.h>

#include <windows.h>
#pragma warning(disable:4996)


int main() {

	FILE* fp;

	unsigned char Readbuf[256][256];
	unsigned char Writebuf[256][256];
	unsigned long histogram[256] = { 0 };
	double histogram_n[256] = { 0 };
	double histogram_c[256];
	float scale = 256 * 256;

 
	fp = fopen("LENA_256x256_8bit.raw", "rb");

	for (int i = 0; i < 256; i++)
		fread(Readbuf[i], 1, 256, fp);

 	fclose(fp);

 

	//ํžˆ์Šคํ† ๊ทธ๋žจ
	for (int i = 0; i < 256; i++) {
		for (int j = 0; j < 256; j++) {
			histogram[Readbuf[i][j]]++;
		}
	}

 

	int result, wb, wf, ub, uf, u, max, temp;
	result = wb = wf = ub = uf = u = max = temp = 0;

 
	// ์˜ค์ธ„

	for (int i = 0; i < 256; i++) {
		for (int j = 0; j < i + 1; j++) { // background
			wb += histogram[j];  // weight
			ub = (double)(ub + j * histogram[i] / wb);  // mean
		}

		for (int j = i + 1; j < 256; j++) { // foreground
			wf += histogram[j];   // weight
			uf = (double)(uf + j * histogram[j] / wf);  // mean
		}

 		u = (wb * ub) + (wf * uf);  // total mean
		result = wb * wf * (uf - ub) * (uf - ub);  //between variance
		// result = (wb*(ub-u)*(ub-u)) + (wf*(uf-u)*(uf-u))

		if (result > max) {
			max = result;
			temp = i;
		}
	}

	//์ž„๊ณ„๊ฐ’ T 
 	int T = temp;
	printf("T = %d", T);


	//์˜์ƒ ์ด์ง„ํ™”
	for (int i = 0; i < 256; i++) {
		for (int j = 0; j < 256; j++) {
			if(Readbuf[i][j] > T)
				Writebuf[i][j] = 255;
			else
				Writebuf[i][j] = 0;
		}
	}

	

	//์˜์ƒ ์ €์žฅ
	fp = fopen("lena.raw", "wb");


	for (int i = 0; i < 256; i++)

		fwrite(Writebuf[i], 1, 256, fp);

 

	fclose(fp);

 

	return 0;

}



int otsh_threshold = 0;
double inter_class_variance = 0;
double calcBinary[256];
Mat dst; // ์‚ฌ์ „์— ์ •๊ทœํ™”๋œ ํžˆ์Šคํ† ๊ทธ๋žจ ํ–‰๋ ฌ

// 0 ~ 256 ์‚ฌ์ด์˜ ์ž„๊ณ„๊ฐ’ ๊ตฌํ•˜๊ธฐ
for (int i = 0; i < 256; i++) {
    int alpha = 0, beta = 0;
    int sum1 = 0, sum2 = 0;
    double avg1 = 0, avg2 = 0;

    // w0 ๊ณ„์‚ฐ (0 ~ t๊นŒ์ง€์˜ ๋ˆ„์ ํ•ฉ)
    for (int j = 0; j < i; j++)
        sum1 += calcBinary[j];

    // w1 ๊ณ„์‚ฐ (t + 1 ~ L - 1๊นŒ์ง€์˜ ๋ˆ„์ ํ•ฉ)
    sum2 = dst.rows * dst.cols - sum1;

    // w0๊ณผ w1์˜ ๊ฐ€์ค‘์น˜ ๊ตฌํ•˜๊ธฐ
    alpha = (double)sum1 / (double)calcBinary.size();
    beta = (double)sum2 / (double)calcBinary.size();

    // μ0๊ณผ μ1 ๊ตฌํ•˜๊ธฐ (w0๊ณผ w1์˜ ํ‰๊ท )
    for (int m = 0; m < i; m++)
        avg1 += (double)(m * calcBinary[m]) / (double)sum1;

    for (int m = i; m < 256; m++)
        avg2 += (double)(m * calcBinary[m]) / (double)sum2;

    // v0๊ณผ v1 ๊ตฌํ•˜๊ธฐ (๋ถ„์‚ฐ)
    double temp = alpha * beta * pow((avg1 - avg2), 2);

    // ๊ฐ€์žฅ ํฐ V between(t)๋ฅผ ์ž„๊ณ„๊ฐ’ T๋กœ ์ทจํ•œ๋‹ค.
    if (inter_class_variance < temp) {
        inter_class_variance = temp;
        otsh_threshold = i;
    }
}

 

 

 

728x90

'๐Ÿค– Computer Vision' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

์ปดํ“จํ„ฐ ๋น„์ „ ์˜์ƒ์ฒ˜๋ฆฌ - ์—์ง€ ๊ฐ•๋„์™€ ์—์ง€ ๋ฐฉํ–ฅ (๊ทธ๋ ˆ๋””์–ธํŠธ, ๊ฐ€์šฐ์‹œ์•ˆ, LOG ํ•„ํ„ฐ, ์˜๊ต์ฐจ ๊ฒ€์ถœ, ๋ผํ”Œ๋ผ์‹œ์•ˆ, Marr80 ์—์ง€ ๊ฒ€์ถœ ์•Œ๊ณ ๋ฆฌ์ฆ˜, ๋‹ค์ค‘ ์Šค์ผ€์ผ)  (2) 2023.10.06
์ปดํ“จํ„ฐ ๋น„์ „ ์˜์ƒ์ฒ˜๋ฆฌ - ์ปฌ๋Ÿฌ Color (HSV, HSB, HSI๋ชจ๋ธ), ์‹ค์Šต ์ฝ”๋“œ C์–ธ์–ด, C++  (0) 2023.10.02
์ปดํ“จํ„ฐ ๋น„์ „ ์˜์ƒ์ฒ˜๋ฆฌ - ์—์ง€ ๊ฒ€์ถœ (๋””์ง€ํ„ธ ์˜์ƒ์˜ ๋ฏธ๋ถ„, ๊ณ„๋‹จ ์—์ง€, ๋žจํ”„ ์—์ง€, ์Šค๋ฌด๋”ฉ ๊ธฐ๋ฒ•, ์†Œ๋ฒจ ์—ฐ์‚ฐ์ž, ์˜๊ต์ฐจ ์ฐพ๊ธฐ)  (0) 2023.09.11
์ปดํ“จํ„ฐ ๋น„์ „ ์˜์ƒ์ฒ˜๋ฆฌ - ์ปฌ๋Ÿฌ Color (RGB, CIE, CMY, YCbCr, HSI, HSV ๋ชจ๋ธ), ์‹ค์Šต ์ฝ”๋“œ Python, openCV  (2) 2023.08.29
์ปดํ“จํ„ฐ ๋น„์ „ ์˜์ƒ์ฒ˜๋ฆฌ - (ํŒฝ์ฐฝ, ์นจ์‹, ์—ด๊ธฐ, ๋‹ซ๊ธฐ) openCV Python ์ฝ”๋“œ  (1) 2023.08.22