how can I convert circleF object to array object using emgu c# - c#

I am new at EmguCV & C# and I have a project that find circles' info (centers' X & Y coordinates) from USB Camera. According to these coordinates, prototype machine moves rollers with stepper motor x-axis or y-axis.
I used CircleF with HoughCircles and found coordinates with this code:
CircleF[] circles = imgProcessed.HoughCircles(new Gray(100), new Gray(50), 2, imgProcessed.Height / 4, 30, 45)[0];
I want to create 2d array from CircleF to apply array process and apply mathematical operations to circles' X and Y values.
I thought that if I convert "circles" CircleF to array, these processes are easier.
I know CircleF is a kind of array, but I cannot apply some array process (like sorting) on it. I want to sort X values of circles' centers from small to large. I cannot do that like Array.Sort(circles)
Does anybody help me about this situation?
or if it is possible to apply array process on CircleF, how can I apply?
Code samples would be useful. Thanks for now.

What exactly do you mean by "array process"? CircleF basically just "wraps" the center coordinate of the circle along with the radius and area. Take a look at the following which is taken from the Emgu.Cv.dll:
public struct CircleF : IEquatable<CircleF>
{
public CircleF(PointF center, float radius);
public double Area { get; }
public PointF Center { get; set; }
[XmlAttribute("Radius")]
public float Radius { get; set; }
public bool Equals(CircleF circle2);
}
So when iterating your array of CircleF structs you can:
for(int i=0;i < circles.Length;i++){
var currCircleF = circles[i];
//do something with currCircleF, e.g. change radius,center,etc.
}

To sort the array of circles according to the value of center X:
var sortedCircleFbyX = circles.OrderBy(c => c.Center.X).ToArray();
The result is an array of CircleF. You can then perform processing like:
//do more processing
foreach (var circleF in sortedCircleFbyX)
{
MoveStepperMotor(circleF.Center);
}

Related

Finding an item in linear graph using LINQ

I have a list of positions denoted by X and Y. [{3,4}, {5,5}, {6,5},{7,8}]
public class Position {
public int X { get; set; }
public int Y { get; set; }
}
I need to find positions which will be present in the linear graph Starting
x=1 and y=0. [{1,0},{2,1},{3,2},{4,3}, ...].
I do not have the list which denotes the graph. I am looking for a way to find the positions based on starting point of the graph.
I can create the list of possibilities and find the matching positions. Before I do that I want to know is there a better approach?
The points are on a straight line. The formula for the points you mention is: y = x - 1. You can apply this formula in a where clause:
var x = new List<Position>();
...
var pointsOnLine = x.Where(p => p.Y == p.X -1);
You can do this also if you have another line or formula.

Finding nearest coordinate to other coordinate

Given an array of geo coordinates and another geo coordinate, I would like to find the nearest coordinate(s) to it.
For example, given the array:
lat long
52.525782 13.316927
52.526409 13.319083
52.525678 13.320317
And the point: 52.525730, 13.314556, then the first point 52.525782, 13.316927 will be returned, as it is the closest one.
Is the only way of acheiving it is looping through all the array and finding the distance between the points? What happens if the array contains too much coordinates?
You can try it using LINQ, but the inner workings of LINQ would still loop over your collection. For example:
//Your list with coordinates
List<GeoCoordinate> coords = new List<GeoCoordinate>();
//The coord you want to compare
GeoCoordinate crd = new GeoCoordinate();
//Sorts the list based on the proximity of the coords to crd
var sortedCoords = coords.OrderBy(x => x.GetDistanceTo(crd)).ToList();
I know it doesn't use an array, but I find using lists is easier.
I think that should work, let me know if it does!
struct coord
{
public double lat;
public double lon;
}
public void Main(coord coord)
{
var coords = new[]{ new coord(){lat=1, lon=1} };
var closest = coords.Min(p => Math.Abs(p.lat - coord.lat) + Math.Abs(p.lon - coord.lon));
}

XNA C# 2D Tile Engine

