图像缩放和合并算法

Posted qianbo_insist

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了图像缩放和合并算法相关的知识,希望对你有一定的参考价值。

show me the code

一个缩放一个合并,读者自己读吧


/*

  lrgb:   input 24bits rgb buffer
  srgb:   output 24bits rgb buffer
  width:  input width
  height: input height
  xscale: changed vector
  yscale: changed vector 
*/
//注意:钱波
//无论是正的还是反的都没有关系,不要在意,因为输入是一副图像,不要忘了这一点
uint8_t * scale(unsigned char *inrgb, int width, int height,
	float xscale, //摄像头画面x轴缩放
	float yscale, //摄像头画面Y轴缩放
	int &outw,
	int &outh,
	uint8_t * deskrgb, //叠加的大画布 
	int outrgbpitch   //大画布的pitch,一行占用像素,rgb24是 w*3
)

	int in = 0, out = 0;
	int ox, oy;     //the pixel site is after changed
	int rx, ry;     //the pixel site is before changed
	int temp = 0;   //turn site(x,y) to memory storage
	outw = width * xscale;      //after changed width
	outh = height * yscale;    //after changed height

	int pitch = outw * outh * 3;
	int memlen = pitch * outh;
	unsigned char *outrgb = new uint8_t[memlen];

										//rx = ox/xscale + 0.5;// out--to--input
										//ry = oy/yscale + 0.5;// out--to--input
	for (oy = 0; oy < outh; oy++)
	
		ry = (int)(oy / yscale + 0.5);
		if (ry >= height)
			ry--;
		temp = ry * width * 3;//origion pixel site of which width

		for (ox = 0; ox < outw; ox++)
		
			rx = (int)(ox / xscale + 0.5);
			if (rx >= width)
				rx--;
			in = temp + rx * 3;//change site(x,y) to storage

outrgb[out + 0] = inrgb[in + 0];
outrgb[out + 1] = inrgb[in + 1];
outrgb[out + 2] = inrgb[in + 2];
out += 3;
		
		//outrgb += outrgbpitch;
	

	return outrgb;

//合并rgb24,
//如果是摄像头是4:3 则取摄像头画面的16:9 
//注意,came画面是反的,desk是正的,所以,算法要倒算
void merge_rgb(uint8_t *desk, int sw, int sh, uint8_t *came,

	int cw,  //摄像头画面宽度
	int ch,  //摄像头画面高度
	float xscale, //摄像头画面x轴缩放
	float yscale) //摄像头画面y轴缩放


	int pitch = cw * 3;
	uint8_t * src = came + pitch *(ch - 1); //camera 的 最后一行

	//把画面移动到右上角,离顶50像素,离右边20像素
	int right_distance = cw*xscale * 3 + 20 * 3;
	uint8_t * dst = desk + (sw * 3 * 50 - right_distance);

	//裁剪16:9
	int tempdh = cw * 9 / 16;
	if (tempdh > ch)
		tempdh = ch;
	uint8_t *src1 = came + pitch *(ch - tempdh);
	int outw, outh;
	uint8_t * out = scale(src1, cw, tempdh, xscale, yscale, outw, outh, desk, sw * 3);

	src = out + outw * 3 * (outh - 1);

	//return;
	//取其中的dw ,dh 的小画面
	//开始缩放,直接内存开始读取
	//

	//把画面移动到中间位置
	//uint8_t * dst = desk + sw * 3 / 2 - dw * 3 / 2;
//omp_set_num_threads(4);
//#pragma omp parallel
	//
//#pragma omp for
	for (int i = 0; i < outh; i++)
	
#if 1
		memcpy(dst, src, outw * 3);
#else
		for (int j = 0; j < dw * 3; j++)
			*(dst + j) = *(dst + j)*0.3 + *(src + j)*0.7;
#endif
		src -= outw * 3;
		dst += sw * 3;
	
	delete[]out;
	//


int read_capture_mode()

	string lpPath = "./config_camera.ini";
	//int quality = GetPrivateProfileIntA("quality", "value", 31, lpPath.c_str());
	//int hencode = GetPrivateProfileIntA("hardware encode", "hencode", 0, lpPath.c_str());
	//int deskw = GetPrivateProfileIntA("desktop", "w", 0, lpPath.c_str());
	//int deskh = GetPrivateProfileIntA("desktop", "h", 0, lpPath.c_str());
	//char server[32];
	//GetPrivateProfileStringA("server", "ip", "127.0.0.1", server, 32, lpPath.c_str());
	int ret = 0;
	char capture[32];
	GetPrivateProfileStringA("capture", "video", "oneframe", capture, 32, lpPath.c_str());
	string s = capture;
	if (s.compare("oneframe") == 0)
	
		return 0;
	
	else if (s.compare("amcap_sample") == 0)
	
		return 1;
	
	return 0;

以上是关于图像缩放和合并算法的主要内容,如果未能解决你的问题,请参考以下文章

在 iOS 中以圆形格式裁剪和缩放图像

缩放图像小于 UIImageView

在GTK中缩放图像

Objective-C:从 UIScrollView 获取缩放的裁剪图像

学音视频一定要掌握这几个算法

带有缩放图像悬停功能的 Wordpress 图像插件