Quay ảnh trong EmguCV 3.0
Tốn thời gian quá, cuối cùng cũng quay được ảnh:
RotationMatrix2D mat_ro; mat_ro = new RotationMatrix2D(card.Center, card.Angle, 1); Mat dst = BinaryFrame.Clone(); CvInvoke.WarpAffine(BinaryFrame, dst, mat_ro, new Size(400, 200));
// Bổ sung hàm quay ảnh để sử dụng
public static Mat RotateImage(Mat image, float angle)
{
RotationMatrix2D mat_ro;
Size size = new Size(image.Width,image.Height);
PointF center = new PointF((float)image.Width / 2, (float)image.Height / 2);
mat_ro = new RotationMatrix2D(center, angle, 1);
Mat dst = image.Clone();
CvInvoke.WarpAffine(image, dst, mat_ro, size);
return dst;
}
public static Mat RotateImageZoom(Mat image, float angle)
{
float offsetX, offsetY;
float width = image.Width;
float height = image.Height;
PointF center = new PointF( width / 2, height / 2);
RotationMatrix2D mat_ro;
Size size = new Size(image.Width , image.Height);
RotatedRect boundrs = new RotatedRect(center, size, angle);
Rectangle bounds = boundrs.MinAreaRect();
Mat dst = new Mat((int)bounds.Size.Width, (int)bounds.Size.Height, DepthType.Cv8U, 3);
offsetX = ((int)bounds.Size.Width - width) / 2;
offsetY = ((int)bounds.Size.Height - height) / 2;
center =new PointF(center.X+ offsetX,center.Y+ offsetY);
//boder
CvInvoke.CopyMakeBorder(image, dst, (int)offsetY, (int)offsetY, (int)offsetX, (int)offsetX, BorderType.Constant);
// img.copyTo(resized(roi));
mat_ro = new RotationMatrix2D(center, angle, 1);
//dst = image.Clone();
CvInvoke.WarpAffine(dst, dst, mat_ro, dst.Size);
return dst;
}
Bình luận gần đây