outline from gadient mask

  • 226 Replies
  • 74492 Views
*

keghn

  • Trusty Member
  • *********
  • Terminator
  • *
  • 824
Re: outline from gadient mask
« Reply #15 on: April 01, 2017, 02:59:19 pm »
 @YotomMaker. XNor is about breaking a image up into a grid of squares, in a quad tree fashion is ok. Then compare
average color or grey scale to each other square. Using XOR logic to compare adjacent  squares.  If they are not equal then there is a line between them.
 If not they are the same blob. Once you change a image to lines then you can convert them into chain code,

 Finding the contours: 

http://answers.opencv.org/question/88501/how-to-classify-chain-codes/

https://pdfs.semanticscholar.org/eb33/e3215bf023d4e8b1dbcaae37bbfb7ae5e3cc.pdf

https://en.wikipedia.org/wiki/Ramer%E2%80%93Douglas%E2%80%93Peucker_algorithm

https://www.youtube.com/watch?v=TS_WFAVeHuI
« Last Edit: April 01, 2017, 03:38:31 pm by keghn »

*

yotamarker

  • Trusty Member
  • **********
  • Millennium Man
  • *
  • 1003
  • battle programmer
    • battle programming
Re: outline from gadient mask
« Reply #16 on: April 01, 2017, 05:04:28 pm »
maybe you can figure it out
Code
Imports System.Math
Imports WindowsApp1.rinegan

Public Class Form1
    Dim eye1 As New Aeye()
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim bmp As New Bitmap("c:\testimage\x1.bmp")
        Dim bmp2 As Bitmap = bmp.Clone()
        PictureBox1.Image = bmp.Clone()

        Dim imageMatrix(bmp.Width, bmp.Height) As Boolean
        Dim hx As New List(Of Integer)
        Dim lx As New List(Of Integer)
        Dim hy As New List(Of Integer)
        Dim ly As New List(Of Integer)
        hx.Add(0)
        hy.Add(0)
        lx.Add(bmp.Width)
        ly.Add(bmp.Height)

        Dim bounderArray(3000) As List(Of Point)
        Dim bounderArrayindex As Integer = 0

        bounderArray(bounderArrayindex) = New List(Of Point)
        bounderArray(bounderArrayindex).Add(New Point(0, 0))
        For i = 1 To bmp.Height - 2
            For j = 1 To bmp.Width - 2
                imageMatrix(j, i) = Not Aeye.isOutLine(j, i, bmp, 20)
                If imageMatrix(j, i) Then
                    bmp2 = Aeye.mark_dark_pixel(j, i, bmp2, 1)
                    If Not imageMatrix(j - 1, i) And Not imageMatrix(j, i - 1) Then 'new outline point
                        bounderArrayindex += 1
                        bounderArray(bounderArrayindex) = New List(Of Point)
                        bounderArray(bounderArrayindex).Add(New Point(j, i))

                        hx.Add(0)
                        hy.Add(0)
                        lx.Add(bmp.Width)
                        ly.Add(bmp.Height)
                        Aeye.maxer(j, hx(bounderArrayindex))
                        Aeye.maxer(i, hy(bounderArrayindex))
                        Aeye.miner(j, lx(bounderArrayindex))
                        Aeye.miner(i, ly(bounderArrayindex))

                    ElseIf imageMatrix(j, i - 1) Then
                        For index = 0 To bounderArrayindex
                            If bounderArray(index).Contains(New Point(j, i - 1)) Then
                                bounderArray(index).Add(New Point(j, i))
                                Aeye.maxer(j, hx(bounderArrayindex))
                                Aeye.maxer(i, hy(bounderArrayindex))
                                Aeye.miner(j, lx(bounderArrayindex))
                                Aeye.miner(i, ly(bounderArrayindex))
                                Exit For
                            End If
                        Next
                    Else
                        For index = 0 To bounderArrayindex
                            If bounderArray(index).Contains(New Point(j, i - 1)) Then
                                bounderArray(index).Add(New Point(j, i))
                                Aeye.maxer(j, hx(bounderArrayindex))
                                Aeye.maxer(i, hy(bounderArrayindex))
                                Aeye.miner(j, lx(bounderArrayindex))
                                Aeye.miner(i, ly(bounderArrayindex))
                                Exit For
                            End If
                        Next
                    End If
                End If
            Next
        Next

        For index = 0 To bounderArrayindex
            bmp = Aeye.graphicContour(bmp, lx(index), hx(index), ly(index), hy(index))
        Next
        PictureBox2.Image = bmp.Clone()

    End Sub

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim bmp As New Bitmap("c:\testimage\x1.bmp")
        PictureBox1.Image = bmp.Clone()
    End Sub
