图像的裁剪—首先获取图像的源点,根据源点裁出所需的图像大小

int CutOut(IplImage* src, IplImage* dst, int x, int y, int w,int h)
{
	//x,y为矩形框左上角点坐标,w为宽度,h为高度
	int width_src = src->widthStep;
	int height_src = src->height;
	byte* gray_src = (byte*)src->imageData;


	int width_dst = dst->widthStep;
	int height_dst = dst->height;
	byte* gray_dst = (byte*)dst->imageData;


	for (int i = 0; i < height_dst; i++)
		for (int j = 0; j < width_dst; j++)
		{
			gray_dst[i*width_dst + j] = gray_src[(y + i)*width_src + x + j];
		}
	return 0;


}
//提示信息
void CFirst_VersionDlg::OnBnClickedButton4()
{
	// TODO:  在此添加控件通知处理程序代码
	//Application->MessageBox(///"说明信息框///",///"说明信息框///",MB_HELP);


	IplImage *temp;
	CString path_Num;	


	CString path = "G:\\1 (";//编辑图片路径
	CString Last_Path;


	IplImage *image_Re = 0;
	int Max_Value = 0;
	double Pic_Value;


	int pos_X, pos_Y;
	for (int i = 2; i <= 2; i++)//原始图片有1000张,i=2只是做了一个测试
	{
		path_Num.Format("%d", i);
	//	image_Re = cvCreateImage(cvSize(680, 480), IPL_DEPTH_64F, 3);


		Last_Path = path + path_Num + ").jpg";
		temp = cvLoadImage(Last_Path, -1);


		int width = temp->widthStep;
		int height = temp->height;


		int half_height = height / 2;
		
		char *ptr = temp->imageData;
		//获取图片的高度	
		
		//cvShowImage("src", temp);
		byte* gray_src = (byte*)temp->imageData;
	
		//for (int i = 0; i < height_dst; i++)
		for (int j = 0; j < width; j++)
			{
				Pic_Value = gray_src[(half_height - 1)*width + j*4];//j*4和图像每个像素所占内存大小有关
				if (Pic_Value >= Max_Value)
				{
					Max_Value = Pic_Value;
					pos_X = j*4;
					pos_Y = half_height;
				}
			}

	}
	//设置目标大小为150*50                  裁剪的具体细节
	IplImage* Img_dst = cvCreateImage(cvSize(680, 680), temp->depth, temp->nChannels);
	//设置起始点坐标
	CutOut(temp, Img_dst, pos_X - 300, pos_Y - 300, Img_dst->widthStep, Img_dst->height);

	cvShowImage("cutout", Img_dst);
	cvWaitKey(0);
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值