Android Question Why imageview.GetPixel so slow?

Mscal

Member
Licensed User
Sorry for the wrong title,i mean bitmap.getpixel

1.jpg is a 1920*1080 picture

B4X:
dim b As Bitmap
Dim i As Int,j As Int,s as string
b.Initialize(File.DirRootExternal, "1.jpg")
s=""
For i=1 To b.Width-1
        ToastMessageShow(i,True)   '
        DoEvents
        For j=1 To b.Height-1
            Log(i & "," & j)
            If b.GetPixel(i,j)<=-8421505 Then
                s=s & "0"
            Else
                s=s & "1"
            End If
        Next
Next

Here need more than 2min for running the codes,but in VB6 picture.point is very fast.
Is there more fast way to achieve this function?
 

sorex

Expert
Licensed User
Longtime User
is this in debug or release mode? release mode should be faster but there's a lot of pixels to check there (2 million)
 
Upvote 0

Mscal

Member
Licensed User
is this in debug or release mode? release mode should be faster but there's a lot of pixels to check there (2 million)
I tested release mode,slow again.
yes,there are too many points,but in VB6 use picturebox.point is very fast.I think there must be some way faster
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
If I recall right there's a lib that supports fast bitmap > array operations. the lookup in the array should be a lot faster then.
 
Upvote 0

Mscal

Member
Licensed User
What are you trying to achieve?

Why is the DoEvents in there?
I want to convert colour picture to black-white only picture. the doevents is used for running "ToastMessageShow(i,True)" immediately. then I know where the value i goes.
 
Upvote 0

JordiCP

Expert
Licensed User
Longtime User
You code is very slow because of two reasons:
  • Calling getpixel one by one --> instead you should do the conversion only once for instance using this example from Klaus
  • The string statement is highly expensive, since each time a new string is created.--> better use StringBuilder. Or even better, if what you want is to create a new binarized bitmap, work directly with an array and the convert it to bitmap
Results won't be optimal but much faster than now.

Don't know if the lib posted by sorex has this method (binary thresholding), but with JavaCv it is just some lines of code. The problem is that you must setup it and use inline Java which is not obvious

Also, instead of thresholding using a fixed level (0x7F for each channel in this case) you may want to use Otsu Thresholding (adaptive based on image characteristics), look at this link : https://www.b4x.com/android/forum/threads/otsu-thresholding-binarization-of-images.44406/#content
 
Upvote 0

Mscal

Member
Licensed User
You code is very slow because of two reasons:
  • Calling getpixel one by one --> instead you should do the conversion only once for instance using this example from Klaus
  • The string statement is highly expensive, since each time a new string is created.--> better use StringBuilder. Or even better, if what you want is to create a new binarized bitmap, work directly with an array and the convert it to bitmap
Results won't be optimal but much faster than now.

Don't know if the lib posted by sorex has this method (binary thresholding), but with JavaCv it is just some lines of code. The problem is that you must setup it and use inline Java which is not obvious

Also, instead of thresholding using a fixed level (0x7F for each channel in this case) you may want to use Otsu Thresholding (adaptive based on image characteristics), look at this link : https://www.b4x.com/android/forum/threads/otsu-thresholding-binarization-of-images.44406/#content
thank you very much,i'll try it
 
Upvote 0

eps

Expert
Licensed User
Longtime User
I want to convert colour picture to black-white only picture. the doevents is used for running "ToastMessageShow(i,True)" immediately. then I know where the value i goes.

Hmm... I see why you're using DoEvents - but it was my understanding that this also slows down processing as well.

Have you seen this post (it's old - but there is a project attached which may well be of use to you here - in this instance)

https://www.b4x.com/android/forum/t...-of-converting-rgb-images-to-grayscale.44316/
 
Upvote 0
Top