I have decided to have a go at making a dungeon crawler game with the Xna framework. I am a computer science student and am quite familiar with c# and .net framework. I have some questions about different parts of the development for my engine.
Loading Maps
I have a tile class that stores the vector2 position, 2dtexture and dimensions of the tile. I have another class called tilemap that has a list of tiles that are indexed by position. I am reading from a text file which is in the number format above that matches the number to the index in the tile list and creates a new tile with the correct texture and position, storing it into another list of tiles.
public List<Tile> tiles = new List<Tile>(); // List of tiles that I have added to the game
public List<TileRow> testTiles = new List<TileRow>(); // Tilerow contains a list of tiles along the x axis along with there vector2 position.
Reading and storing the map tiles.
using (StreamReader stream = new StreamReader("TextFile1.txt"))
{
while (stream.EndOfStream != true)
{
line = stream.ReadLine().Trim(' ');
lineArray = line.Split(' ');
TileRow tileRow = new TileRow();
for (int x = 0; x < lineArray.Length; x++)
{
tileXCo = x * tiles[int.Parse(lineArray[x])].width;
tileYCo = yCo * tiles[int.Parse(lineArray[x])].height;
tileRow.tileList.Add(new Tile(tiles[int.Parse(lineArray[x])].titleTexture, new Vector2(tileXCo,tileYCo)));
}
testTiles.Add(tileRow);
yCo++;
}
}
For drawing the map.
public void Draw(SpriteBatch spriteBatch, GameTime gameTime)
{
foreach (TileRow tes in testTiles)
{
foreach (Tile t in tes.tileList)
{
spriteBatch.Draw(t.titleTexture, t.position, Color.White);
}
}
}
Questions:
Is this the correct way I should be doing it, or should I just be storing a list referencing my tiles list?
How would I deal with Multi Layered Maps?
Collision Detection
At the moment I have a method that is looping through every tile that is stored in my testTiles list and checking to see if its dimensions are intersecting with the players dimensions and then return a list of all the tiles that are. I have a derived class of my tile class called CollisionTile that triggers a collision when the player and that rectangle intersect. (public class CollisionTile : Tile)
public List<Tile> playerArrayPosition(TileMap tileMap)
{
List<Tile> list = new List<Tile>();
foreach (TileRow test in tileMap.testTiles)
{
foreach (Tile t in test.tileList)
{
Rectangle rectangle = new Rectangle((int)tempPosition.X, (int)tempPosition.Y, (int)playerImage.Width / 4, (int)playerImage.Height / 4);
Rectangle rectangle2 = new Rectangle((int)t.position.X, (int)t.position.Y, t.width, t.height);
if (rectangle.Intersects(rectangle2))
{
list.Add(t);
}
}
}
return list;
}
Yeah, I am pretty sure this is not the right way to check for tile collision. Any help would be great.
Sorry for the long post, any help would be much appreciated.
You are right. This is a very inefficient way to draw and check for collision on your tiles. What you should be looking into is a Quadtree data structure.
A quadtree will store your tiles in a manner that will allow you to query your world using a Rectangle, and your quadtree will return all tiles that are contained inside of that Rectangle.
List<Tiles> tiles = Quadtree.GetObjects(rectangle);
This allows you to select only the tiles that need to be processed. For example, when drawing your tiles, you could specify a Rectangle the size of your viewport, and only those tiles would be drawn (culling).
Another example, is you can query the world with your player's Rectangle and only check for collisions on the tiles that are returned for that portion of your world.
For loading your tiles, you may want to consider loading into a two dimensional array, instead of a List. This would allow you to fetch a tile based on its position, instead of cross referencing it between two lists.
Tile[,] tiles = new Tile[,]
Tile tile = tiles[x,y];
Also, in this case, an array data structure would be a lot more efficient than using a List.
For uniform sets of tiles with standard widths and heights, it is quite easy to calculate which tiles are visible on the screen, and to determine which tile(s) your character is overlapping with. Even though I wrote the QuadTree in Jon's answer, I think it's overkill for this. Generally, the formula is:
tileX = someXCoordinate % tileWidth;
tileY = someYCoordinate % tileHeight;
Then you can just look that up in a 2D array tiles[tileX, tileY]. For drawing, this can be used to figure out which tile is in the upper left corner of the screen, then either do the same again for the bottom right (+1), or add tiles to the upper left to fill the screen. Then your loop will look more like:
leftmostTile = screenX % tileWidth; // screenX is the left edge of the screen in world coords
topmostTile = screenY % tileHeight;
rightmostTile = (screenX + screenWidth) % tileWidth;
bottommostTile = (screenY + screenHeight) % tileHeight;
for(int tileX = leftmostTile; tileX <= rightmostTile; tileX++)
{
for(int tileY = topmostTile; tileY <= bottommostTile; tileY++)
{
Tile t = tiles[tileX][tileY];
// ... more stuff
}
}
The same simple formula can be used to quickly figure out which tile(s) are under rectangular areas.
IF however, your tiles are non-uniform, or you have an isometric view, or you want the additional functionality that a QuadTree provides, I would consider Jon's answer and make use of a QuadTree. I would try to keep tiles out of the QuadTree if you can though.

