outline from gadient mask

  • 226 Replies
  • 74370 Views
*

yotamarker

  • Trusty Member
  • **********
  • Millennium Man
  • *
  • 1003
  • battle programmer
    • battle programming
outline from gadient mask
« on: March 31, 2017, 04:15:38 am »
I'm looking for an alg to know if a pixel is an outline pixel based on the pixels around it.

*

Korrelan

  • Trusty Member
  • ***********
  • Eve
  • *
  • 1454
  • Look into my eyes! WOAH!
    • YouTube
Re: outline from gadient mask
« Reply #1 on: March 31, 2017, 11:11: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!...    /    Project Page    /    KorrTecx Website

*

yotamarker

  • Trusty Member
  • **********
  • Millennium Man
  • *
  • 1003
  • battle programmer
    • battle programming
Re: outline from gadient mask
« Reply #2 on: March 31, 2017, 02:20: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

  • Trusty Member
  • **********
  • Millennium Man
  • *
  • 1003
  • battle programmer
    • battle programming
Re: outline from gadient mask
« Reply #3 on: March 31, 2017, 02:57:47 pm »
so I implemented your alg but something is missing.

method in aeye class :
Code
  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
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

  • Trusty Member
  • *********
  • Terminator
  • *
  • 824

*

Korrelan

  • Trusty Member
  • ***********
  • Eve
  • *
  • 1454
  • Look into my eyes! WOAH!
    • YouTube
Re: outline from gadient mask
« Reply #5 on: March 31, 2017, 03:16: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!...    /    Project Page    /    KorrTecx Website

*

yotamarker

  • Trusty Member
  • **********
  • Millennium Man
  • *
  • 1003
  • battle programmer
    • battle programming
Re: outline from gadient mask
« Reply #6 on: March 31, 2017, 03:44:56 pm »
it is working thank you very much.
Code
 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

  • Trusty Member
  • ***********
  • Eve
  • *
  • 1454
  • Look into my eyes! WOAH!
    • YouTube
Re: outline from gadient mask
« Reply #7 on: March 31, 2017, 10:16: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!...    /    Project Page    /    KorrTecx Website

*

keghn

  • Trusty Member
  • *********
  • Terminator
  • *
  • 824
Re: outline from gadient mask
« Reply #8 on: April 01, 2017, 12:21:17 am »
 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

  • Trusty Member
  • **********
  • Millennium Man
  • *
  • 1003
  • battle programmer
    • battle programming
Re: outline from gadient mask
« Reply #9 on: April 01, 2017, 09:50:17 am »
now I need to get the images objects bounds
https://www.youtube.com/watch?v=5m3_2C8xrFI&t=204s

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

  • Trusty Member
  • ***********
  • Eve
  • *
  • 1454
  • Look into my eyes! WOAH!
    • YouTube
Re: outline from gadient mask
« Reply #10 on: April 01, 2017, 12:39:28 pm »
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, 01:17:53 pm by korrelan »
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 #11 on: April 01, 2017, 02:23: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

  • Trusty Member
  • **********
  • Millennium Man
  • *
  • 1003
  • battle programmer
    • battle programming
Re: outline from gadient mask
« Reply #12 on: April 01, 2017, 02:26: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

  • Trusty Member
  • ***********
  • Eve
  • *
  • 1454
  • Look into my eyes! WOAH!
    • YouTube
Re: outline from gadient mask
« Reply #13 on: April 01, 2017, 02:37:29 pm »
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 #14 on: April 01, 2017, 02:46:54 pm »
they should use videos for the recognition not analyze the juice out of images

 


OpenAI Speech-to-Speech Reasoning Demo
by MikeB (AI News )
March 15, 2024, 08:14:02 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
AI-Generated Art Cannot Receive Copyrights
by frankinstien (AI News )
August 24, 2023, 08:49:45 am

Users Online

215 Guests, 0 Users

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

Articles