예제 #1
0
        public static AddTextureInfo GetAddTextureInfoForIndexCollection(CreateMeshContract inputParams, ICollection <int> indexCollection)
        {
            var logger = new Logger(inputParams.LogFilePath);

            logger.Log("Start GetAddTextureInfoForIndexCollection");

            var angles         = inputParams.ClickInputs.Angles;
            var addTextureInfo = new AddTextureInfo
            {
                ImageInfos = new AddTexImageInfo[indexCollection.Count]
            };

            var imageAlterationParams = new ImageAlterationParams
            {
                MoldPtDensity        = inputParams.PtDensity,
                MinImageHeightRatio  = inputParams.MinImageHeightRatio,
                PercentExtraWidth    = inputParams.PercentExtraWidth,
                ImageFolder          = inputParams.ImageFolder,
                InvalidColor         = inputParams.InvalidColor,
                ResizeType           = ResizeType.ResizeSufficiently,
                BottomPaddingPercent = inputParams.BottomPaddingPercent
            };

            var ctr = 0;

            foreach (var index in indexCollection)
            {
                var clickInput = inputParams.ClickInputs.ImageClickInputDetailsList[index];
                var angle      = angles[index];

                var imageParams = ImageProcessor.GetImageParams(inputParams.VariationIn3DCoordinates, inputParams.BackgroundStrippingParams, clickInput, angle, imageAlterationParams);

                addTextureInfo.ImageInfos[ctr] = new AddTexImageInfo {
                    CameraLocation = imageParams.cameraLocation, ImageBitmap = imageParams.Image, LookingAt = imageParams.lookingAt
                };
                if (ctr == 0)
                {
                    //set the camera ratio
                    addTextureInfo.CameraRatio = new TextureGenerator.CameraRatio {
                        XRangeAtInfinity = imageParams.CameraRatio.xRangeAtInfinity, YRangeAtInfinity = imageParams.CameraRatio.yRangeAtInfinity
                    };
                }

                //make sure the rest of the images are resized to the same size as the first one so that all texture images are of the same ht
                imageAlterationParams.ResizeType           = ResizeType.ToSpecifiedSizes;
                imageAlterationParams.SpecificResizeHeight = imageParams.Image.Height;
                imageAlterationParams.SpecificResizeWidth  = imageParams.Image.Width;

                ctr++;
            }
            logger.Log("End GetAddTextureInfoForIndexCollection");
            return(addTextureInfo);
        }
예제 #2
0
        /// <summary>
        /// The width of the object can be more than the diameter of the base disc
        /// In such scenarios, we will need to keep some extra padding on each side of disc
        /// while cropping the image. This function calculates the amount by which we should pad.
        /// </summary>
        /// <param name="contract"></param>
        /// <returns></returns>
        public static double GetExtraPadding(CreateMeshContract contract)
        {
            var maxExtraPaddingPercent = 0.0;
            var bottomPaddingPercent   = contract.BottomPaddingPercent;

            //for each image
            foreach (var clickInput in contract.ClickInputs.ImageClickInputDetailsList)
            {
                var imageName = clickInput.ImageName;

                var bitmapImg = (Bitmap)Image.FromFile(string.Format(@"{0}\{1}", contract.ImageFolder, imageName));

                //resize to 100 (small size)
                const int newWidth  = 100;
                var       newHeight = newWidth * bitmapImg.Height / bitmapImg.Width;

                var imageAlterationParams = new ImageAlterationParams
                {
                    ResizeType           = ResizeType.ToSpecifiedSizes,
                    SpecificResizeHeight = newHeight,
                    SpecificResizeWidth  = newWidth
                };

                var resizedImage = ImageResizer.ResizeImage(bitmapImg, imageAlterationParams);

                //strip background
                BackgroundStripper.StripBackground(resizedImage, contract.BackgroundStrippingParams);

                //get actual borders for this image
                var origImageCorners   = ImageCorners.GetImageCornersFromClickInputs(clickInput, contract.MinImageHeightRatio, bottomPaddingPercent);
                var actualImageCorners = ImageCorners.GetActualImageCorners(origImageCorners, resizedImage, clickInput.ClickPositionListForImages);

                //calculate extra padding percent
                var padding = ExtraPadding.GetExtraPaddingPercent(resizedImage, actualImageCorners,
                                                                  contract.InvalidColor);

                //check if it is more than the maximum so far
                if (maxExtraPaddingPercent < padding)
                {
                    maxExtraPaddingPercent = padding;
                }
            }
            return(maxExtraPaddingPercent);
        }
