public SobelAnalyzer(BitmapData bitmap, int sobelThreshold, float bucketTreshold) { var thumbnail = GetThumbnail(bitmap); var thumbnailData = thumbnail.LockBits(new Rectangle(0, 0, thumbnail.Width, thumbnail.Height), ImageLockMode.ReadOnly, thumbnail.PixelFormat); var thumbnailInspection = new BorderInspector(thumbnailData, _thumbnailSize, 1.0f); thumbnail.UnlockBits(thumbnailData); var features = GetFeatureMap(thumbnail); var featureBox = new Rectangle(0, 0, features.Width, features.Height); var featureData = features.LockBits(featureBox, ImageLockMode.ReadOnly, features.PixelFormat); var featureInspection = new BorderInspector(featureData, sobelThreshold, bucketTreshold, forceRgb: true); if (featureInspection.Failed) { featureBox = featureBox.Contract(10); var additionalInspection = new BorderInspector(featureData, featureBox, sobelThreshold, bucketTreshold); if (!additionalInspection.Failed) { featureInspection = additionalInspection; } } if (featureInspection.Failed) { _boundingBox = new Rectangle(0, 0, bitmap.Width, bitmap.Height); _foundBoundingBox = false; } else { _boundingBox = GetBoundingBoxForFeature(featureData, featureInspection.Rectangle, sobelThreshold, bitmap.Width, bitmap.Height); _foundBoundingBox = true; } features.UnlockBits(featureData); features.Dispose(); thumbnail.Dispose(); _analysis = new CropAnalysis { Success = _foundBoundingBox, BoundingBox = _boundingBox, Background = thumbnailInspection.BackgroundColor, }; }
public BoundsAnalyzer(BitmapData bitmap, int colorThreshold, float bucketTreshold) { var outerBox = new Rectangle(0, 0, bitmap.Width, bitmap.Height); var imageBox = outerBox; var borderInspection = new BorderInspector(bitmap, imageBox, colorThreshold, bucketTreshold); if (borderInspection.Failed) { colorThreshold = (int)Math.Round(colorThreshold * 0.5); bucketTreshold = 1.0f; imageBox = imageBox.Contract(10); var additionalInspection = new BorderInspector(bitmap, imageBox, colorThreshold, bucketTreshold); if (!additionalInspection.Failed) { borderInspection = additionalInspection; } } if (borderInspection.Failed) { _boundingBox = outerBox; _foundBoundingBox = false; } else { if (borderInspection.BytesPerPixel == 3) { _boundingBox = GetBoundingBoxForContentRgb(bitmap, borderInspection.Rectangle, borderInspection.BackgroundColor, colorThreshold); } else { _boundingBox = GetBoundingBoxForContentArgb(bitmap, borderInspection.Rectangle, borderInspection.BackgroundColor, colorThreshold); } _foundBoundingBox = ValidateRectangle(_boundingBox); } _cropAnalysis = new CropAnalysis { Background = borderInspection.BackgroundColor, BoundingBox = _boundingBox, Success = _foundBoundingBox }; }