End Class

*

yotamarker

  • Trusty Member
  • **********
  • Millennium Man
  • *
  • 1003
  • battle programmer
    • battle programming
Re: outline from gadient mask
« Reply #17 on: April 01, 2017, 06:18:05 pm »
here is the class with the tracer how come it only works 4 simple shapes is beyond me

Code
Imports System.Math
Public Class Aeye
    Public outlinePixel As Integer = 30
    Dim Imageslist As New List(Of ImageList)
    Dim ImageListCounter As Integer = 0
    Dim minX, minY, maxX, maxY As Integer
    Dim checkedMatrix(1000, 1000) As Boolean
    Dim imageMatrix(1000, 1000) As Boolean
    Dim HX(1000) As Integer
    Dim LX(1000) As Integer
    Dim HY(1000) As Integer
    Dim LY(1000) As Integer
    Dim Icounter As Integer
    Public Shared Function is_pixel_dark_at(ByVal xPos As Integer, ByVal Ypos As Integer, ByVal image As Bitmap, ByVal DarkPixel As Integer) As Boolean
        Dim color As Color
        Dim r, g, b As Integer
        color = image.GetPixel(xPos, Ypos)
        r = color.R
        g = color.G
        b = color.B
        If (r < DarkPixel) And (g < DarkPixel) And (b < DarkPixel) Then
            Return True
        Else
            Return False
        End If
    End Function
    Public Shared Function isOutLine(ByVal xPos As Integer, ByVal Ypos As Integer, ByVal image As Bitmap, ByVal bias As Integer) As Boolean

        Dim result As Boolean = False
        Dim change As Byte = 0
        If xPos > 0 And xPos < image.Width - 1 And Ypos > 0 And Ypos < image.Height - 1 Then
            Dim color1, color2, color3, color4, color5, color6, color7, color8, color9 As Color
            color1 = image.GetPixel(xPos - 1, Ypos - 1)
            color2 = image.GetPixel(xPos, Ypos - 1)
            color3 = image.GetPixel(xPos + 1, Ypos - 1)
            color7 = image.GetPixel(xPos - 1, Ypos + 1)
            color8 = image.GetPixel(xPos, Ypos + 1)
            color9 = image.GetPixel(xPos + 1, Ypos + 1)
            color4 = image.GetPixel(xPos - 1, Ypos)
            color6 = image.GetPixel(xPos + 1, Ypos)
            color5 = image.GetPixel(xPos, Ypos)

            If (CType(color5.R, Integer) > CType(color4.R, Integer) - bias) And (CType(color5.R, Integer) < CType(color4.R, Integer) + bias) Then
                change += 1

            End If
            If (CType(color5.G, Integer) > CType(color4.G, Integer) - bias) And (CType(color5.G, Integer) < CType(color4.G, Integer) + bias) Then
                change += 1

            End If
            If (CType(color5.B, Integer) > CType(color4.B, Integer) - bias) And (CType(color5.B, Integer) < CType(color4.B, Integer) + bias) Then
                change += 1

            End If
            If (CType(color5.R, Integer) > CType(color8.R, Integer) - bias) And (CType(color5.R, Integer) < CType(color8.R, Integer) + bias) Then
                change += 1

            End If
            If (CType(color5.G, Integer) > CType(color8.G, Integer) - bias) And (CType(color5.G, Integer) < CType(color8.G, Integer) + bias) Then
                change += 1

            End If
            If (CType(color5.B, Integer) > CType(color8.B, Integer) - bias) And (CType(color5.B, Integer) < CType(color8.B, Integer) + bias) Then
                change += 1

            End If
        End If

        'Dim sumR, sumG, sumB As Integer

        'sumR = (CType(color1.R, Integer) + CType(color2.R, Integer) + CType(color3.R, Integer))
        'sumR -= (CType(color7.R, Integer) + CType(color8.R, Integer) + CType(color9.R, Integer))
        ''sumG = (CType(color7.R, Integer) + CType(color8.R, Integer) + CType(color9.R, Integer))
        ''sumG -= (CType(color1.G, Integer) + CType(color2.G, Integer) + CType(color3.G, Integer))
        ''sumB = (CType(color1.B, Integer) + CType(color2.B, Integer) + CType(color3.B, Integer))
        ''sumB -= (CType(color7.B, Integer) + CType(color8.B, Integer) + CType(color9.B, Integer))
        'sumR = Abs(sumR)
        ''sumG = Abs(sumG)
        ''sumB = Abs(sumB)
        ''Dim sumR2, sumG2, sumB2 As Integer
        ''sumR2 = (CType(color1.R, Integer) + CType(color4.R, Integer) + CType(color7.R, Integer))
        ''sumG2 = (CType(color1.G, Integer) + CType(color4.G, Integer) + CType(color7.G, Integer))
        ''sumB2 = (CType(color1.B, Integer) + CType(color4.B, Integer) + CType(color7.B, Integer))
        ''sumR2 -= (CType(color3.R, Integer) + CType(color6.R, Integer) + CType(color9.R, Integer))
        ''sumG2 -= (CType(color3.G, Integer) + CType(color6.G, Integer) + CType(color9.G, Integer))
        ''sumB2 -= (CType(color3.B, Integer) + CType(color6.B, Integer) + CType(color9.B, Integer))
        ''sumR2 = Abs(sumR2)
        ''sumG2 = Abs(sumG2)
        ''sumB2 = Abs(sumB2)

        ''sumR2 = color1.R + color4.R + color7.R
        ''sumG2 = color1.G + color4.G + color7.G
        ''sumB2 = color1.B + color4.B + color7.B
        ''sumR2 -= color3.R + color6.R + color9.R
        ''sumG2 -= color3.G + color6.G + color9.G
        ''sumB2 -= color3.B + color6.B + color9.B
        If change = 6 Then
            result = True
        End If
        Return result
    End Function
    Public Shared Function mark_dark_pixel(ByVal x As Integer, ByVal y As Integer, ByVal bmp1 As Bitmap, ByVal marker As Byte)
        For i As Integer = 0 To marker
            For j As Integer = 0 To marker
                Try
                    bmp1.SetPixel(x + j, y + i, Color.Green)
                Catch ex As Exception

                End Try
            Next
        Next
        Return bmp1

    End Function
    Public Function contourImage(ByVal bmp As Bitmap) As Bitmap

        For index = 0 To bmp.Height - 1
            For j = 0 To bmp.Width - 1
                checkedMatrix(j, index) = False
                imageMatrix(j, index) = is_pixel_dark_at(j, index, bmp, 30)
                'If imageMatrix(j, index) Then
                '    MsgBox(j, index)
                'End If
            Next

        Next
        minX = bmp.Width
        minY = bmp.Height
        maxX = 0
        maxY = 0
        Dim jindex As Integer = 0
        Icounter = 0
        For i = 0 To bmp.Height - 1 Step 5

            While jindex < bmp.Width - 1
                If Not checkedMatrix(jindex, i) Then
                    If Not imageMatrix(jindex, i) Then
                        checkedMatrix(jindex, i) = True
                    Else
                        tracer(bmp, bmp.Width, bmp.Height, jindex, i)
                        HX(Icounter) = maxX
                        LX(Icounter) = minX
                        HY(Icounter) = maxY
                        LY(Icounter) = minY
                        Icounter += 1
                        minX = bmp.Width
                        minY = bmp.Height
                        maxX = 0
                        maxY = 0
                        jindex = jumpImage(jindex, i)
                    End If
                End If



                jindex += 5
            End While
            jindex = 0
        Next

        For index = 0 To Icounter
            bmp = graphicContour(bmp, LX(index), HX(index), LY(index), HY(index))
        Next

        Return bmp

    End Function
    Public Function jumpImage(ByVal x, y) As Integer
        If Icounter > 999 Then
            Icounter = 999
        End If
        Dim n As Integer = 0
        For index = 0 To Icounter
            If (LX(index) <= x And HX(index) >= x) And (LY(index) <= y And HY(index) >= y) Then
                n = HX(index)
            End If
        Next
        Return n
    End Function
    Public Shared Function graphicContour(ByVal bmp As Bitmap, ByVal xmin As Integer, ByVal xmax As Integer, ByVal ymin As Integer, ByVal ymax As Integer) As Bitmap
        For i = xmin To xmax
            bmp = mark_dark_pixel(i, ymin, bmp, 1)
            bmp = mark_dark_pixel(i, ymax, bmp, 1)
        Next
        For i = ymin To ymax
            bmp = mark_dark_pixel(xmin, i, bmp, 1)
            bmp = mark_dark_pixel(xmax, i, bmp, 1)
        Next
        Return bmp
    End Function
    Private Sub tracer(ByVal bmp1 As Bitmap, ByVal w1 As Integer, ByVal h1 As Integer, ByVal x As Integer, ByVal y As Integer)
        If (x > 0 And x < w1) And (y > 0 And y < h1) Then
            If Not checkedMatrix(x, y) Then
                checkedMatrix(x, y) = True
                If imageMatrix(x, y) Then 'not black pixel
                    maxer(x, maxX)
                    maxer(y, maxY)
                    miner(x, minX)
                    miner(y, minY)
                    tracer(bmp1, w1, h1, x - 5, y - 5)
                    tracer(bmp1, w1, h1, x, y - 5)
                    tracer(bmp1, w1, h1, x + 5, y - 5)
                    tracer(bmp1, w1, h1, x - 5, y)
                    tracer(bmp1, w1, h1, x + 5, y)
                    tracer(bmp1, w1, h1, x - 5, y + 5)
                    tracer(bmp1, w1, h1, x, y + 5)
                    tracer(bmp1, w1, h1, x + 5, y + 5)
                End If
            End If
        End If
    End Sub
    Public Sub maxer(ByVal a As Integer, ByRef b As Integer)
        If a > b Then
            b = a
        End If
    End Sub
    Public Sub miner(ByVal a As Integer, ByRef b As Integer)
        If a < b Then
            b = a
        End If
    End Sub