Is there an algorithm to compute miles between coordinates?

I want to be able to display a Bing map in a Windows 8/Store app with an array of pushpins/waypoints at a zoom setting that will show every location, but no more than that - IOW, I want as much detail as possible while still showing all of the locations/coordinates.
I have this pseudocode:
public static int GetMapZoomSettingForCoordinates(List<String> coordinatesList)
{
string furthestNorth = GetFurthestNorth(coordinatesList);
string furthestSouth = GetFurthestSouth(coordinatesList);
string furthestEast = GetFurthestEast(coordinatesList);
string furthestWest = GetFurthestWest(coordinatesList);
int milesBetweenNorthAndSouthExtremes = GetMilesBetween(furthestNorth, furthestSouth);
int milesBetweenEastAndWestExtremes = GetMilesBetween(furthestEast, furthestWest);
int greaterCardinalDistance = Math.Max(milesBetweenNorthAndSouthExtremes, milesBetweenEastAndWestExtremes);
return GetZoomSettingForDistance(greaterCardinalDistance);
}
...but the "sticking point" (the hard part) are the "milesBetween" functions. Is there an existing algorithm for computing the miles between two coordinates?
I do realize this is a U.S.-centric bunch of code for now (miles vs. kilometers); that is, for now, as designed.
UPDATE
This is my new pseudocode (actual compiling code, but untested):
public static int GetMapZoomSettingForCoordinates(List<string> coordinatePairsList)
{
List<double> LatsList = new List<double>();
List<double> LongsList = new List<double>();
List<string> tempList = new List<string>();
foreach (string s in coordinatePairsList)
{
tempList.AddRange(s.Split(';'));
double dLat;
double.TryParse(tempList[0], out dLat);
double dLong;
double.TryParse(tempList[0], out dLong);
LatsList.Add(dLat);
LongsList.Add(dLong);
tempList.Clear();
}
double furthestNorth = GetFurthestNorth(LatsList);
double furthestSouth = GetFurthestSouth(LatsList);
double furthestEast = GetFurthestEast(LongsList);
double furthestWest = GetFurthestWest(LongsList);
int milesToDisplay =
HaversineInMiles(furthestWest, furthestNorth, furthestEast, furthestSouth);
return GetZoomSettingForDistance(milesToDisplay);
}
private static double GetFurthestNorth(List<double> longitudesList)
{
double northernmostVal = 0.0;
foreach (double d in longitudesList)
{
if (d > northernmostVal)
{
northernmostVal = d;
}
}
return northernmostVal;
}
...I still don't know what GetZoomSettingForDistance() should be/do, though...
UPDATE 2
This is "more better":
public static int GetMapZoomSettingForCoordinates(List<Tuple<double, double>> coordinatePairsList)
{
var LatsList = new List<double>();
var LongsList = new List<double>();
foreach (Tuple<double,double> tupDub in coordinatePairsList)
{
LatsList.Add(tupDub.Item1);
LongsList.Add(tupDub.Item2);
}
double furthestNorth = GetFurthestNorth(LongsList);
double furthestSouth = GetFurthestSouth(LongsList);
double furthestEast = GetFurthestEast(LatsList);
double furthestWest = GetFurthestWest(LatsList);
int milesToDisplay =
HaversineInMiles(furthestWest, furthestNorth, furthestEast, furthestSouth);
return GetZoomSettingForDistance(milesToDisplay);
}
UPDATE 3
I realized that my logic was backwards, or wrong, at any rate, regarding meridians of longitude and parallels of latitude. While it's true that meridians of longitude are the vertical lines ("drawn" North-to-South or vice versa) and that parallels of latitude are the horizontal lines ("drawn" East-to-West), points along those line represent the North-South location based on parallels of latitude, and represent East-West locations based on meridians of longitude. This seemed backwards in my mind until I visualized the lines spinning across (longitude) and up and over (latitude) the earth, rather than simply circling the earth like the rings of Saturn do; what also helped get my perception right was reminding myself that it is the values of the meridians of longitude that determine in which time zone one finds themselves. SO, the code above should change to pass latitudes to determine furthest North and furthest South, and conversely pass longitudes to determine furthest East and furthest West.
You can use the Haversine formula to compute the distance along the surface of a sphere.
Here's a C++ function to compute the distance using the Earth as the size of the sphere. It would easily be convertible to C#.
Note that the formula can be simplified if you want to just find the distance either latitudinally or longitudinally (which it sounds like you are trying to do).
To get the straight line distance you use the Pythagorean Theorem to find the hypotenuse.
d = ((delta x)^2 + (delta y)^2)^.5
Basically square both the changes in the x direction and the y direction, add them, then take the square root.
in your pseudo code it looks like you could have many points and you want to find a maximum distance that should encompass all of them, which makes sense if you are trying to figure out a scale for the zoom of the map. The same formula should work, just use milesBetweenEastAndWestExtremes for delta x, and milesBetweenNorthAndSouthExtremes for delta y. You may opt to add a fixed amount to this just to make sure you don't have points right on the very edge of the map.

