언어/C

C언어 영상처리 - Rotation

이게될까 2024. 4. 30. 18:09
728x90
728x90

이건 좌측 하단이 회전축 

void Rotation(double* y, int width, int height, double theta, BITMAPFILEHEADER bmpFile, BITMAPINFOHEADER bmpInfo) {
	theta = theta / 180 * PI;
	double* gy;
	gy = (double*)calloc(width * height, sizeof(double));

	for (int j = 0; j < height; j++) {
		for (int i = 0; i < width; i++) {
			int x2, y2;
			x2 = (int)(cos(theta) * i - sin(theta) * j);
			y2 = (int)(sin(theta) * i + cos(theta) * j);
			if (x2>=0 && x2 < width && y2 >= 0 && y2 < height) gy[y2 * width + x2] = y[j * width + i];

		}
	}

	makeOutFile(bmpFile, bmpInfo, "rotateOut.bmp", gy);
	free(gy);
}

 

이건 영상 중앙을 중심 좌표로 잡아 돌린다.

void Rotation(double* y, int width, int height, double theta, BITMAPFILEHEADER bmpFile, BITMAPINFOHEADER bmpInfo) {
	theta = theta / 180 * PI;
	double* gy;
	gy = (double*)calloc(width * height, sizeof(double));

	for (int j = 0; j < height; j++) {
		for (int i = 0; i < width; i++) {
			int x2, y2;
			x2 = (int)(cos(theta) * (i-width/2) - sin(theta) * (j-height/2) + width/2 );
			y2 = (int)(sin(theta) * (i - width / 2) + cos(theta) * (j - height / 2)+ height/2 );
			if (x2>=0 && x2 < width && y2 >= 0 && y2 < height) gy[y2 * width + x2] = y[j * width + i];

		}
	}

	makeOutFile(bmpFile, bmpInfo, "rotateOut.bmp", gy);
	free(gy);
}

 

이제 중간중간 난 구멍을 메꿨습니다. == filling hole

void Rotation(double* y, int width, int height, double theta, BITMAPFILEHEADER bmpFile, BITMAPINFOHEADER bmpInfo) {
	theta = theta / 180 * PI;
	double* gy;
	gy = (double*)calloc(width * height, sizeof(double));

	for (int j = 0; j < height; j++) {
		for (int i = 0; i < width; i++) {
			int x2, y2;
			x2 = (int)(cos(theta) * (i-width/2) - sin(theta) * (j-height/2) + width/2 );
			y2 = (int)(sin(theta) * (i - width / 2) + cos(theta) * (j - height / 2)+ height/2 );
			if (x2>=0 && x2 < width && y2 >= 0 && y2 < height) gy[y2 * width + x2] = y[j * width + i];

		}
	}
	for (int j = 0; j < height; j++) {
		for (int i = 0; i < width; i++) {
			if (gy[j * width + i] == 0) {
				if (i + 1 == width && gy[j * width + i - 1] != 0)gy[j * width + i] = gy[j * width + i - 1];
				else if (i + 1 != width && gy[j * width + i + 1] != 0) gy[j * width + i] = gy[j * width + i +1];
				
			}
		}
	}
	makeOutFile(bmpFile, bmpInfo, "rotateOut.bmp", gy);
	free(gy);
}
728x90