예제 #3
0
        public static Bitmap GetCroppedImage(ImageClickInputDetails clickInputDetails, Bitmap image, ImageAlterationParams imageAlterationParams)
        {
            var imgCornersExtra = GetImgCornersWithExtraPadding(clickInputDetails, image, imageAlterationParams);

            if (imgCornersExtra.Left >= 0 && imgCornersExtra.Right < image.Width)
            {
                //crop image from all side
                var rectangle = new Rectangle((int)imgCornersExtra.Left, (int)imgCornersExtra.Top, (int)imgCornersExtra.Width, (int)imgCornersExtra.Height);
                return(image.Clone(rectangle, image.PixelFormat));
            }
            else
            {
                //prepare a new image
                var newImg = new Bitmap(Convert.ToInt32(imgCornersExtra.Width), Convert.ToInt32(imgCornersExtra.Height), image.PixelFormat);
                var g      = Graphics.FromImage(newImg);
                g.Clear(imageAlterationParams.InvalidColor);
                g.InterpolationMode = InterpolationMode.HighQualityBilinear;

                //when overlaying the image, give the top coordinate in the negative direction so that it gets cropped to the top pixel
                var topOfImageForOverlay = -imgCornersExtra.Top;

                if (imgCornersExtra.Left > 0)
                {
                    //needs trimming from the left
                    var rectangle = new Rectangle((int)imgCornersExtra.Left, 0, (int)(image.Width - imgCornersExtra.Left - 1), image.Height);
                    image = image.Clone(rectangle, image.PixelFormat);
                    g.DrawImage(image, 0, (int)topOfImageForOverlay, image.Width, image.Height);
                }
                else
                {
                    var moveForwardBy = -imgCornersExtra.Left;
                    g.DrawImage(image, (int)moveForwardBy, (int)topOfImageForOverlay, image.Width, image.Height);
                }

                g.Dispose();
                return(newImg);
            }
        }
예제 #4
0
        private static ImageCorners GetImgCornersWithExtraPadding(ImageClickInputDetails clickInputs, Bitmap image, ImageAlterationParams imageAlterationParams)
        {
            //image corners with respect to the dimensions of click area
            var clickImgCorners = ImageCorners.GetImageCornersFromClickInputs(clickInputs, imageAlterationParams.MinImageHeightRatio, imageAlterationParams.BottomPaddingPercent);

            //image corners in actual image dimension
            var actualImgCorners = ImageCorners.GetActualImageCorners(clickImgCorners, image, clickInputs.ClickPositionListForImages);

            //image corners with extra padding on on each side of disc
            return(GetModifiedImgCornersForExtraWidth(actualImgCorners, imageAlterationParams.PercentExtraWidth));
        }
예제 #5
0
        private static Bitmap GetRotatedCroppedResizedAndStrippedImage(BackgroundStrippingParams strippingParams, ImageClickInputDetails clickInput, ImageAlterationParams imageAlterationParams)
        {
            //pickup image from the source location
            var image = (Bitmap)Image.FromFile(String.Format(@"{0}\{1}", imageAlterationParams.ImageFolder, clickInput.ImageName));

            //rotate
            image = MainProcessor.RotateImg(image, (float)clickInput.RotateImageBy, imageAlterationParams.InvalidColor);

            //crop
            image = ImageCropper.GetCroppedImage(clickInput, image, imageAlterationParams);

            //resize
            image = ImageResizer.ResizeImage(image, imageAlterationParams);

            //strip background
            BackgroundStripper.StripBackground(image, strippingParams);

            return(image);
        }