Math : How to convert a 3D World to 2D Screen coordinate

I'm looking for a way to convert 3D xyz coordinates to 2D xy (pixel) coordinates. I'm getting a list of coordinates which I need to plot on a 2d plane.
The plane will always be a top-down view width the following dimensions width:800 px, height 400px
The 3D world coordinates can contain negative values aswell ranging from -4000 to 4000. I have read a few conversion articles on Wikipedia and a couple of SO threads but they either didn't fit my needs or they were too complex for my limited math knowledge.
I hope someone can help me.
Thank you for your time.
Regards,
Mark
you can use something like [(x/z),(y/z)] to project 3d to 2d - I believe this is a fairly crude method and I would think that 3d to 2d Googlings would return some fairly standard algorithms
Rob is more or less correct, just that normally a scaling factor needs to be used (i.e [k*(x/z), k*(y/z)]). If you never change your point or direction of view, all the math you need to fully understand why this works are the intercept theorems.
I think the standard implementation of this uses so-called homogenous coordinates, which is a bit more complicated. But for a quick-and-dirty implementation just using 'normal' 3D coordinates works fine.
You also need to be a bit careful when dealing with coordinates that are behind your point of view. In fact, this is what I have found the most ugly part of (polygon-based) 3D graphics.
You may find this interesting: A 3D Plotting Library in C#.
Something that may help: Some code i'm working on...
// Location in 3D space (x,y,z), w = 1 used for affine matrix transformations...
public class Location3d : Vertex4d
{
// Default constructor
public Location3d()
{
this.x = 0;
this.y = 0;
this.z = 0;
this.w = 1; // w = 1 used for affine matrix transformations...
}
// Initiated constructor(dx,dy,dz)
public Location3d(double dx, double dy, double dz)
{
this.x = dx;
this.y = dy;
this.z = dz;
this.w = 1; // w = 1 used for affine matrix transformations...
}
}
// Point in 2d space(x,y) , screen coordinate system?
public class Point2d
{
public int x { get; set; } // 2D space x,y
public int y { get; set; }
// Default constructor
public point2d()
{
this.x = 0;
this.y = 0;
}
}
// Check if a normal vertex4d of a plane is pointing away?
// z = looking toward the screen +1 to -1
public bool Checkvisible(Vertex4d v)
{
if(v.z <= 0)
{
return false; // pointing away, thus invisible
}
else
{
return true;
}
}
// Check if a vertex4d is behind you, out of view(behinde de camera?)
// z = looking toward the screen +1 to -1
public bool CheckIsInFront(Vertex4d v)
{
if(v.z < 0)
{
return false; // some distans from the camera
}
else
{
return true;
}
}
Some clipping to be done if the vertecies are outside the screen area !!!

Categories