C语言旋转CHW的图像

#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>


/*	c = 3, h = 2, w = 3
	(0, 1, 2,           (2, 5
	3, 4, 5),           1, 4
	(6, 7, 8,      ==>  0, 3)
	9, 10, 11),         8, 11
	(12, 13, 14,        7, 10
	15, 16, 17),        6, 9,
	                    14, 17
						13, 16,
						12, 15
*/
static int rotate_left90(uint8_t *org_img, int height, int width, int channel)
{
	int rval = 0;
	int h = 0, w = 0, c = 0;
	int dst_h = 0, dst_w = 0;
	uint8_t *tmp_img = NULL;

	do {
		tmp_img = (uint8_t *)malloc(height * width * channel * sizeof(uint8_t));
		if (tmp_img == NULL) {
			rval = -1;
			break;
		}
		memcpy(tmp_img, org_img, height * width * channel * sizeof(uint8_t));

		dst_h = width;
		dst_w = height;

		for (c = 0; c < channel; c++) {
			for (h = 0; h < dst_h; h++) {
				for (w = 0; w < dst_w; w++) {
						org_img[c * dst_h * dst_w + h * dst_w + w] = \
							tmp_img[c * height * width + w * width + (width - 1 - h)];
				}
			}
		}
	} while(0);

	if (tmp_img) {
		free(tmp_img);
		tmp_img = NULL;
	}

	return rval;
}

/*	c = 3, h = 2, w = 3
	(0, 1, 2,           (3, 0
	3, 4, 5),           4, 1
	(6, 7, 8,      ==>  5, 2)
	9, 10, 11),         9, 6
	(12, 13, 14,        10, 7
	15, 16, 17),        11, 8,
	                    15, 12
						16, 13,
						17, 14
*/
static int rotate_right90(uint8_t *org_img, int height, int width, int channel)
{
	int rval = 0;
	int h = 0, w = 0, c = 0;
	int dst_h = 0, dst_w = 0;
	uint8_t *tmp_img = NULL;

	do {
		tmp_img = (uint8_t *)malloc(height * width * channel * sizeof(uint8_t));
		if (tmp_img == NULL) {
			rval = -1;
			break;
		}
		memcpy(tmp_img, org_img, height * width * channel * sizeof(uint8_t));

		dst_h = width;
		dst_w = height;

		for (c = 0; c < channel; c++) {
			for (h = 0; h < dst_h; h++) {
				for (w = 0; w < dst_w; w++) {
						org_img[c * dst_h * dst_w + h * dst_w + w] = \
							tmp_img[c * height * width + (height - 1 - w ) * width + h];
				}
			}
		}
	} while(0);

	if (tmp_img) {
		free(tmp_img);
		tmp_img = NULL;
	}

	return rval;
}

/*	c = 3, h = 2, w = 3
	(0, 1, 2,           (5, 4, 3,
	3, 4, 5),           2, 1, 0)
	(6, 7, 8,      ==>  (11, 10, 9
	9, 10, 11),         8, 7, 6)
	(12, 13, 14,        (17, 16, 15
	15, 16, 17),        14, 13, 12)
*/
static int rotate_right180(uint8_t *org_img, int height, int width, int channel)
{
	int rval = 0;
	int h = 0, w = 0, c = 0;
	int dst_h = 0, dst_w = 0;
	uint8_t *tmp_img = NULL;

	do {
		tmp_img = (uint8_t *)malloc(height * width * channel * sizeof(uint8_t));
		if (tmp_img == NULL) {
			rval = -1;
			break;
		}
		memcpy(tmp_img, org_img, height * width * channel * sizeof(uint8_t));

		dst_h = height;
		dst_w = width;

		for (c = 0; c < channel; c++) {
			for (h = 0; h < dst_h; h++) {
				for (w = 0; w < dst_w; w++) {
						org_img[c * dst_h * dst_w + h * dst_w + w] = \
							tmp_img[c * height * width + (height - 1 - h) * width + (width - 1 - w)];
				}
			}
		}
	} while(0);

	if (tmp_img) {
		free(tmp_img);
		tmp_img = NULL;
	}

	return rval;
}

static void fill_test_img(uint8_t *test_img, int height, int width, int channel)
{
	int i = 0, j = 0, c = 0;

	for (i = 0; i < height * width * channel; i++) {
		test_img[i] = i;
	}

	for (c = 0; c < channel; c++) {
		printf("[");
		for (i = 0; i < height; i++) {
			for (j = 0; j < width; j++) {
				printf("%d,", test_img[c * width * height + i * width + j]);
			}

			if (i == height - 1) {
				printf("]\n");
			} else {
				printf("\n");
			}
		}
	}
}

