public ImageProcess(ImageIO sourceImage, int targetWidth, int targetHeight, Interpolation interpolation, double bicubicFactor = -0.5) { _imageParameter = new ImageParameter(sourceImage, null, sourceImage.Path, null); sourceImage.LockBits(); _processOption = new ProcessOption(Transform.None, interpolation); _distortionParameter = null; _warpParameter = null; _faceTransformation = null; _interpolationParameter = new InterpolationParameter(targetWidth, targetHeight, bicubicFactor); _processResult = new ProcessResult(this.InterpolationFunction(), null, null); sourceImage.UnlockBits(); }
public ImageProcess(ImageIO sourceImage, ImageIO faceImage, int targetWidth, int targetHeight, Interpolation interpolation, double bicubicFactor = -0.5) { _imageParameter = new ImageParameter(sourceImage, faceImage, sourceImage.Path, faceImage.Path); sourceImage.LockBits(); faceImage.LockBits(); _processOption = new ProcessOption(Transform.TPS, interpolation); _distortionParameter = null; _warpParameter = null; _faceTransformation = new FaceTransformation(this.ImageParameter.SourcePath, this.ImageParameter.FacePath); _interpolationParameter = new InterpolationParameter(targetWidth, targetHeight, bicubicFactor); _processResult = new ProcessResult(this.InterpolationFunction(), this.FaceTransformation.Source_marked, this.FaceTransformation.Face_marked); sourceImage.UnlockBits(); faceImage.UnlockBits(); }
public ImageProcess(ImageIO sourceImage, double warpFactor, int warpCenter_x, int warpCenter_y, int targetWidth, int targetHeight, Interpolation interpolation, double bicubicFactor = -0.5) { _stretch = false; _imageParameter = new ImageParameter(sourceImage, null, sourceImage.Path, null); sourceImage.LockBits(); _processOption = new ProcessOption(Transform.Warp, interpolation); _distortionParameter = null; _warpParameter = new WarpParameter(sourceImage.Width * sourceImage.Height, warpFactor, warpCenter_x, warpCenter_y); _faceTransformation = null; _interpolationParameter = new InterpolationParameter(targetWidth, targetHeight, bicubicFactor); _processResult = new ProcessResult(this.InterpolationFunction(), null, null); sourceImage.UnlockBits(); }
// 最近邻插值 private ImageIO NearestNeighborInterpolationCore() { ImageIO processed = new ImageIO(null, this.InterpolationParameter.TargetWidth, this.InterpolationParameter.TargetHeight); processed.LockBits(); double widthFactor = Convert.ToDouble(this.ImageParameter.SourceImage.Width - 1) / Convert.ToDouble(this.InterpolationParameter.TargetWidth - 1); double heightFactor = Convert.ToDouble(this.ImageParameter.SourceImage.Height - 1) / Convert.ToDouble(this.InterpolationParameter.TargetHeight - 1); //并行计算 Info infoBox = new Info(); infoBox.Show(); Parallel.For(0, this.InterpolationParameter.TargetHeight, y => { Parallel.For(0, this.InterpolationParameter.TargetWidth, x => { Poseition transformPoseition = this.TransformFunction(Convert.ToDouble(x) * widthFactor, Convert.ToDouble(y) * heightFactor); Poseition interpolatePosition = new Poseition(transformPoseition.X * widthFactor, transformPoseition.Y * heightFactor); if (transformPoseition.X < 0 || transformPoseition.X > (this.ImageParameter.SourceImage.Width - 1) || transformPoseition.Y < 0 || transformPoseition.Y > (this.ImageParameter.SourceImage.Height - 1)) { if (this.Stretch) { Poseition newPoseition = new Poseition(Math.Max(0, Math.Min(this.ImageParameter.SourceImage.Width - 1, transformPoseition.X)), Math.Max(0, Math.Min(this.ImageParameter.SourceImage.Height - 1, transformPoseition.Y))); System.Drawing.Color rgb = this.ImageParameter.SourceImage.GetPixel(Convert.ToInt32(newPoseition.X), Convert.ToInt32(newPoseition.Y)); processed.SetPixel(x, y, rgb); } else { System.Drawing.Color rgb = System.Drawing.Color.FromArgb(0, 0, 0); processed.SetPixel(x, y, rgb); } } else { Poseition newPoseition = new Poseition(Math.Max(0, Math.Min(this.ImageParameter.SourceImage.Width - 1, transformPoseition.X)), Math.Max(0, Math.Min(this.ImageParameter.SourceImage.Height - 1, transformPoseition.Y))); System.Drawing.Color rgb = this.ImageParameter.SourceImage.GetPixel(Convert.ToInt32(newPoseition.X), Convert.ToInt32(newPoseition.Y)); processed.SetPixel(x, y, rgb); } }); }); infoBox.Close(); Success successBox = new Success(); successBox.ShowDialog(); processed.UnlockBits(); return(processed); }
// 构造函数 public FaceTransformation(string source_path, string face_path) { _source_path = source_path; _source_points_path = source_path.Split('.')[0] + ".txt"; _source = new ImageIO(source_path); _source.LockBits(); _source_points = this.LoadPoints(this.Source_points_path); _source_marked = this.MarkPoints(this.Source, this.Source_points); _face_path = face_path; _face_points_path = face_path.Split('.')[0] + ".txt"; _face = new ImageIO(face_path); _face.LockBits(); _face_points = this.LoadPoints(this.Face_points_path); _face_marked = this.MarkPoints(this.Face, this.Face_points); _facePointsAffined = this.AffineTransformation(); _TPS_transform_matrix = this.TPSTransformMatrix(); _source.UnlockBits(); _face.UnlockBits(); }
// 标记关键点 private ImageIO MarkPoints(ImageIO image, double[] points) { ImageIO marked = new ImageIO(image.Path, null); marked.LockBits(); Color rgb = Color.FromArgb(43, 115, 175); Parallel.For(0, 68, i => { Parallel.For(0, 5, y_bias => { Parallel.For(0, 5, x_bias => { int y = Math.Max(0, Math.Min(image.Height - 1, Convert.ToInt32(points[2 * i + 1]) + y_bias - 2)); int x = Math.Max(0, Math.Min(image.Width - 1, Convert.ToInt32(points[2 * i]) + x_bias - 2)); marked.SetPixel(x, y, rgb); }); }); }); marked.UnlockBits(); //marked.SaveImage(); return(marked); }
// 双三次插值 private ImageIO BiCubicInterpolationCore() { ImageIO processed = new ImageIO(null, this.InterpolationParameter.TargetWidth, this.InterpolationParameter.TargetHeight); processed.LockBits(); double widthFactor = Convert.ToDouble(this.ImageParameter.SourceImage.Width - 1) / Convert.ToDouble(this.InterpolationParameter.TargetWidth - 1); double heightFactor = Convert.ToDouble(this.ImageParameter.SourceImage.Height - 1) / Convert.ToDouble(this.InterpolationParameter.TargetHeight - 1); //并行计算 Info infoBox = new Info(); infoBox.Show(); Parallel.For(0, this.InterpolationParameter.TargetHeight, y => { Parallel.For(0, this.InterpolationParameter.TargetWidth, x => { Poseition transformPoseition = this.TransformFunction(Convert.ToDouble(x) * widthFactor, Convert.ToDouble(y) * heightFactor); Poseition interpolatePosition = new Poseition(transformPoseition.X - 0.5, transformPoseition.Y - 0.5); if (transformPoseition.X < 0 || transformPoseition.X > (this.ImageParameter.SourceImage.Width - 1) || transformPoseition.Y < 0 || transformPoseition.Y > (this.ImageParameter.SourceImage.Height - 1)) { if (this.Stretch) { double[] source_pixels_weight_sum = { 0, 0, 0 }; byte[] rgb = { 0, 0, 0 }; Parallel.For(0, 4, y_index => { Parallel.For(0, 4, x_index => { Poseition dPosition = new Poseition(-interpolatePosition.X + Convert.ToInt32(interpolatePosition.X) + x_index - 1, -interpolatePosition.Y + Convert.ToInt32(interpolatePosition.Y) + y_index - 1); Poseition positionWeight = new Poseition(BiCubicWeight(dPosition.X, this.InterpolationParameter.BicubicFactor), BiCubicWeight(dPosition.Y, this.InterpolationParameter.BicubicFactor)); Poseition indexPosition = new Poseition(Math.Max(0, Math.Min(this.ImageParameter.SourceImage.Width - 1, Convert.ToInt32(interpolatePosition.X) + x_index - 1)), Math.Max(0, Math.Min(this.ImageParameter.SourceImage.Height - 1, Convert.ToInt32(interpolatePosition.Y) + y_index - 1))); source_pixels_weight_sum[0] += Convert.ToDouble(this.ImageParameter.SourceImage.GetPixel(Convert.ToInt32(indexPosition.X), Convert.ToInt32(indexPosition.Y)).R) * positionWeight.X * positionWeight.Y; source_pixels_weight_sum[1] += Convert.ToDouble(this.ImageParameter.SourceImage.GetPixel(Convert.ToInt32(indexPosition.X), Convert.ToInt32(indexPosition.Y)).G) * positionWeight.X * positionWeight.Y; source_pixels_weight_sum[2] += Convert.ToDouble(this.ImageParameter.SourceImage.GetPixel(Convert.ToInt32(indexPosition.X), Convert.ToInt32(indexPosition.Y)).B) * positionWeight.X * positionWeight.Y; }); }); for (int i = 0; i < 3; i++) { rgb[i] = Convert.ToByte(Math.Max(0, Math.Min(255, source_pixels_weight_sum[i]))); } System.Drawing.Color rgb_color = System.Drawing.Color.FromArgb(rgb[0], rgb[1], rgb[2]); processed.SetPixel(x, y, rgb_color); } else { System.Drawing.Color rgb = System.Drawing.Color.FromArgb(0, 0, 0); processed.SetPixel(x, y, rgb); } } else { double[] source_pixels_weight_sum = { 0, 0, 0 }; byte[] rgb = { 0, 0, 0 }; Parallel.For(0, 4, y_index => { Parallel.For(0, 4, x_index => { Poseition dPosition = new Poseition(-interpolatePosition.X + Convert.ToInt32(interpolatePosition.X) + x_index - 1, -interpolatePosition.Y + Convert.ToInt32(interpolatePosition.Y) + y_index - 1); Poseition positionWeight = new Poseition(BiCubicWeight(dPosition.X, this.InterpolationParameter.BicubicFactor), BiCubicWeight(dPosition.Y, this.InterpolationParameter.BicubicFactor)); Poseition indexPosition = new Poseition(Math.Max(0, Math.Min(this.ImageParameter.SourceImage.Width - 1, Convert.ToInt32(interpolatePosition.X) + x_index - 1)), Math.Max(0, Math.Min(this.ImageParameter.SourceImage.Height - 1, Convert.ToInt32(interpolatePosition.Y) + y_index - 1))); source_pixels_weight_sum[0] += Convert.ToDouble(this.ImageParameter.SourceImage.GetPixel(Convert.ToInt32(indexPosition.X), Convert.ToInt32(indexPosition.Y)).R) * positionWeight.X * positionWeight.Y; source_pixels_weight_sum[1] += Convert.ToDouble(this.ImageParameter.SourceImage.GetPixel(Convert.ToInt32(indexPosition.X), Convert.ToInt32(indexPosition.Y)).G) * positionWeight.X * positionWeight.Y; source_pixels_weight_sum[2] += Convert.ToDouble(this.ImageParameter.SourceImage.GetPixel(Convert.ToInt32(indexPosition.X), Convert.ToInt32(indexPosition.Y)).B) * positionWeight.X * positionWeight.Y; }); }); for (int i = 0; i < 3; i++) { rgb[i] = Convert.ToByte(Math.Max(0, Math.Min(255, source_pixels_weight_sum[i]))); } System.Drawing.Color rgb_color = System.Drawing.Color.FromArgb(rgb[0], rgb[1], rgb[2]); processed.SetPixel(x, y, rgb_color); } }); }); infoBox.Close(); Success successBox = new Success(); successBox.ShowDialog(); processed.UnlockBits(); return(processed); }
// 双线性插值 private ImageIO BiIinearInterpolationCore() { ImageIO processed = new ImageIO(null, this.InterpolationParameter.TargetWidth, this.InterpolationParameter.TargetHeight); processed.LockBits(); double widthFactor = Convert.ToDouble(this.ImageParameter.SourceImage.Width - 1) / Convert.ToDouble(this.InterpolationParameter.TargetWidth - 1); double heightFactor = Convert.ToDouble(this.ImageParameter.SourceImage.Height - 1) / Convert.ToDouble(this.InterpolationParameter.TargetHeight - 1); //并行计算 Info infoBox = new Info(); infoBox.Show(); Parallel.For(0, this.InterpolationParameter.TargetHeight, y => { Parallel.For(0, this.InterpolationParameter.TargetWidth, x => { Poseition transformPoseition = this.TransformFunction(Convert.ToDouble(x) * widthFactor, Convert.ToDouble(y) * heightFactor); Poseition interpolatePosition = new Poseition(transformPoseition.X, transformPoseition.Y); Poseition interpolatePositionFloor = new Poseition(Math.Floor(transformPoseition.X), Math.Floor(transformPoseition.Y)); Poseition interpolatePosition11 = new Poseition(Math.Max(0, Math.Min(this.InterpolationParameter.TargetWidth - 1, interpolatePositionFloor.X)), Math.Max(0, Math.Min(this.InterpolationParameter.TargetHeight - 1, interpolatePositionFloor.Y))); Poseition interpolatePosition12 = new Poseition(Math.Max(0, Math.Min(this.InterpolationParameter.TargetWidth - 1, interpolatePositionFloor.X)), Math.Max(0, Math.Min(this.InterpolationParameter.TargetHeight - 1, interpolatePositionFloor.Y + 1))); Poseition interpolatePosition21 = new Poseition(Math.Max(0, Math.Min(this.InterpolationParameter.TargetWidth - 1, interpolatePositionFloor.X + 1)), Math.Max(0, Math.Min(this.InterpolationParameter.TargetHeight - 1, interpolatePositionFloor.Y))); Poseition interpolatePosition22 = new Poseition(Math.Max(0, Math.Min(this.InterpolationParameter.TargetWidth - 1, interpolatePositionFloor.X + 1)), Math.Max(0, Math.Min(this.InterpolationParameter.TargetHeight - 1, interpolatePositionFloor.Y + 1))); if (transformPoseition.X <= 0 || transformPoseition.X >= (this.ImageParameter.SourceImage.Width - 1) || transformPoseition.Y <= 0 || transformPoseition.Y >= (this.ImageParameter.SourceImage.Height - 1)) { if (this.Stretch) { Poseition newPoseition = new Poseition(Math.Max(0, Math.Min(this.ImageParameter.SourceImage.Width - 1, transformPoseition.X)), Math.Max(0, Math.Min(this.ImageParameter.SourceImage.Height - 1, transformPoseition.Y))); System.Drawing.Color rgb = this.ImageParameter.SourceImage.GetPixel(Convert.ToInt32(newPoseition.X), Convert.ToInt32(newPoseition.Y)); processed.SetPixel(x, y, rgb); } else { System.Drawing.Color rgb = System.Drawing.Color.FromArgb(0, 0, 0); processed.SetPixel(x, y, rgb); } } else { double[] source_pixels_weight_sum = { 0, 0, 0 }; byte[] rgb = { 0, 0, 0 }; source_pixels_weight_sum[0] += Convert.ToDouble(this.ImageParameter.SourceImage.GetPixel(Convert.ToInt32(interpolatePosition11.X), Convert.ToInt32(interpolatePosition11.Y)).R) * (interpolatePosition22.X - interpolatePosition.X) * (interpolatePosition22.Y - interpolatePosition.Y); source_pixels_weight_sum[0] += Convert.ToDouble(this.ImageParameter.SourceImage.GetPixel(Convert.ToInt32(interpolatePosition12.X), Convert.ToInt32(interpolatePosition12.Y)).R) * (interpolatePosition22.X - interpolatePosition.X) * (interpolatePosition.Y - interpolatePosition11.Y); source_pixels_weight_sum[0] += Convert.ToDouble(this.ImageParameter.SourceImage.GetPixel(Convert.ToInt32(interpolatePosition21.X), Convert.ToInt32(interpolatePosition21.Y)).R) * (interpolatePosition.X - interpolatePosition11.X) * (interpolatePosition22.Y - interpolatePosition.Y); source_pixels_weight_sum[0] += Convert.ToDouble(this.ImageParameter.SourceImage.GetPixel(Convert.ToInt32(interpolatePosition22.X), Convert.ToInt32(interpolatePosition22.Y)).R) * (interpolatePosition.X - interpolatePosition11.X) * (interpolatePosition.Y - interpolatePosition11.Y); source_pixels_weight_sum[1] += Convert.ToDouble(this.ImageParameter.SourceImage.GetPixel(Convert.ToInt32(interpolatePosition11.X), Convert.ToInt32(interpolatePosition11.Y)).G) * (interpolatePosition22.X - interpolatePosition.X) * (interpolatePosition22.Y - interpolatePosition.Y); source_pixels_weight_sum[1] += Convert.ToDouble(this.ImageParameter.SourceImage.GetPixel(Convert.ToInt32(interpolatePosition12.X), Convert.ToInt32(interpolatePosition12.Y)).G) * (interpolatePosition22.X - interpolatePosition.X) * (interpolatePosition.Y - interpolatePosition11.Y); source_pixels_weight_sum[1] += Convert.ToDouble(this.ImageParameter.SourceImage.GetPixel(Convert.ToInt32(interpolatePosition21.X), Convert.ToInt32(interpolatePosition21.Y)).G) * (interpolatePosition.X - interpolatePosition11.X) * (interpolatePosition22.Y - interpolatePosition.Y); source_pixels_weight_sum[1] += Convert.ToDouble(this.ImageParameter.SourceImage.GetPixel(Convert.ToInt32(interpolatePosition22.X), Convert.ToInt32(interpolatePosition22.Y)).G) * (interpolatePosition.X - interpolatePosition11.X) * (interpolatePosition.Y - interpolatePosition11.Y); source_pixels_weight_sum[2] += Convert.ToDouble(this.ImageParameter.SourceImage.GetPixel(Convert.ToInt32(interpolatePosition11.X), Convert.ToInt32(interpolatePosition11.Y)).B) * (interpolatePosition22.X - interpolatePosition.X) * (interpolatePosition22.Y - interpolatePosition.Y); source_pixels_weight_sum[2] += Convert.ToDouble(this.ImageParameter.SourceImage.GetPixel(Convert.ToInt32(interpolatePosition12.X), Convert.ToInt32(interpolatePosition12.Y)).B) * (interpolatePosition22.X - interpolatePosition.X) * (interpolatePosition.Y - interpolatePosition11.Y); source_pixels_weight_sum[2] += Convert.ToDouble(this.ImageParameter.SourceImage.GetPixel(Convert.ToInt32(interpolatePosition21.X), Convert.ToInt32(interpolatePosition21.Y)).B) * (interpolatePosition.X - interpolatePosition11.X) * (interpolatePosition22.Y - interpolatePosition.Y); source_pixels_weight_sum[2] += Convert.ToDouble(this.ImageParameter.SourceImage.GetPixel(Convert.ToInt32(interpolatePosition22.X), Convert.ToInt32(interpolatePosition22.Y)).B) * (interpolatePosition.X - interpolatePosition11.X) * (interpolatePosition.Y - interpolatePosition11.Y); for (int i = 0; i < 3; i++) { rgb[i] = Convert.ToByte(Math.Max(0, Math.Min(255, source_pixels_weight_sum[i]))); } System.Drawing.Color rgb_color = System.Drawing.Color.FromArgb(rgb[0], rgb[1], rgb[2]); processed.SetPixel(x, y, rgb_color); } }); }); infoBox.Close(); Success successBox = new Success(); successBox.ShowDialog(); processed.UnlockBits(); return(processed); }