PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
VS 2015 converting an image to rows and columns-VBForums
Results 1 to 16 of 16

Thread: converting an image to rows and columns

  1. #1

    Thread Starter
    Member
    Join Date
    Feb 2016
    Posts
    47

    converting an image to rows and columns

    Name:  122.jpg
Views: 154
Size:  12.0 KB


    Hello All,
    I would detect the black dots in the attachmed image into rows and columns .
    and get positions in an array like
    (1,2)
    (2,1)
    (2,2)
    ...
    ....

    any help is appreciated.
    Last edited by mrnooo2000; Feb 18th, 2018 at 08:59 PM.

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    100,658

    Re: converting an image to rows and columns

    Quote Originally Posted by mrnooo2000 View Post
    I would detect the black dots in the attachmed image into rows and columns .
    That doesn't really make sense as is. What are you expecting to end up with? My guess would be a list of Point values containing the X and Y coordinates of the dark spots. Are we to assume that the spots will only ever be one pixel is size? Please provide a FULL and CLEAR explanation of the problem.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    100,658

    Re: converting an image to rows and columns

    You can basically use two nested For loops to visit each pixel in the Image and get its Color. If the Color is Black, add the current coordinates to a List(Of Point). You may find the following helpful in speeding up the process:

    http://www.vbforums.com/showthread.p...light=lockbits
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    Member
    Join Date
    Feb 2016
    Posts
    47

    Re: converting an image to rows and columns

    That doesn't really make sense as is. What are you expecting to end up with? My guess would be a list of Point values containing the X and Y coordinates of the dark spots. Are we to assume that the spots will only ever be one pixel is size? Please provide a FULL and CLEAR explanation of the problem.
    no i dont need the pixel
    i need the relative position of the dark dots

    i tried the edge detection but with no success till now

  5. #5
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    100,658

    Re: converting an image to rows and columns

    Quote Originally Posted by mrnooo2000 View Post
    no i dont need the pixel
    i need the relative position of the dark dots
    That clears up... nothing. Position relative to what? Please rethink what the words "full" and "clear" mean. We know nothing about what you're trying to do so you have to provide ALL the relevant information. I have no idea how the example list you provide:
    (1,2)
    (2,1)
    (2,2)
    ...
    ....
    relates to the image you posted. If you make us guess then there's a good chance we'll guess wrong and that's a waste of everyone's time.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6

    Thread Starter
    Member
    Join Date
    Feb 2016
    Posts
    47

    Re: converting an image to rows and columns

    You can basically use two nested For loops to visit each pixel in the Image and get its Color. If the Color is Black, add the current coordinates to a List(Of Point). You may find the following helpful in speeding up the process:
    i tried this way but the problem is the dark dot is not one pixel
    Last edited by mrnooo2000; Feb 18th, 2018 at 09:20 PM.

  7. #7

    Thread Starter
    Member
    Join Date
    Feb 2016
    Posts
    47

    Re: converting an image to rows and columns

    relative to each other
    that means from the top
    i want to detect the first point to be in the first row but it will in the second column cause there is a point before it in the second row

    i hope this clears what i want

  8. #8

    Thread Starter
    Member
    Join Date
    Feb 2016
    Posts
    47

    Re: converting an image to rows and columns

    i hope this pic clears what i want

    Name:  122.jpg
