-
Feb 18th, 2018, 08:54 PM
#1
Thread Starter
Member
converting an image to rows and columns
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.
-
Feb 18th, 2018, 08:59 PM
#2
Re: converting an image to rows and columns
Originally Posted by mrnooo2000
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.
-
Feb 18th, 2018, 09:04 PM
#3
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
-
Feb 18th, 2018, 09:06 PM
#4
Thread Starter
Member
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
-
Feb 18th, 2018, 09:10 PM
#5
Re: converting an image to rows and columns
Originally Posted by mrnooo2000
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.
-
Feb 18th, 2018, 09:11 PM
#6
Thread Starter
Member
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.
-
Feb 18th, 2018, 09:17 PM
#7
Thread Starter
Member
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
-
Feb 18th, 2018, 09:33 PM
#8
Thread Starter
Member
Re: converting an image to rows and columns
i hope this pic clears what i want
-
Feb 18th, 2018, 10:11 PM
#9
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:
Private Sub ConvertAbsoluteCoordinatesToGridCoordinates(points As List(Of Point)) points.Sort(Function(p1, p2) p1.X.CompareTo(p2.X)) Dim lastX = -1 Dim columnIndex = 1 For i = 0 To points.Count - 1 Dim p = points(i) If p.X <> lastX Then lastX = p.X columnIndex += 1 End If p.X = columnIndex points(i) = p Next points.Sort(Function(p1, p2) p1.Y.CompareTo(p2.Y)) Dim lastY = -1 Dim rowIndex = 1 For i = 0 To points.Count - 1 Dim p = points(i) If p.Y <> lastY Then lastY = p.Y rowIndex += 1 End If p.Y = rowIndex points(i) = p Next End Sub
-
Feb 18th, 2018, 10:19 PM
#10
Thread Starter
Member
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
-
Feb 18th, 2018, 10:25 PM
#11
Re: converting an image to rows and columns
Originally Posted by mrnooo2000
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.
-
Feb 18th, 2018, 10:28 PM
#12
Thread Starter
Member
Re: converting an image to rows and columns
thanks a lot i'm traying to do that
-
Feb 20th, 2018, 12:37 AM
#13
Thread Starter
Member
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?
-
Feb 20th, 2018, 05:51 AM
#14
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.
-
Feb 23rd, 2018, 04:39 AM
#15
Thread Starter
Member
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.
-
Mar 5th, 2018, 02:45 PM
#16
Thread Starter
Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|