End Class
Public Class ImageList
    Public minX As Integer
    Public minY As Integer
    Public maxX As Integer
    Public maxY As Integer
End Class

*

keghn

  • Trusty Member
  • *********
  • Terminator
  • *
  • 824
Re: outline from gadient mask
« Reply #18 on: April 01, 2017, 08:05:29 pm »
 I thought it is working the way you wanted?
 I only work in C/C++ and bash.

*

yotamarker

  • Trusty Member
  • **********
  • Millennium Man
  • *
  • 1003
  • battle programmer
    • battle programming
Re: outline from gadient mask
« Reply #19 on: April 01, 2017, 08:27:25 pm »
the traces errors for complex images.
stack overflow, app gets stuck, out of range exceptions, just a leaf in the image
is enough to give it a seizure.

*

yotamarker

  • Trusty Member
  • **********
  • Millennium Man
  • *
  • 1003
  • battle programmer
    • battle programming
Re: outline from gadient mask
« Reply #20 on: April 01, 2017, 09:27:10 pm »

*

Korrelan

  • Trusty Member
  • ***********
  • Eve
  • *
  • 1454
  • Look into my eyes! WOAH!
    • YouTube
Re: outline from gadient mask
« Reply #21 on: April 02, 2017, 10:35:08 am »
It’s really hard too see what’s happening in the vid lol.

