public String JustCropandBrightness(BladeCroppingSettings settings, string inputFile, string outputFile, int LeftCrop, int RightCrop, int BrightnessCorrection) { try { Image <Bgr, Byte> ImageInput = new Image <Bgr, Byte>(inputFile); if (RightCrop > ImageInput.Width) { RightCrop = ImageInput.Width - 1; } Rectangle rectCrop = new Rectangle((int)(LeftCrop), 0, (int)(RightCrop - LeftCrop), ImageInput.Height); Image <Bgr, Byte> CroppedImg = ImageInput.GetSubRect(rectCrop); if (BrightnessCorrection != 0) { //convert to HSV Image <Hsv, Byte> HSVImage = CroppedImg.Convert <Hsv, Byte>(); for (int i = 0; i < HSVImage.Width; i++) { for (int j = 0; j < HSVImage.Height; j++) { Byte val = HSVImage.Data[j, i, 2]; if ((int)val + BrightnessCorrection <= 0) { HSVImage.Data[j, i, 2] = 0; } else if ((int)val + BrightnessCorrection >= 255) { HSVImage.Data[j, i, 2] = 255; } else { val = (Byte)((int)val + BrightnessCorrection); HSVImage.Data[j, i, 2] = val; } } } CroppedImg = HSVImage.Convert <Bgr, Byte>(); HSVImage.Dispose(); } CroppedImg.Save(outputFile); ImageInput.Dispose(); CroppedImg.Dispose(); copyMetaData(inputFile, outputFile); GC.Collect(); return("OK"); } catch (Exception e) { return(e.ToString()); } }
public int[] getCropValues(BladeCroppingSettings settings, Image <Bgr, Byte> image) { GC.Collect(); int SmallerPhotos = settings.getScaleDown(); // save large photos or small. 0 is 10X smaller if (image.Cols < 100) { return(new int[] { 0, 0 }); } int smoother = 5; //filter value float borderfrac = 6; // what fraction of the image is added border float scalefactor = 1; //times smaller processed image is //get image size double height = image.Height;// get image dimensions double width = image.Width; int Rewidth = (int)(width / scalefactor); int Reheight = (int)(height / scalefactor); //create scaled version of the image Image <Bgr, Byte> resizedimage = new Image <Bgr, byte>(Rewidth, Reheight); //resize the image for processing resizedimage = image.Resize(Rewidth, Reheight, Inter.Linear); bool DarkGround = GetGroundDark(resizedimage);// return if the ground is darker or lighter than the blade Image <Gray, Byte> thresh_special = SingleLineTurbineFinder(resizedimage, DarkGround); int Flag_Special = 0; int seq_Special = 0; int ThreshVal = 3; //image border variables int borderLeft = (int)(Rewidth / borderfrac); int borderRight = (int)(Rewidth / borderfrac); int RightPixel = (int)(Rewidth - borderRight - Rewidth / 8); int LeftPixel = (int)(borderLeft + Rewidth / 8); int RightPixel_Special = (int)(Rewidth / 2); int LeftPixel_Special = (int)(Rewidth / 2 - 1); while ((seq_Special < smoother)) { if ((thresh_special[0, LeftPixel].Intensity < (Byte)ThreshVal) && (Flag_Special == 0)) { seq_Special += 1; if (seq_Special >= smoother) { LeftPixel_Special = LeftPixel; Flag_Special = 1; } } else if (Flag_Special == 0) { seq_Special = 0; } LeftPixel += 1; if (LeftPixel >= width / scalefactor / 2) { break; } } //////////////////////////////////////////////////////////RightSide Flag_Special = 0; seq_Special = 0; while ((seq_Special < smoother)) { if ((thresh_special[0, RightPixel].Intensity < ThreshVal) && (Flag_Special == 0)) { seq_Special += 1; if (seq_Special >= smoother) { RightPixel_Special = RightPixel; Flag_Special = 1; } } else if (Flag_Special == 0) { seq_Special = 0; } ////////////////////////////// RightPixel -= 1; if (RightPixel <= width / scalefactor / 2) { break; } } ////////this section checks for side cases and towers if (Math.Abs(LeftPixel_Special - (borderLeft + width / scalefactor / 8)) < 20) { LeftPixel_Special = (int)(width / scalefactor / 3); int tmpval = (int)(thresh_special[(int)(0), (int)(LeftPixel_Special - borderLeft)].Intensity); while (tmpval < ThreshVal) { tmpval = (int)(thresh_special[(int)(0), (int)(LeftPixel_Special - borderLeft)].Intensity); LeftPixel_Special -= 1; if ((thresh_special[(int)(0), (int)(LeftPixel_Special - borderLeft)].Intensity) > ThreshVal) { break; } else if (LeftPixel_Special <= borderLeft) { LeftPixel_Special = borderLeft + 1; break; } } } if (Math.Abs(RightPixel_Special - width / scalefactor - (borderRight + width / scalefactor / 8)) < 20) { RightPixel_Special = (int)(2 * width / scalefactor / 3); int tmpval = (int)(thresh_special[(int)(0), (int)(RightPixel_Special + borderRight)].Intensity); while ((int)(thresh_special[(int)(0), (int)(RightPixel_Special)].Intensity) < ThreshVal) { tmpval = (int)(thresh_special[(int)(0), (int)(RightPixel_Special + borderRight)].Intensity); RightPixel_Special += 1; if ((int)(thresh_special[(int)(0), (int)(RightPixel_Special + borderRight)].Intensity) > ThreshVal) { break; } else if (RightPixel_Special > (width / scalefactor) - 1) { RightPixel_Special = (int)(width / scalefactor - borderRight - 1); break; } } } //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// if (LeftPixel_Special <= borderLeft) { LeftPixel_Special = borderLeft; } if (RightPixel_Special >= width / scalefactor - 1 - borderRight) { RightPixel_Special = (int)(width / scalefactor - 1 - borderRight); } double angle = 0; //var task = Task.Factory.StartNew(() => getSkewAngle(resizedimage)); //if (task.Wait(TimeSpan.FromSeconds(10))) { // angle = task.Result; // Console.WriteLine("got angle"); } //else //{ // angle = 0; //} //adds extra border on trailing edge side //if (angle < 0) //{ // borderRight = borderRight * 2; //} //else //{ // borderLeft = borderLeft * 2; //} if (LeftPixel_Special <= borderLeft) { LeftPixel_Special = borderLeft; } if (RightPixel_Special >= resizedimage.Width - 1 - borderRight) { RightPixel_Special = (int)(resizedimage.Width - 1 - borderRight); } Rectangle rectCrop = new Rectangle((int)(LeftPixel_Special - borderLeft), 0, (int)(RightPixel_Special + borderRight + borderLeft - LeftPixel_Special), (int)(resizedimage.Height)); Image <Bgr, Byte> croppedImage; rectCrop.X = (int)(LeftPixel - borderLeft); rectCrop.Width = (int)(RightPixel + borderRight + borderLeft - LeftPixel); croppedImage = resizedimage.GetSubRect(rectCrop); int rows = croppedImage.Rows; int cols = croppedImage.Cols; int ch = croppedImage.NumberOfChannels; Mat M = GetSkewTransform(resizedimage, angle, LeftPixel_Special, RightPixel_Special, borderLeft, borderRight); Image <Bgr, Byte> outimage; Image <Hsv, Byte> touchedim; //if we are saving the large or compressed image if (SmallerPhotos == 0) { Image <Bgr, Byte> tmpimage = new Image <Bgr, Byte>((int)width, (int)height); outimage = image; CvInvoke.WarpAffine(outimage, tmpimage, M, new Size((int)width, (int)height)); rectCrop = new Rectangle((int)((LeftPixel_Special - borderLeft) * scalefactor), 0, (int)((RightPixel_Special + borderRight + borderLeft - LeftPixel_Special) * scalefactor), (int)(height)); outimage.Dispose(); Image <Bgr, Byte> CroppedImg = tmpimage.GetSubRect(rectCrop); tmpimage.Dispose(); touchedim = CroppedImg.Convert <Hsv, Byte>(); CroppedImg.Dispose(); GC.Collect(); } else { Image <Bgr, Byte> tmpimage = new Image <Bgr, Byte>((int)resizedimage.Width, (int)resizedimage.Height);; outimage = resizedimage; CvInvoke.WarpAffine(outimage, tmpimage, M, new Size((int)(width / scalefactor), (int)(height / scalefactor))); outimage.Dispose(); rectCrop = new Rectangle((int)(LeftPixel_Special - borderLeft), 0, (int)(RightPixel_Special + borderRight + borderLeft - LeftPixel_Special), (int)(height / scalefactor)); Image <Bgr, Byte> CroppedImg = tmpimage.GetSubRect(rectCrop); tmpimage.Dispose(); touchedim = CroppedImg.Convert <Hsv, Byte>(); CroppedImg.Dispose(); GC.Collect(); } Image <Bgr, Byte> touchedOutIm = touchedim.Convert <Bgr, Byte>(); touchedim.Dispose(); GC.Collect(); touchedOutIm.Dispose(); GC.Collect(); image.Dispose(); GC.Collect(); GC.WaitForPendingFinalizers(); int[] cropVals = new int[] { LeftPixel_Special - borderLeft, RightPixel_Special + borderRight }; return(cropVals); }