Share My Creation Photo to sketch. B4A can do it

This was the result..
1733158540678.png
1733158688530.png
 

Attachments

  • sketch.png
    sketch.png
    212.8 KB · Views: 30

toby

Well-Known Member
Licensed User
Longtime User
Even though this thread is called "Share My Creations", I'm surprised that the OP has so far refused to share anything, except some sketch images, about his creation, whether it's in the Google play store so that people can download to try it out, or providing the source code for others to study. I here provide my similar test app source code for those interested in it to take a look and to feel free to improve. The test app used one of his original photos and produced attached sketch. Enjoy!
B4X:
Sub Globals
    Dim imgView As ImageView ' ImageView to display the photo
    Dim btnSketch As Button ' Button to trigger the sketch effect
    Dim lightFactor As Int = 150 ' Factor to make the sketch lighter
End Sub

Sub Activity_Create(FirstTime As Boolean)
    ' Initialize UI components
    imgView.Initialize("")
    btnSketch.Initialize("btnSketch")

    ' Add UI components to the activity
    Activity.AddView(imgView, 10dip, 10dip, 300dip, 300dip)
    Activity.AddView(btnSketch, 10dip, 320dip, 100dip, 50dip)
    btnSketch.Text = "Sketch"

    ' Load a sample image (replace with your own image path)
    Dim bmp As Bitmap
    bmp.Initialize(File.DirAssets, "girl.png")
    imgView.Bitmap = bmp
End Sub

Sub btnSketch_Click
    Dim originalBmp As Bitmap = imgView.Bitmap
    Dim sketchBmp As Bitmap = ConvertToSketch(originalBmp)
    imgView.Bitmap = sketchBmp
End Sub

Sub ConvertToSketch(originalBmp As Bitmap) As Bitmap
    Dim bc As BitmapCreator
    bc.Initialize(originalBmp.Width, originalBmp.Height)
    
    ' Load the original bitmap into BitmapCreator
    bc.CopyPixelsFromBitmap(originalBmp)

    Dim width As Int = bc.mWidth
    Dim height As Int = bc.mHeight
    
    ' Convert the entire image to grayscale
    Dim grayBC As BitmapCreator
    grayBC.Initialize(width, height)
    
    Dim argb As ARGBColor
    Dim grayMatrix(width, height) As Int
    
    For x = 0 To width - 1
        For y = 0 To height - 1
            bc.GetARGB(x, y, argb)
            ' Convert to grayscale
            Dim gray As Int = (argb.r + argb.g + argb.b) / 3
            grayMatrix(x, y) = gray
            ' Apply grayscale value to grayBC
            argb.r = gray
            argb.g = gray
            argb.b = gray
            grayBC.SetARGB(x, y, argb)
        Next
    Next

    ' Edge detection (Sobel filter)
    Dim edgeBC As BitmapCreator
    edgeBC.Initialize(width, height)

    For x = 1 To width - 2
        For y = 1 To height - 2
            ' Sobel X and Y gradients
            Dim gX As Int = (grayMatrix(x + 1, y - 1) + 2 * grayMatrix(x + 1, y) + grayMatrix(x + 1, y + 1)) - _
                            (grayMatrix(x - 1, y - 1) + 2 * grayMatrix(x - 1, y) + grayMatrix(x - 1, y + 1))
            
            Dim gY As Int = (grayMatrix(x - 1, y + 1) + 2 * grayMatrix(x, y + 1) + grayMatrix(x + 1, y + 1)) - _
                            (grayMatrix(x - 1, y - 1) + 2 * grayMatrix(x, y - 1) + grayMatrix(x + 1, y - 1))
            
            Dim magnitude As Int = Sqrt(gX * gX + gY * gY)
            If magnitude > 255 Then magnitude = 255
            
            ' Invert edges for sketch effect
            Dim invertedMagnitude As Int = 255 - magnitude
            
            ' Apply edge detection result
            argb.a = 255 ' Full opacity
            argb.r = invertedMagnitude
            argb.g = invertedMagnitude
            argb.b = invertedMagnitude
            
            edgeBC.SetARGB(x, y, argb)
        Next
    Next

    ' Blend edges with a lighter background for sketch effect
    Dim sketchBC As BitmapCreator
    sketchBC.Initialize(width, height)

    For x = 0 To width - 1
        For y = 0 To height - 1
            ' Get the edge pixel
            edgeBC.GetARGB(x, y, argb)
            
            ' Blend with white (lighter background)
            Dim sketchRed As Int = Min(255, argb.r + 50)
            Dim sketchGreen As Int = Min(255, argb.g + 50)
            Dim sketchBlue As Int = Min(255, argb.b + 50)
            
            ' Set the sketch pixel
            argb.r = sketchRed
            argb.g = sketchGreen
            argb.b = sketchBlue
            sketchBC.SetARGB(x, y, argb)
        Next
    Next

    ' Return the final sketch Bitmap
    Return sketchBC.Bitmap