static void print_result(uint8_t *result_img, int height, int width, int channel)
{
	int i = 0, j = 0, c = 0;

	for (c = 0; c < channel; c++) {
		printf("[");
		for (i = 0; i < height; i++) {
			for (j = 0; j < width; j++) {
				printf("%d,", result_img[c * width * height + i * width + j]);
			}

			if (i == height - 1) {
				printf("]\n");
			} else {
				printf("\n");
			}
		}
	}
}

static int test_channel_3(void)
{
	// hwc; for test, h=2, w=3, c=3
	int height = 2;
	int width = 3;
	int channel = 3;
	uint8_t test_img[18];
	int i = 0, j = 0, k = 0;
	int new_w = 0, new_h = 0;
	int ret = 0;

	printf("\n\n=========== start rotate_left90 for channel_3 ===========\n");
	fill_test_img(test_img, height, width, channel);
	ret = rotate_left90(test_img, height, width, channel);
	assert(ret == 0);
	printf("After rotate_left90: \n");
	new_w = 2; new_h = 3;
	print_result(test_img, new_h, new_w, channel);


	printf("\n\n=========== start rotate_right90 for channel_3 ===========\n");
	fill_test_img(test_img, height, width, channel);
	ret = rotate_right90(test_img, height, width, channel);
	assert(ret == 0);
	printf("After rotate_right90: \n");
	new_w = 2; new_h = 3;
	print_result(test_img, new_h, new_w, channel);

	printf("\n\n=========== start rotate_right180 for channel_3 ===========\n");
	fill_test_img(test_img, height, width, channel);
	ret = rotate_right180(test_img, height, width, channel);
	assert(ret == 0);
	printf("After rotate_right180: \n");
	new_w = 3; new_h = 2;
	print_result(test_img, new_h, new_w, channel);

	return ret;
}

static int test_channel_1(void)
{
	// hwc; for test, h=2, w=3, c=1
	int height = 2;
	int width = 3;
	int channel = 1;
	uint8_t test_img[6];
	int new_w = 0, new_h = 0;
	int ret = 0;

	printf("\n\n=========== start rotate_left90 for channel_1 ===========\n");
	/*
		0, 1, 2,          2, 5,
		3, 4, 5,  ===>    1, 4,
						  0, 3,
	};*/
	fill_test_img(test_img, height, width, channel);
	ret = rotate_left90(test_img, height, width, channel);
	assert(ret == 0);
	printf("After rotate_left90: \n");
	new_w = 2; new_h = 3;
	print_result(test_img, new_h, new_w, channel);


	printf("\n\n=========== start rotate_right90 for channel_1 ===========\n");
	fill_test_img(test_img, height, width, channel);
	ret = rotate_right90(test_img, height, width, channel);
	assert(ret == 0);
	printf("After rotate_right90: \n");
	new_w = 2; new_h = 3;
	print_result(test_img, new_h, new_w, channel);

	printf("\n\n=========== start rotate_right180 for channel_1 ===========\n");
	fill_test_img(test_img, height, width, channel);
	ret = rotate_right180(test_img, height, width, channel);
	assert(ret == 0);
	printf("After rotate_right180: \n");
	new_w = 3; new_h = 2;
	print_result(test_img, new_h, new_w, channel);

	return ret;
}

int main()
{
	test_channel_1();

	test_channel_3();

	return 0;
}

结果

=========== start rotate_left90 for channel_1 ===========
[0,1,2,
3,4,5,]
After rotate_left90: 
[2,5,
1,4,
0,3,]


=========== start rotate_right90 for channel_1 ===========
[0,1,2,
3,4,5,]
After rotate_right90: 
[3,0,
4,1,
5,2,]


=========== start rotate_right180 for channel_1 ===========
[0,1,2,
3,4,5,]
After rotate_right180: 
[5,4,3,
2,1,0,]


=========== start rotate_left90 for channel_3 ===========
[0,1,2,
3,4,5,]
[6,7,8,
9,10,11,]
[12,13,14,
15,16,17,]
After rotate_left90: 
[2,5,
1,4,
0,3,]
[8,11,
7,10,
6,9,]
[14,17,
13,16,
12,15,]


=========== start rotate_right90 for channel_3 ===========
[0,1,2,
3,4,5,]
[6,7,8,
9,10,11,]
[12,13,14,
15,16,17,]
After rotate_right90: 
[3,0,
4,1,
5,2,]
[9,6,
10,7,
11,8,]
[15,12,
16,13,
17,14,]


=========== start rotate_right180 for channel_3 ===========
[0,1,2,
3,4,5,]
[6,7,8,
9,10,11,]
[12,13,14,
15,16,17,]
After rotate_right180: 
[5,4,3,
2,1,0,]
[11,10,9,
8,7,6,]
[17,16,15,
14,13,12,]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值