As I explained earlier one of the main problems you’re going to encounter is JPG compression artifacts.  Try to use BMP or TIF images for your trace experiments.

https://en.wikipedia.org/wiki/Compression_artifact

If the program is working on simple images then it’s probably a complexity explosion problem.

Also change the program so you can see just the outline in the right picture box.  Then superimpose your boundaries on it so you can better see the problems.  Adapt the visual output of the code so you can visually debug it.

 :)
It thunk... therefore it is!...    /    Project Page    /    KorrTecx Website

*

yotamarker

  • Trusty Member
  • **********
  • Millennium Man
  • *
  • 1003
  • battle programmer
    • battle programming
Re: outline from gadient mask
« Reply #22 on: April 02, 2017, 03:50:16 pm »
phase 1 getting the horizontal bounds.
the problem is with the picture with that little Russian girl with leafs behind her and the ryu pic

*

Korrelan

  • Trusty Member
  • ***********
  • Eve
  • *
  • 1454
  • Look into my eyes! WOAH!
    • YouTube
Re: outline from gadient mask
« Reply #23 on: April 02, 2017, 05:50:36 pm »
OK I've been out for a steak and a few pints... And I really don't mean to sound sarcastic but... Could it be the outlines of the four million, billion, trillion leaves that's overwhelming your array bounds?

 :)
