1. 程式人生 > >影象的裁剪—首先獲取影象的源點,根據源點裁出所需的影象大小

影象的裁剪—首先獲取影象的源點,根據源點裁出所需的影象大小

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);
}