End Sub

sketch-effect.jpg
 

jkhazraji

Active Member
Licensed User
Longtime User
Even though this thread is called "Share My Creations", I'm surprised that the OP has so far refused to share anything, except some sketch images, about his creation, whether it's in the Google play store so that people can download to try it out, or providing the source code for others to study. I here provide my similar test app source code for those interested in it to take a look and to feel free to improve. The test app used one of his original photos and produced attached sketch. Enjoy!
B4X:
Sub Globals
    Dim imgView As ImageView ' ImageView to display the photo
    Dim btnSketch As Button ' Button to trigger the sketch effect
    Dim lightFactor As Int = 150 ' Factor to make the sketch lighter
End Sub

Sub Activity_Create(FirstTime As Boolean)
    ' Initialize UI components
    imgView.Initialize("")
    btnSketch.Initialize("btnSketch")

    ' Add UI components to the activity
    Activity.AddView(imgView, 10dip, 10dip, 300dip, 300dip)
    Activity.AddView(btnSketch, 10dip, 320dip, 100dip, 50dip)
    btnSketch.Text = "Sketch"

    ' Load a sample image (replace with your own image path)
    Dim bmp As Bitmap
    bmp.Initialize(File.DirAssets, "girl.png")
    imgView.Bitmap = bmp
End Sub

Sub btnSketch_Click
    Dim originalBmp As Bitmap = imgView.Bitmap
    Dim sketchBmp As Bitmap = ConvertToSketch(originalBmp)
    imgView.Bitmap = sketchBmp
End Sub

