OpenCv调整图像大小及仿射变换(旋转)

缩放:

procedure TFrmMain.BtnResizeClick(Sender: TObject);
var
  pImg: PIplImage;
  pDst: PIplImage;
begin
  if not OpenDialog1.Execute then Exit;
  pImg := cvLoadImage(PChar(OpenDialog1.FileName), CV_LOAD_IMAGE_GRAYSCALE);
  pDst := cvCreateImage(cvSize_(pImg.Width div 3, pImg.Height div 3), IPL_DEPTH_8U, 1);
  cvResize(pImg, pDst);
  cvSaveImage('C:\1.bmp', pDst);
  cvReleaseImage(pImg);
  cvReleaseImage(pDst);
end;

仿射变换:

procedure TFrmMain.BtnAffineClick(Sender: TObject);
var
  srcTri: array[0..2] of CvPoint2D32f;
  dstTri: array[0..2] of CvPoint2D32f;
  rot_mat, warp_mat: PCvMat;
  src, dst: PIplImage;
begin
  if not OpenDialog1.Execute then Exit;
  rot_mat := cvCreateMat(2, 3, CV_32FC1);
  warp_mat := cvCreateMat(2, 3, CV_32FC1);
  src := cvLoadImage(PChar(OpenDialog1.FileName), CV_LOAD_IMAGE_GRAYSCALE);
  dst := cvCloneImage(src);
  dst.Origin := src.Origin;
  cvZero(dst);

  srcTri[0].x := 0; srcTri[0].y := 0;
  srcTri[1].x := src.Width - 1; srcTri[1].y := 0;
  srcTri[2].x := 0; srcTri[2].y := src.Height - 1;
  dstTri[0].x := src.Width * 0.0; dstTri[0].y := src.Height * 0.33;
  dstTri[1].x := src.Width * 0.85; dstTri[1].y := src.Height * 0.25;
  dstTri[2].x := src.Width * 0.15; dstTri[2].y := src.Height * 0.7;

  cvGetAffineTransform(@srcTri[0], @dstTri[0], warp_mat);
  cvWarpAffine(src, dst, warp_mat, CV_INTER_LINEAR+CV_WARP_FILL_OUTLIERS, cvScalarAll(0));
  cvSaveImage('C:\1.bmp', dst);
  cvReleaseImage(src);
  cvReleaseImage(dst);
end;

旋转:

procedure TFrmMain.BtnRotateClick(Sender: TObject);
var
  center: CvPoint2D32f;
  angle, scale: Double;
  rot_mat: PCvMat;
  src, dst: PIplImage;
begin
  if not OpenDialog1.Execute then Exit;
  rot_mat := cvCreateMat(2, 3, CV_32FC1);
  src := cvLoadImage(PChar(OpenDialog1.FileName), CV_LOAD_IMAGE_GRAYSCALE);
  dst := cvCloneImage(src);
  dst.Origin := src.Origin;
  cvZero(dst);

  center.x := src.Width / 2;
  center.y := src.Height / 2;
  angle := -50.0;
  scale := 1; //缩放比例
  cv2DRotationMatrix(center, angle, scale, rot_mat);
  cvWarpAffine(src, dst, rot_mat, CV_INTER_LINEAR+CV_WARP_FILL_OUTLIERS, cvScalarAll(0));
  cvSaveImage('C:\1.bmp', dst);
  cvReleaseImage(src);
  cvReleaseImage(dst);
end;

原图:

OpenCv调整图像大小及仿射变换(旋转)_第1张图片

缩放:

仿射:

OpenCv调整图像大小及仿射变换(旋转)_第2张图片

旋转:

你可能感兴趣的:(c,image,div,DST)