It thunk... therefore it is!...    /    Project Page    /    KorrTecx Website

*

yotamarker

  • Trusty Member
  • **********
  • Millennium Man
  • *
  • 1003
  • battle programmer
    • battle programming
Re: outline from gadient mask
« Reply #24 on: April 02, 2017, 06:28:02 pm »
yes, originally I thought separating the objects by "not out line pixel line"
would work for all cases. I think to solve this I'll have to consider also
lines with consecutive clusters of none outline as "not out line pixel line" may solve it.

what do you think ?

also I never drank alcohol what does it feel like ?

*

yotamarker

  • Trusty Member
  • **********
  • Millennium Man
  • *
  • 1003
  • battle programmer
    • battle programming
Re: outline from gadient mask
« Reply #25 on: April 03, 2017, 04:42:05 pm »
it pretty much works now, I"ll need also to address the special cases
I need to make 3 videos :
1 sweet it is solved video
3 explaining the algorithm
2 ranting about the hell I went through

*

yotamarker

  • Trusty Member
  • **********
  • Millennium Man
  • *
  • 1003
  • battle programmer
    • battle programming

*

yotamarker

  • Trusty Member
  • **********
  • Millennium Man
  • *
  • 1003
  • battle programmer
    • battle programming
Re: outline from gadient mask
« Reply #27 on: April 06, 2017, 06:25:22 pm »
now it is needed to get each of the overlapping image in the rectangle separately
in the example below it needs a rectangle for the chair and a rectangle for the guy.



any algorithm suggestions ?

*

keghn

  • Trusty Member
  • *********
  • Terminator
  • *
  • 824
Re: outline from gadient mask
« Reply #28 on: April 07, 2017, 12:13:15 am »
 A trained Neural Network can do that. Or a video tracking algorithm that tracks the person getting in and out of the wheel chair.
 It would pull information from other frames.
 Tracks two objects to their collision or merging.

*

yotamarker

  • Trusty Member
  • **********
  • Millennium Man
  • *
  • 1003
  • battle programmer
    • battle programming
Re: outline from gadient mask
« Reply #29 on: April 07, 2017, 01:32:05 am »
he isn't getting out of the chair because it is a sign.

 


OpenAI Speech-to-Speech Reasoning Demo
by ivan.moony (AI News )
March 28, 2024, 01:31:53 pm
Say good-bye to GPUs...
by MikeB (AI News )
March 23, 2024, 09:23:52 am
Google Bard report
by ivan.moony (AI News )
February 14, 2024, 04:42:23 pm
Elon Musk's xAI Grok Chatbot
by MikeB (AI News )
December 11, 2023, 06:26:33 am
Nvidia Hype
by 8pla.net (AI News )
December 06, 2023, 10:04:52 pm
How will the OpenAI CEO being Fired affect ChatGPT?
by 8pla.net (AI News )
December 06, 2023, 09:54:25 pm
Independent AI sovereignties
by WriterOfMinds (AI News )
November 08, 2023, 04:51:21 am
LLaMA2 Meta's chatbot released
by 8pla.net (AI News )
October 18, 2023, 11:41:21 pm

Users Online

263 Guests, 0 Users

Most Online Today: 363. Most Online Ever: 2369 (November 21, 2020, 04:08:13 pm)

Articles