Sub ConvertToSketch(originalBmp As Bitmap) As Bitmap
    Dim bc As BitmapCreator
    bc.Initialize(originalBmp.Width, originalBmp.Height)
   
    ' Load the original bitmap into BitmapCreator
    bc.CopyPixelsFromBitmap(originalBmp)

    Dim width As Int = bc.mWidth
    Dim height As Int = bc.mHeight
   
    ' Convert the entire image to grayscale
    Dim grayBC As BitmapCreator
    grayBC.Initialize(width, height)
   
    Dim argb As ARGBColor
    Dim grayMatrix(width, height) As Int
   
    For x = 0 To width - 1
        For y = 0 To height - 1
            bc.GetARGB(x, y, argb)
            ' Convert to grayscale
            Dim gray As Int = (argb.r + argb.g + argb.b) / 3
            grayMatrix(x, y) = gray
            ' Apply grayscale value to grayBC
            argb.r = gray
            argb.g = gray
            argb.b = gray
            grayBC.SetARGB(x, y, argb)
        Next
    Next

    ' Edge detection (Sobel filter)
    Dim edgeBC As BitmapCreator
    edgeBC.Initialize(width, height)

    For x = 1 To width - 2
        For y = 1 To height - 2
            ' Sobel X and Y gradients
            Dim gX As Int = (grayMatrix(x + 1, y - 1) + 2 * grayMatrix(x + 1, y) + grayMatrix(x + 1, y + 1)) - _
                            (grayMatrix(x - 1, y - 1) + 2 * grayMatrix(x - 1, y) + grayMatrix(x - 1, y + 1))
           
            Dim gY As Int = (grayMatrix(x - 1, y + 1) + 2 * grayMatrix(x, y + 1) + grayMatrix(x + 1, y + 1)) - _
                            (grayMatrix(x - 1, y - 1) + 2 * grayMatrix(x, y - 1) + grayMatrix(x + 1, y - 1))
           
            Dim magnitude As Int = Sqrt(gX * gX + gY * gY)
            If magnitude > 255 Then magnitude = 255
           
            ' Invert edges for sketch effect
            Dim invertedMagnitude As Int = 255 - magnitude
           
            ' Apply edge detection result
            argb.a = 255 ' Full opacity
            argb.r = invertedMagnitude
            argb.g = invertedMagnitude
            argb.b = invertedMagnitude
           
            edgeBC.SetARGB(x, y, argb)
        Next
    Next

    ' Blend edges with a lighter background for sketch effect
    Dim sketchBC As BitmapCreator
    sketchBC.Initialize(width, height)

    For x = 0 To width - 1
        For y = 0 To height - 1
            ' Get the edge pixel
            edgeBC.GetARGB(x, y, argb)
           
            ' Blend with white (lighter background)
            Dim sketchRed As Int = Min(255, argb.r + 50)
            Dim sketchGreen As Int = Min(255, argb.g + 50)
            Dim sketchBlue As Int = Min(255, argb.b + 50)
           
            ' Set the sketch pixel
            argb.r = sketchRed
            argb.g = sketchGreen
            argb.b = sketchBlue
            sketchBC.SetARGB(x, y, argb)
        Next
    Next

    ' Return the final sketch Bitmap
    Return sketchBC.Bitmap
End Sub

View attachment 159337
You can say what you said and do what you did in a better way.
 

jkhazraji

Active Member
Licensed User
Longtime User
You are right.. your's is much better. Forget AI
Indeed I am using the Photoshop algorithm to do the sketching: Convert to Gray, Invert, Apply Gaussian Blur, and finally Color Dodge Blend. One can control the rradius of the sketch by changing the radius of Gaussian blur.
 
Last edited:

Beja

Expert
Licensed User
Longtime User
Even though this thread is called "Share My Creations", I'm surprised that the OP has so far refused to share anything, except some sketch images, about his creation, whether it's in the Google play store so that people can download to try it out, or providing the source code for others to study. I here provide my similar test app source code for those interested in it to take a look and to feel free to improve. The test app used one of his original photos and produced attached sketch. Enjoy!
B4X:
Sub Globals
    Dim imgView As ImageView ' ImageView to display the photo
    Dim btnSketch As Button ' Button to trigger the sketch effect
    Dim lightFactor As Int = 150 ' Factor to make the sketch lighter
End Sub

Sub Activity_Create(FirstTime As Boolean)
    ' Initialize UI components
    imgView.Initialize("")
    btnSketch.Initialize("btnSketch")

    ' Add UI components to the activity
    Activity.AddView(imgView, 10dip, 10dip, 300dip, 300dip)
    Activity.AddView(btnSketch, 10dip, 320dip, 100dip, 50dip)
    btnSketch.Text = "Sketch"

    ' Load a sample image (replace with your own image path)
    Dim bmp As Bitmap
    bmp.Initialize(File.DirAssets, "girl.png")
    imgView.Bitmap = bmp
End Sub

Sub btnSketch_Click
    Dim originalBmp As Bitmap = imgView.Bitmap
    Dim sketchBmp As Bitmap = ConvertToSketch(originalBmp)
    imgView.Bitmap = sketchBmp
End Sub

