Author Topic: outline from gadient mask  (Read 1564 times)

yotamarker

  • *******
  • Starship Trooper
  • Posts: 463
outline from gadient mask
« on: March 31, 2017, 03:03:38 AM »
I'm looking for an alg to know if a pixel is an outline pixel based on the pixels around it.

korrelan

  • *
  • Replicant
  • ********
  • Posts: 568
  • Look into my eyes! WOAH!
    • Main YouTube
Re: outline from gadient mask
« Reply #1 on: March 31, 2017, 09:59:06 AM »
Quote
I'm looking for an alg to know if a pixel is an outline pixel based on the pixels around it.

So… a image convolution filter?

There are several methods of obtaining a shape outline from an image and loads of pre-written libraries available.

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

I wrote my own adaptive convolution routines for extracting object boundaries from images.  The main idea is to use colour contrast NOT pixel brightness.  Simply break the image into its RGB components and detect the colour boundaries. 



The image in the center is using brightness as the bias; as you can see the light/ brightness variation causes contours.  Bottom left is the same routine using colour contrast… much better.

Check each pixel with its adjacent RGB values.  If there is a difference within a set bias mark as an outline point/ pixel.

.bias=20
.red = red value of pixel your checking
.ored = red value of adjacent pixel
.If red > ored –bias and red < ored+bias then change=change+1
.do same for green & blue
.if change=3 then draw outline pixel else don’t

I’m not preaching but the best way to understand/ learn this kind of simple stuff is to experiment with your own code. Using someone else’s pre-written routines rarely gives the desired results.


 :)
It thunk... therefore it is!

yotamarker

  • *******
  • Starship Trooper
  • Posts: 463
Re: outline from gadient mask
« Reply #2 on: March 31, 2017, 01:08:33 PM »
that's exactly what i'm talking about but I don't understand what you mean by adjacent pixel
each pixel is surrounded by 8 other pixels

yotamarker

  • *******
  • Starship Trooper
  • Posts: 463
Re: outline from gadient mask
« Reply #3 on: March 31, 2017, 01:45:47 PM »
so I implemented your alg but something is missing.

method in aeye class :
Code: [Select]
  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
        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 = 3 Then
            result = True
        End If
        Return result
    End Function

button click event main code :
Code: [Select]
Imports System.Math
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()

        For i = 1 To bmp.Height - 2
            For j = 1 To bmp.Width - 2
                If Not Aeye.isOutLine(j, i, bmp, 20) Then
                    bmp2 = Aeye.mark_dark_pixel(j, i, bmp2, 1)
                End If






            Next
        Next

        PictureBox2.Image = bmp2.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

result :


keghn

  • *
  • Starship Trooper
  • *******
  • Posts: 335

korrelan

  • *
  • Replicant
  • ********
  • Posts: 568
  • Look into my eyes! WOAH!
    • Main YouTube
Re: outline from gadient mask
« Reply #5 on: March 31, 2017, 02:04:10 PM »
That was just meant to give you the basic idea lol.

If you are reading the RGB value left to right, bottom to top you need to check the pixel either above or below also.

So check color8.r against the target color5.r pixel also. (rgb)

.if change=6 then draw outline pixel else don’t

Also you are not checking between bias…

.If red > ored –bias and red < ored+bias then change=change+1

Also make sure your working in pixel scales not twips etc.

Also you don’t really need the rest of the pixel variables if your not going to use them.

 :)
It thunk... therefore it is!

yotamarker

  • *******
  • Starship Trooper
  • Posts: 463
Re: outline from gadient mask
« Reply #6 on: March 31, 2017, 02:32:56 PM »
it is working thank you very much.
Code: [Select]
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

I'll delete the unused variables also.

korrelan

  • *
  • Replicant
  • ********
  • Posts: 568
  • Look into my eyes! WOAH!
    • Main YouTube
Re: outline from gadient mask
« Reply #7 on: March 31, 2017, 09:04:48 PM »
@yot

You are welcome.

@keghn

I agree both are very good algorithms and well worth exploring/ learning.

I presumed yot was new to this type of coding, and I’m sure you’ll agree… programming should be fun… and I think it’s important to understand exactly what the code does and how it does it.  Using someone else’s library doesn't quite achieve that.

Now I need to read the top of the thread to figure out why yot needs to find the perimeter of a triangle lol.

 :)
It thunk... therefore it is!

keghn

  • *
  • Starship Trooper
  • *******
  • Posts: 335
