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);
}
图像的裁剪—首先获取图像的源点,根据源点裁出所需的图像大小
最新推荐文章于 2021-06-25 11:42:05 发布