Sub ConvertToSketch(originalBmp As Bitmap) As Bitmap
    Dim bc As BitmapCreator
    bc.Initialize(originalBmp.Width, originalBmp.Height)
   
    ' Load the original bitmap into BitmapCreator
    bc.CopyPixelsFromBitmap(originalBmp)

    Dim width As Int = bc.mWidth
    Dim height As Int = bc.mHeight
   
    ' Convert the entire image to grayscale
    Dim grayBC As BitmapCreator
    grayBC.Initialize(width, height)
   
    Dim argb As ARGBColor
    Dim grayMatrix(width, height) As Int
   
    For x = 0 To width - 1
        For y = 0 To height - 1
            bc.GetARGB(x, y, argb)
            ' Convert to grayscale
            Dim gray As Int = (argb.r + argb.g + argb.b) / 3
            grayMatrix(x, y) = gray
            ' Apply grayscale value to grayBC
            argb.r = gray
            argb.g = gray
            argb.b = gray
            grayBC.SetARGB(x, y, argb)
        Next
    Next

    ' Edge detection (Sobel filter)
    Dim edgeBC As BitmapCreator
    edgeBC.Initialize(width, height)

    For x = 1 To width - 2
        For y = 1 To height - 2
            ' Sobel X and Y gradients
            Dim gX As Int = (grayMatrix(x + 1, y - 1) + 2 * grayMatrix(x + 1, y) + grayMatrix(x + 1, y + 1)) - _
                            (grayMatrix(x - 1, y - 1) + 2 * grayMatrix(x - 1, y) + grayMatrix(x - 1, y + 1))
           
            Dim gY As Int = (grayMatrix(x - 1, y + 1) + 2 * grayMatrix(x, y + 1) + grayMatrix(x + 1, y + 1)) - _
                            (grayMatrix(x - 1, y - 1) + 2 * grayMatrix(x, y - 1) + grayMatrix(x + 1, y - 1))
           
            Dim magnitude As Int = Sqrt(gX * gX + gY * gY)
            If magnitude > 255 Then magnitude = 255
           
            ' Invert edges for sketch effect
            Dim invertedMagnitude As Int = 255 - magnitude
           
            ' Apply edge detection result
            argb.a = 255 ' Full opacity
            argb.r = invertedMagnitude
            argb.g = invertedMagnitude
            argb.b = invertedMagnitude
           
            edgeBC.SetARGB(x, y, argb)
        Next
    Next

    ' Blend edges with a lighter background for sketch effect
    Dim sketchBC As BitmapCreator
    sketchBC.Initialize(width, height)

    For x = 0 To width - 1
        For y = 0 To height - 1
            ' Get the edge pixel
            edgeBC.GetARGB(x, y, argb)
           
            ' Blend with white (lighter background)
            Dim sketchRed As Int = Min(255, argb.r + 50)
            Dim sketchGreen As Int = Min(255, argb.g + 50)
            Dim sketchBlue As Int = Min(255, argb.b + 50)
           
            ' Set the sketch pixel
            argb.r = sketchRed
            argb.g = sketchGreen
            argb.b = sketchBlue
            sketchBC.SetARGB(x, y, argb)
        Next
    Next

    ' Return the final sketch Bitmap
    Return sketchBC.Bitmap
End Sub

Thank you Toby so much for sharing the code>> very useful.
 

jkhazraji

Active Member
Licensed User
Longtime User
TWIMC. See how the sketch is made
1733782830001.png
1733783425821.png
 
Last edited:

AnandGupta

Expert
Licensed User
Longtime User
@jkhazraji did you make any app in B4X for the above ?
You are posting in "Share My Creation", so it is assumed that you have created in B4X, the above app.
Please clarify.
 

jkhazraji

Active Member
Licensed User
Longtime User
@jkhazraji did you make any app in B4X for the above ?
You are posting in "Share My Creation", so it is assumed that you have created in B4X, the above app.
Please clarify.
Maybe I am still newbie in B4X but I am an old member of this community, and I know what to post Sir. It is clear from the title that it is done with b4a and also can be done with b4j and b4i. If you are interested in the code I can give it to you with pleasure. Just DM me.
 
Top