예제 #6
0
        public static ImageParams GetImageParams(double variationIn3DCoordinates, BackgroundStrippingParams strippingParams, ImageClickInputDetails clickInput, double angle, ImageAlterationParams imageAlterationParams)
        {
            var imageData = GetRotatedCroppedResizedAndStrippedImage(strippingParams, clickInput, imageAlterationParams);

            //get camera ratio
            var cameraRatio = GetCamRatio(imageData, variationIn3DCoordinates);

            var cameraLocationPt = GetCamPos(variationIn3DCoordinates * 4.0, angle, 0.0);
            var lookingAtPt      = GetCamDir(angle, 0.0, 0.0);

            return(new ImageParams
            {
                InvalidColor = imageAlterationParams.InvalidColor,
                Image = imageData,
                CameraAtInfinity = true,
                cameraLocation = cameraLocationPt,
                lookingAt = lookingAtPt,
                CameraRatio = cameraRatio
            });
        }
예제 #7
0
 public static Bitmap ResizeImage(Bitmap image, ImageAlterationParams imageAlterationParams)
 {
     return(ImageResizer.ResizeImage(image, imageAlterationParams));
 }
예제 #8
0
        public static void ApplyImage(CreateMeshContract inputParams, int index, ref Stream moldDataPtr, Logger logger)
        {
            var clickInput = inputParams.ClickInputs.ImageClickInputDetailsList[index];
            var angle      = inputParams.ClickInputs.Angles[index];

            var imageAlterationParameters = new ImageAlterationParams
            {
                MoldPtDensity        = inputParams.PtDensity,
                MinImageHeightRatio  = inputParams.MinImageHeightRatio,
                PercentExtraWidth    = inputParams.PercentExtraWidth,
                ImageFolder          = inputParams.ImageFolder,
                InvalidColor         = inputParams.InvalidColor,
                ResizeType           = ResizeType.ComputeSizeBasedOnPtDensity,
                BottomPaddingPercent = inputParams.BottomPaddingPercent
            };
            var imageParams = ImageProcessor.GetImageParams(inputParams.VariationIn3DCoordinates, inputParams.BackgroundStrippingParams, clickInput, angle, imageAlterationParameters);

            if (moldDataPtr == null)
            {
                moldDataPtr = CreateMoldDataStream(imageParams.Image.Width, imageParams.Image.Height, (float)inputParams.VariationIn3DCoordinates, inputParams.PtDensity, logger);
            }

            var processMoldParams = new ProcessMoldParams
            {
                MoldData    = moldDataPtr,
                ImageParams = imageParams
            };
            var imagesToObject = new ImagesToObject(processMoldParams, inputParams.LogFilePath);

            imagesToObject.ProcessImage();

            #region track_changes_for_troubleshooting

            if (inputParams.SaveProcessedImages)
            {
                SaveProcessedImages(processMoldParams.ImageParams.Image, inputParams.ImageFolder, "ibmodeler3_modified_images", index, angle, logger);
            }

            var imageParamsToBeReused = false;
            if (AnalyzeMoldForDataLosses)
            {
                //if first time, just set values for subsequent use
                if (index == 0)
                {
                    FirstImageParams      = imageParams;
                    imageParamsToBeReused = true;
                }
                else
                {
                    var processMoldParamsForAnalysis = new ProcessMoldParams
                    {
                        MoldData    = moldDataPtr,
                        ImageParams = FirstImageParams,
                        SetColorForRetainedPixels = false
                    };
                    int pixelsWithAllPointsLost = MoldFileAnalyzerForImg.ProcessImage(processMoldParamsForAnalysis);
                    if (pixelsWithAllPointsLost > 0)
                    {
                        SaveDataLossImages(FirstImageParams.Image, inputParams.ImageFolder, "ibmodeler3_data_loss_analysis", index, angle, pixelsWithAllPointsLost, logger);
                    }
                }
            }

            #endregion

            //this will dispose off the image data from memory
            if (!imageParamsToBeReused)
            {
                imageParams.Dispose();
            }

            logger.Log(string.Format("Image applied for camera at an angle of {0} degrees", Math.Round(angle * 180 / Math.PI, 2)));
        }