Re: outline from gadient mask
« Reply #8 on: March 31, 2017, 11:09:17 PM »
 I do not use the common edge detectors to find lines. I use XNOR logic: 

https://techcrunch.com/2017/01/19/xnor-ai-frees-ai-from-the-prison-of-the-supercomputer/

yotamarker

  • *******
  • Starship Trooper
  • Posts: 463
Re: outline from gadient mask
« Reply #9 on: April 01, 2017, 08:38:17 AM »
now I need to get the images objects bounds


I used a recursive function to trace the objects, but it just glitches a lot with stack
overflows and out of range exceptions. so I'm looking for another algorithm.

korrelan

  • *
  • Replicant
  • ********
  • Posts: 568
  • Look into my eyes! WOAH!
    • Main YouTube
Re: outline from gadient mask
« Reply #10 on: April 01, 2017, 11:27:28 AM »
Oh! Now I understand what you’re trying to do. 

You are trying to follow the objects perimeter to define a bounding box around each object in the image; I presume you are then going to try and recognise the objects within the boxes… nah! That isn’t going to work on complex scenes.

Your program is crashing because of your array limits; you only allowed 1000 bounding boxes.  There are loads of problems with this approach to object recognition; besides your routine detecting the JPG compression anomalies in your selected image (bad image choice) defining the outline of a single object is practically impossible.  Without extra information from stereo cameras or depth sensors you will never find the perimeter of an object in a video feed for example.



There are other methods like blobbing that can find the center of ‘mass’ of a block of colour but most objects are composed of more than one colour. You can blob and then run an outline routine etc… there are so many options/ methods. 

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

Then you have to consider things like scale and rotational invariance of objects. 

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

As usual this is a huge complex topic that often requires a custom solution.

A better method/ approach is to use feature detection. 

At its simplest this could mean selecting a ten pixel square section of the image you are wanting to recognise and storing it with a name.  Then scan any selected image for the saved section; chances of finding the exact same block in another image are pretty slim; especially if you select a feature unique to the image.  This technique is used for finding smiling faces in pictures etc; using eigenface templates.

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

This of course is the main technique used in convolutional networks, the image is searched with millions of smaller images of known features… both the image to be searched and the feature image set are run through a convolutional filter to highlight a specific property, lines, contrast gradients etc.

The list of methods and techniques is endless… enjoy learning.

 :)
« Last Edit: April 01, 2017, 12:05:53 PM by korrelan »
It thunk... therefore it is!

yotamarker

  • *******
  • Starship Trooper
  • Posts: 463
Re: outline from gadient mask
« Reply #11 on: April 01, 2017, 01:11:22 PM »
why isn’t it going to work on complex scenes ? I wont be chekin each pixel in the range
just about 8x8 and the corners before that.

yotamarker

  • *******
  • Starship Trooper
  • Posts: 463
Re: outline from gadient mask
« Reply #12 on: April 01, 2017, 01:14:08 PM »
@keghn  that xnor ai link, no doubt they created something cool but, it is useless to me cause it has no algorithms or walkthroughs

korrelan

  • *
  • Replicant
  • ********
  • Posts: 568
  • Look into my eyes! WOAH!
    • Main YouTube
Re: outline from gadient mask
« Reply #13 on: April 01, 2017, 01:25:29 PM »
It thunk... therefore it is!

yotamarker

  • *******
  • Starship Trooper
  • Posts: 463
Re: outline from gadient mask
« Reply #14 on: April 01, 2017, 01:34:54 PM »
they should use videos for the recognition not analyze the juice out of images

 

Welcome

Please login or register.



Login with username, password and session length
Russian Military Robots
by Art (Robotics News)
Today at 06:14:30 PM
DeepMind
by Art (AI News )
April 19, 2017, 06:19:36 PM
A Murder Posted on Facebook Prompts Outrage and Questions Over Responsibility
by Art (AI News )
April 18, 2017, 10:12:52 AM
AI based 3d scanner
by Art (AI News )
April 18, 2017, 10:04:39 AM
Artificial Earthling
by Art (AI News )
April 14, 2017, 03:49:08 PM
Faster websites with fewer bugs
by Tyler (Robotics News)
April 13, 2017, 10:50:45 PM
New Tools Needed to Track Technology’s Impact on Jobs, Panel Says
by Tyler (AI News )
April 13, 2017, 10:50:43 PM
SMART automation
by Tyler (Robotics News)
April 13, 2017, 04:48:34 PM

Users Online

20 Guests, 0 Users

Most Online Today: 39. Most Online Ever: 208 (August 27, 2008, 08:24:30 AM)

Articles