Automatically crop image boundaries

XverhelstX

Well-Known Member
Licensed User
Longtime User
Hey,

I'm using the following code to automatically crop an image to it's boundaries.

B4X:
'Define the boundaries of the image
Sub DefineBoundaries
   Dim color As Int
   Dim top(2), bottom(2), left(2), right(2) As Int
   
   Log("-------TOP--------")
   'Define from top
   For i=0 To btmpCrop.Width-1 
      For j=0 To btmpCrop.Height-1
         color = btmpCrop.GetPixel(i,j)
         If color <> Colors.RGB(255,255,255) Then
            Log("No Color white from top!")
            Log("x: " & i)
            Log("y: " & j)
            top(0) = i
            top(1) = j
            i = btmpCrop.Width
            Exit
         End If
      Next
   Next
   
   Log("-------Left--------")
   'Define from left
   For i=0 To btmpCrop.Height-1 
      For j=0 To btmpCrop.Width-1
         color = btmpCrop.GetPixel(j,i)
         If color <> Colors.RGB(255,255,255) Then
            Log("No Color white from left!")
            Log("x: " & j)
            Log("y: " & i)
            left(0) = j
            left(1) = i
            i = btmpCrop.Height
            Exit
         End If
      Next
   Next

   Log("-------Bottom--------")
   'Define from bottom
   For i=btmpCrop.Width-1 To 0 Step -1
      For j=btmpCrop.Height-1 To 0 Step -1
         color = btmpCrop.GetPixel(i,j)
         If color <> Colors.RGB(255,255,255) Then
            Log("No Color white from bottom!")
            Log("x: " & i)
            Log("y: " & j)
            bottom(0) = i
            bottom(1) = j
            i = 0
            Exit
         End If
      Next
   Next
   
   
   Log("-------Right--------")
   'Define from right
   For i=btmpCrop.Height-1 To 0 Step -1
      For j=btmpCrop.Width-1 To 0 Step -1
         color = btmpCrop.GetPixel(j,i)
         If color <> Colors.RGB(255,255,255) Then
            Log("No Color white from right!")
            Log("x: " & j)
            Log("y: " & i)
            right(0) = j
            right(1) = i
            i = 0
            Exit
         End If
      Next
   Next

   AutoCrop(left, right, top, bottom)
End Sub
,

I created the code from the pseudocode here:
java - Automatically crop image boundaries - Stack Overflow

I want to know how i could improve (the speed) of the getPixel method with getPixels. (from BitmapExtended or RSImageProcessing library.)
Any example would be great.

Regards,
Tomas
 
Top