Views: 74
Size:  37.3 KB

  9. #9
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    100,658

    Re: converting an image to rows and columns

    I think so, although I'm not 100% sure. I think that the steps would be:

    1. Create a list of Point values where each Point represents the centre of a spot. I can think of a brute-force way to do that but I've done very little image processing so I don't know if there's an easier or quicker way or what that would be.
    2. Sort that list by X value.
    3. Loop through the Points and replace the X value with a column index. The index would start at 1 and increment each time a Point had a different X value to the one before it.
    4. Sort the list by Y value.
    5. Loop through the Points and replace the Y value with a row index. The index would start at 1 and increment each time a Point had a different Y value to the one before it.

    Once that's done, you can do whatever you like with the list. Here's a method that would perform steps 2 to 5:
    vb.net Code:
    1. Private Sub ConvertAbsoluteCoordinatesToGridCoordinates(points As List(Of Point))
    2.     points.Sort(Function(p1, p2) p1.X.CompareTo(p2.X))
    3.  
    4.     Dim lastX = -1
    5.     Dim columnIndex = 1
    6.  
    7.     For i = 0 To points.Count - 1
    8.         Dim p = points(i)
    9.  
    10.         If p.X <> lastX Then
    11.             lastX = p.X
    12.             columnIndex += 1
    13.         End If
    14.  
    15.         p.X = columnIndex
    16.         points(i) = p
    17.     Next
    18.  
    19.     points.Sort(Function(p1, p2) p1.Y.CompareTo(p2.Y))
    20.  
    21.     Dim lastY = -1
    22.     Dim rowIndex = 1
    23.  
    24.     For i = 0 To points.Count - 1
    25.         Dim p = points(i)
    26.  
    27.         If p.Y <> lastY Then
    28.             lastY = p.Y
    29.             rowIndex += 1
    30.         End If
    31.  
    32.         p.Y = rowIndex
    33.         points(i) = p
    34.     Next
    35. End Sub
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  10. #10

    Thread Starter
    Member
    Join Date
    Feb 2016
    Posts
    47

    Re: converting an image to rows and columns

    1. Create a list of Point values where each Point represents the centre of a spot.
    yes that is exactlly what i want to do

  11. #11
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    100,658

    Re: converting an image to rows and columns

    Quote Originally Posted by mrnooo2000 View Post
    yes that is exactlly what i want to do
    One has to wonder why that wasn't stated in the first post then. That's the sort of thing that a full and clear description includes.

    As I said, I could only recommend a brute-force approach, i.e. loop over the pixels and, when a black one is found, look for adjacent pixels of the same colour and use the middle-centre one to represent the spot. There's probably better ways but I can't help you with that.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  12. #12

    Thread Starter
    Member
    Join Date
    Feb 2016
    Posts
    47

    Re: converting an image to rows and columns

    thanks a lot i'm traying to do that

  13. #13

    Thread Starter
    Member
    Join Date
    Feb 2016
    Posts
    47

    Re: converting an image to rows and columns

    i found this liberary for shape detection

    http://www.aforgenet.com/articles/shape_checker/

    and can detect dots with colours other than black and image background is black
    but can not adjust colour filter to deal with blue background

    any help?

  14. #14
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    4,662

    Re: converting an image to rows and columns

    Without having to research the library's code and documentation myself, the easiest thing might be just to change the background color of the image you're dealing with to black. And, assuming that if you're changing the background to black, you'll want to change any existing black color to the background color so you don't loose those pixels, i.e. prevent them from merging with the black background.

    I don't want to take the time to test the function, but I would think something like this would work, (written here, not in IDE so could have syntax or logical issues).
    You would pass the two colors you want to swap, e.g. Colors.Black, Colors.Blue and the image, and save the return bitmap and then do your processing on the returned bitmap.
    You may be able to replace the Bitmap types in the function to Image types and it still work. As I said, I haven't tested the function, but it is based on something similar I've tested before.
    Code:
    Imports System.Drawing.Imaging
    
      Private Function SwapTwoImageColors( Color1 As Color, Color2 as Color, theImage As Bitmap) As Bitmap
          Dim colorMapping(1) As ColorMap
          Dim imageAtt As New ImageAttributes()
    
          colorMapping(0) = New ColorMap              'Map Color1 to Color2
          colorMapping(0).OldColor = Color1 
          colorMapping(0).NewColor = Color2
    
          colorMapping(1) = New ColorMap              'Map Color2 to Color1
          colorMapping(1).OldColor = Color2
          colorMapping(1).NewColor = Color1
    
          imageAtt.SetRemapTable(colorMapping)
    
          Dim swappedBitmap As Bitmap = theImage.Clone
          Using g As Graphics = Graphics.FromImage(swappedBitmap)
              g.DrawImage(theImage, 
                      New Rectangle(0,0,theImage.Width, theImage.Height),
                      0.0F, 0.0F,
                      theImage.Width, theImage.Height, GraphicsUnit.Pixel, imageAtt)
          End Using
          imageAtt.Dispose()
          Return swappedBitmap 
      End Function
    p.s. The function is assuming your pixel Alpha values are 255 and that the drawing will overdraw the existing pixels of the cloned image. If the Alpha values were not 255 then I assume you would get a mess and the two colors would be blended, created a third color (I would think, not tested).
    Last edited by passel; Feb 20th, 2018 at 05:56 AM.

  15. #15

    Thread Starter
    Member
    Join Date
    Feb 2016
    Posts
    47

    Re: converting an image to rows and columns

    ok thanks all

    now i found a solution
    1- split the image into small rectangles 20*20
    2- loop through these rectangles
    3- within each rectangle check each pixel color
    4- the spot must contain 3 adjucent black pixels or more
    5- encircle the spot

    this give a good resault but spots are slightly shifted up or daown
    so i need code to clearly detect spots
    Last edited by mrnooo2000; Feb 23rd, 2018 at 04:45 AM.

  16. #16

    Thread Starter
    Member
    Join Date
    Feb 2016
    Posts
    47

    Re: converting an image to rows and columns

    for who may need to find circles
    first use fastpix class to loop image pixels (fastpix will save your time) to find three points on the edge of the circle
    you can find class source and description here
    http://www.vbforums.com/showthread.p...light=lockbits


    then i used this code to find center and redius

    Code:
     'Find a circle through the three points.
        Private Sub FindCircle(ByVal a As PointF, ByVal b As PointF, ByVal c As PointF, ByRef center As PointF, ByRef radius As Single)
            ' Get the perpendicular bisector of (x1, y1) and (x2, y2).
            Dim x1 As Single = (b.X + a.X) / 2
            Dim y1 As Single = (b.Y + a.Y) / 2
            Dim dy1 As Single = b.X - a.X
            Dim dx1 As Single = -(b.Y - a.Y)
    
            ' Get the perpendicular bisector of (x2, y2) and (x3, y3).
            Dim x2 As Single = (c.X + b.X) / 2
            Dim y2 As Single = (c.Y + b.Y) / 2
            Dim dy2 As Single = c.X - b.X
            Dim dx2 As Single = -(c.Y - b.Y)
    
            ' See where the lines intersect.
            Dim lines_intersect, segments_intersect As Boolean
            Dim intersection, close1, close2 As PointF
            FindIntersection(New PointF(x1, y1), New PointF(x1 + dx1, y1 + dy1), New PointF(x2, y2), New PointF(x2 + dx2, y2 + dy2), lines_intersect, segments_intersect, intersection, close1, close2)
            If Not lines_intersect Then
                MessageBox.Show("The points are colinear")
                center = New PointF(0, 0)
                radius = 0
            Else
                center = intersection
                Dim dx As Single = center.X - a.X
                Dim dy As Single = center.Y - a.Y
                radius = CSng(Math.Sqrt(dx * dx + dy * dy))
            End If
        End Sub
        ' Find the point of intersection between the lines p1 --> p2 and p3 --> p4.
        Private Sub FindIntersection(ByVal p1 As PointF, ByVal p2 As PointF, ByVal p3 As PointF, ByVal p4 As PointF, ByRef lines_intersect As Boolean, ByRef segments_intersect As Boolean, ByRef intersection As PointF, ByRef close_p1 As PointF, ByRef close_p2 As PointF)
            ' Get the segments' parameters.
            Dim dx12 As Single = p2.X - p1.X
            Dim dy12 As Single = p2.Y - p1.Y
            Dim dx34 As Single = p4.X - p3.X
            Dim dy34 As Single = p4.Y - p3.Y
    
            ' Solve for t1 and t2
            Dim denominator As Single = (dy12 * dx34 - dx12 * dy34)
    
            Dim t1 As Single = ((p1.X - p3.X) * dy34 + (p3.Y - p1.Y) * dx34) / denominator
            If Single.IsInfinity(t1) Then
                ' The lines are parallel (or close enough to it).
                lines_intersect = False
                segments_intersect = False
                intersection = New PointF(Single.NaN, Single.NaN)
                close_p1 = New PointF(Single.NaN, Single.NaN)
                close_p2 = New PointF(Single.NaN, Single.NaN)
                Return
            End If
            lines_intersect = True
    
            Dim t2 As Single = ((p3.X - p1.X) * dy12 + (p1.Y - p3.Y) * dx12) / -denominator
    
            ' Find the point of intersection.
            intersection = New PointF(p1.X + dx12 * t1, p1.Y + dy12 * t1)
    
            ' The segments intersect if t1 and t2 are between 0 and 1.
            segments_intersect = ((t1 >= 0) AndAlso (t1 <= 1) AndAlso (t2 >= 0) AndAlso (t2 <= 1))
    
            ' Find the closest points on the segments.
            If t1 < 0 Then
                t1 = 0
            ElseIf t1 > 1 Then
                t1 = 1
            End If
    
            If t2 < 0 Then
                t2 = 0
            ElseIf t2 > 1 Then
                t2 = 1
            End If
    
            close_p1 = New PointF(p1.X + dx12 * t1, p1.Y + dy12 * t1)
            close_p2 = New PointF(p3.X + dx34 * t2, p3.Y + dy34 * t2)
        End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width