@@ -161,7 +161,7 @@ public static IList<Tuple<int, int>> IntersectionOfSegments(IEnumerable<IList<Tu
161161 sum . AddRange ( line ) ;
162162 }
163163 else
164- {
164+ {
165165 // Perform intersection checks
166166 var newSum = new List < Tuple < int , int > > ( ) ;
167167 foreach ( var segmentA in sum )
@@ -211,25 +211,60 @@ public static Tuple<int, int> SegmentsWithMaxDistance(IList<Tuple<int, int>> seg
211211 return new Tuple < int , int > ( i1 , i2 ) ;
212212 }
213213
214+ private IList < Tuple < int , int > > FindHorizontalZeroSegments ( int iy , int minimalSegmentLength )
215+ {
216+ return FlatImage . FindZeroSegments ( FlatImage . GetDerivative ( this . GetHorizontalStripe ( iy ) ) , minimalSegmentLength ) ;
217+ }
218+
219+ private IList < Tuple < int , int > > FindVerticalZeroSegments ( int ix , int minimalSegmentLength )
220+ {
221+ return FlatImage . FindZeroSegments ( FlatImage . GetDerivative ( this . GetVerticalStripe ( ix ) ) , minimalSegmentLength ) ;
222+ }
223+
214224 public Models . Geometry . Rectangle FindBoundingsOfInnerImage ( )
215225 {
216226 // TODO: add tests for this method
217227 const int minimalSegmentLength = 8 ;
218228
219- // TODO: Very simple method, need to perform more checks and do more searches
220229 var stripeCH = FlatImage . FindZeroSegments ( FlatImage . GetDerivative ( this . GetHorizontalStripe ( this . Height / 2 ) ) , minimalSegmentLength ) ;
221230 var stripeCV = FlatImage . FindZeroSegments ( FlatImage . GetDerivative ( this . GetVerticalStripe ( this . Width / 2 ) ) , minimalSegmentLength ) ;
222231
223- var maxH = FlatImage . SegmentsWithMaxDistance ( stripeCH ) ;
224- var maxV = FlatImage . SegmentsWithMaxDistance ( stripeCV ) ;
232+ // TODO: optimize code
233+ for ( var iy = 1 * this . Height / 4 ; iy < 3 * this . Height / 4 ; iy += minimalSegmentLength )
234+ {
235+ stripeCH = FlatImage . IntersectionOfSegments ( new [ ]
236+ {
237+ stripeCH ,
238+ FindHorizontalZeroSegments ( iy , minimalSegmentLength )
239+ } ) ;
240+ }
241+
242+ for ( var ix = 1 * this . Width / 4 ; ix < 3 * this . Width / 4 ; ix += minimalSegmentLength )
243+ {
244+ stripeCV = FlatImage . IntersectionOfSegments ( new [ ]
245+ {
246+ stripeCV ,
247+ FindVerticalZeroSegments ( ix , minimalSegmentLength )
248+ } ) ;
249+ }
225250
226- return new Geometry . Rectangle
251+ if ( ( stripeCH . Count > 1 ) && ( stripeCV . Count > 1 ) )
227252 {
228- X = maxH . Item1 + 1 ,
229- Y = maxV . Item1 + 1 ,
230- Width = maxH . Item2 - maxH . Item1 - 2 ,
231- Height = maxV . Item2 - maxV . Item1 - 2 ,
232- } ;
253+ var maxH = FlatImage . SegmentsWithMaxDistance ( stripeCH ) ;
254+ var maxV = FlatImage . SegmentsWithMaxDistance ( stripeCV ) ;
255+
256+ return new Geometry . Rectangle
257+ {
258+ X = maxH . Item1 + 1 ,
259+ Y = maxV . Item1 + 1 ,
260+ Width = maxH . Item2 - maxH . Item1 - 2 ,
261+ Height = maxV . Item2 - maxV . Item1 - 2 ,
262+ } ;
263+ }
264+ else
265+ {
266+ return new Geometry . Rectangle ( ) ;
267+ }
233268 }
234269
235270 public bool CompareWithFragment ( FlatImage fragment , int startX , int startY )
@@ -297,7 +332,7 @@ public bool CompareWithFragmentWithTolerance(FlatImage fragment, int startX, int
297332
298333 public unsafe System . Drawing . Bitmap ToBitmap ( )
299334 {
300- var bitmap = new System . Drawing . Bitmap ( this . Width , this . Height , System . Drawing . Imaging . PixelFormat . Format24bppRgb ) ;
335+ var bitmap = new System . Drawing . Bitmap ( this . Width , this . Height , System . Drawing . Imaging . PixelFormat . Format24bppRgb ) ;
301336
302337 const int pixelSize = 3 ;
303338 var rect = new System . Drawing . Rectangle ( 0 , 0 , bitmap . Width , bitmap . Height ) ;
0 commit comments