Locating balls on pool table using OpenCVSharp - c#

I'm trying to detect circles on an image, however I don't know what I'm doing wrong but the code is getting for me only 2 circles on the right as shown on the image below:
Here it is the code that I have tried:
using System;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Net;
using System.Windows.Forms.VisualStyles;
using OpenCvSharp;
using Point = System.Drawing.Point;
namespace ConsoleApplication1
{
internal class Program
{
public static void Main(string[] args)
{
Mat image = Cv2.ImRead(#"C:\Users\User\Desktop\pool.jpg", ImreadModes.Color);
Mat cImage = image.Clone();
Mat grayedColor = new Mat();
Cv2.CvtColor(cImage, grayedColor, ColorConversionCodes.BGR2GRAY);
var cirlce = Cv2.HoughCircles(grayedColor, HoughModes.Gradient, 1.2, 1);
foreach (var cirl in cirlce)
{
cImage.Circle(cirl.Center.ToPoint(), (int)cirl.Radius, Scalar.Magenta, 2, LineTypes.Link4);
}
Stopwatch timer = new Stopwatch();
timer.Start();
Cv2.ImShow("Original", cImage);
Cv2.ImShow("Grayed Color", grayedColor);
Console.WriteLine(timer.Elapsed.ToString(#"m\:ss\.fff"));
Cv2.WaitKey();
Cv2.DestroyAllWindows();
}
}
}
Please if you post a solution explain to me what I'm doing wrong exactly.

Related

Nethereum C# how can I get balance in address?

I try to get balance in address.
It is my code:
`using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Nethereum.Web3;
using System.Threading.Tasks;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
Bananas().Wait();
}
static private async Task Bananas()
{
var publicKey = "0xC0b4ec83028307053Fbe8d00ba4372384fe4b52B";
var web3 = new Nethereum.Web3.Web3("https://ropsten.infura.io/myInfura");
//var txCount = await web3.Eth.Transactions.GetTransactionCount.SendRequestAsync(publicKey);
var balance = await web3.Eth.GetBalance.SendRequestAsync(publicKey);
var etherAmount = Web3.Convert.FromWei(balance.Value);
Console.WriteLine(web3);
Console.WriteLine("Get txCount ", etherAmount);
Console.ReadLine();
}
}
}`
I installed Nethereum via PM console: Nethereum.
I use a normal link of infura.
Why don`t I get a next result without the balance in address?
I have just made a console application with your code and all the data is coming back fine from Nethereum.
Your issue is with your Console.WriteLine(... You are passing the etherAmount as a arg0 property into the Console.WriteLine which will not output correctly on the console when you run it.
Try this
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Nethereum.Web3;
using System.Threading.Tasks;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
Bananas().Wait();
}
static private async Task Bananas()
{
var publicKey = "0xC0b4ec83028307053Fbe8d00ba4372384fe4b52B";
var web3 = new Nethereum.Web3.Web3("https://ropsten.infura.io/myInfura");
//var txCount = await web3.Eth.Transactions.GetTransactionCount.SendRequestAsync(publicKey);
var balance = await web3.Eth.GetBalance.SendRequestAsync(publicKey);
var etherAmount = Web3.Convert.FromWei(balance.Value);
Console.WriteLine(web3);
Console.WriteLine("Get txCount " + etherAmount);
Console.ReadLine();
}
}
}
ps nice 1110.337873197299357846 ETH ;) (i know it is only test ETH but we can dream)

Trying to call R script from c# application

Error in code
cmd result
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using RDotNet;
using Microsoft.Win32;
using System.IO;
using System.Diagnostics;
namespace Con_R2
{
class Program
{
static string rPath = "";
static void Main(string[] args)
{
SetupPath(); // current process, soon to be deprecated
using (REngine engine = REngine.CreateInstance("RDotNet"))
{
engine.Initialize(); // required since v1.5
REngine.SetDllDirectory(rPath);
foreach (string path in engine.Evaluate(".libPaths()").AsCharacter())
{
Console.WriteLine(path);
}
engine.Evaluate(".libPaths(C:\\Program Files\\R\\R-3.3.1\\library)");
engine.Evaluate("source('c:/Program Files/R/R-3.3.1/bin/clustering_loadprofiles.r')");
Console.ReadLine();
Console.ReadKey();
}
}
public static void SetupPath(string Rversion = "R-3.3.1")
{
var oldPath = System.Environment.GetEnvironmentVariable("PATH");
rPath = System.Environment.Is64BitProcess ?
string.Format(#"C:\Program Files\R\{0}\bin\x64", Rversion) :
string.Format(#"C:\Program Files\R\{0}\bin\i386", Rversion);
if (!Directory.Exists(rPath))
throw new DirectoryNotFoundException(
string.Format(" R.dll not found in : {0}", rPath));
var newPath = string.Format("{0}{1}{2}", rPath,
System.IO.Path.PathSeparator, oldPath);
System.Environment.SetEnvironmentVariable("PATH", newPath);
}
}
}
Getting this error
Error in sqlFetch(channel, "sysadmin.loadprofile_allcustomers") :
first argument is not an open RODBC channel In addition: Warning
messages: 1: In RODBC::odbcDriverConnect("DSN=informix116") :
[RODBC] ERROR: state IM014, code 0, message [Microsoft][ODBC Driver
Manager] he specified DSN contains an architecture mismatch between
the Driver and Appl ation 2: In
RODBC::odbcDriverConnect("DSN=informix116") : ODBC connection failed

Setting first slide to display using PowerPoint-Api

I use NetOffice.PowerPointApi to play some Powerpoint-Slides of an existing PPTX. This is how this is done:
PowerPoint.Application powerApplication = new PowerPoint.Application();
PowerPoint.Presentation presentation = powerApplication.Presentations.Open("C:\\dev\\test.pptx", MsoTriState.msoTrue, MsoTriState.msoFalse, MsoTriState.msoTrue);
// presentation.SlideShowSettings.StartingSlide = 2;
presentation.SlideShowSettings.Run();
while (powerApplication.ActivePresentation.SlideShowWindow.View.CurrentShowPosition < 4)
{
System.Threading.Thread.Sleep(2000);
powerApplication.ActivePresentation.SlideShowWindow.View.Next();
}
Now my plan was to display slide 3 to 4.
But when I set the startingSlide (commented out in my example) I receive an error on powerApplication.ActivePresentation.SlideShowWindow.View.CurrentShowPosition :
{"SlideShowView.CurrentShowPosition : Invalid request. There is
currently no slide show view for this presentation."}
This only happens when I set the property StartingSlide. If I don't, the presentation runs from the first until the 4th slide.
You need to set more properties of the SlideShowSettings object:
using NetOffice.OfficeApi.Enums;
using NetOffice.PowerPointApi.Enums;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using PowerPoint = NetOffice.PowerPointApi;
namespace PlayPowerPoint
{
class Program
{
static void Main(string[] args)
{
using (var app = new PowerPoint.Application())
{
var presentation = app.Presentations.Open(Path.GetFullPath("Test.pptx"), MsoTriState.msoTrue, MsoTriState.msoFalse, MsoTriState.msoFalse);
var slideShowSettings = presentation.SlideShowSettings;
slideShowSettings.StartingSlide = 2;
slideShowSettings.EndingSlide = 4;
slideShowSettings.RangeType = PpSlideShowRangeType.ppShowSlideRange;
slideShowSettings.AdvanceMode = PpSlideShowAdvanceMode.ppSlideShowManualAdvance;
slideShowSettings.Run();
var slideShowView = presentation.SlideShowWindow.View;
while (slideShowView.CurrentShowPosition < slideShowSettings.EndingSlide)
{
Thread.Sleep(2000);
slideShowView.Next();
}
presentation.Saved = MsoTriState.msoTrue;
presentation.Close();
app.Quit();
}
}
}
}

Error Connecting To MongoDb from C# .NET Console Application

Unable to connect to server localhost:27017:
No connection could be made because the target machine actively refused it 127.0.0.1:27017.
This exception appears when i run a console application with C# using mongoDB
I've downloaded CSharpDriver-1.4.1.4490.msi
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MongoDB.Bson;
using MongoDB.Driver;
using MongoDB.Driver.Builders;
namespace ConsoleApplication4
{
public class Entity
{
public ObjectId Id { get; set; }
public string Name { get; set; }
}
class Program
{
static void Main(string[] args)
{
var connectionString = "mongodb://localhost:27017";
var server = MongoServer.Create(connectionString);
var database = server.GetDatabase("test");
var collection = database.GetCollection<Entity>("entities");
var entity = new Entity { Name = "Tom" };
collection.Insert(entity);
var id = entity.Id;
var query = Query.EQ("_id", id);
entity = collection.FindOne(query);
entity.Name = "Dick";
collection.Save(entity);
var update = Update.Set("Name", "Harry");
collection.Update(query, update);
collection.Remove(query);
}
}
I would follow the directions here, on the Mongo site. Windows quickstart is a really good resource to get started using Mongo on windows.
As far as connecting to the Mongo instance in .Net, if you didn't do anything special during the installation of Mongo, you shouldn't have to explicitly give a connection string. The following code works for my generic set up of Mongo.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MongoDB.Bson;
using MongoDB.Driver;
using MongoDB.Bson.IO;
using MongoDB.Bson.Serialization;
using MongoDB.Bson.Serialization.Attributes;
using MongoDB.Bson.Serialization.Conventions;
using MongoDB.Bson.Serialization.IdGenerators;
using MongoDB.Bson.Serialization.Options;
using MongoDB.Bson.Serialization.Serializers;
using MongoDB.Driver.Builders;
using MongoDB.Driver.GridFS;
using MongoDB.Driver.Wrappers;
namespace MongoDB
{
class Program
{
static void Main(string[] args)
{
MongoServer server;
MongoDatabase moviesDb;
server = MongoServer.Create();
moviesDb = server.GetDatabase("movies_db");
//Create some data
var movie1 = new Movie { Title = "Indiana Jones and the Raiders of the Lost Ark", Year = "1981" };
movie1.AddActor("Harrison Ford");
movie1.AddActor("Karen Allen");
movie1.AddActor("Paul Freeman");
var movie2 = new Movie { Title = "Star Wars: Episode IV - A New Hope", Year = "1977" };
movie2.AddActor("Mark Hamill");
movie2.AddActor("Harrison Ford");
movie2.AddActor("Carrie Fisher");
var movie3 = new Movie { Title = "Das Boot", Year = "1981" };
movie3.AddActor("Jürgen Prochnow");
movie3.AddActor("Herbert Grönemeyer");
movie3.AddActor("Klaus Wennemann");
//Insert the movies into the movies_collection
var moviesCollection = moviesDb.GetCollection<Movie>("movies_collection");
//moviesCollection.Insert(movie1);
//moviesCollection.Insert(movie2);
//moviesCollection.Insert(movie3);
var query = Query.EQ("Year","1981");
var movieFound = moviesDb.GetCollection<Movie>("movies_collection").Drop();
}
}
}

Compile code in a textbox and save to exe

Is it possible to compile any given C# code in textbox, and then save it to an exe?
Is this possible? If so, how can it be done?
Yes, it is possible. You could use CodeDOM. And here's an example:
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.CSharp;
using System.CodeDom.Compiler;
class Program
{
static void Main(string[] args)
{
var csc = new CSharpCodeProvider(new Dictionary<string, string>() { { "CompilerVersion", "v3.5" } });
var parameters = new CompilerParameters(new[] { "mscorlib.dll", "System.Core.dll" }, "foo.exe", true);
parameters.GenerateExecutable = true;
CompilerResults results = csc.CompileAssemblyFromSource(parameters,
#"using System.Linq;
class Program {
public static void Main(string[] args) {
var q = from i in Enumerable.Rnge(1,100)
where i % 2 == 0
select i;
}
}");
results.Errors.Cast<CompilerError>().ToList().ForEach(error => Console.WriteLine(error.ErrorText));
}
}
Apart from compiling the code at runtime, you can just save the code from your textbox to disk, and then use csc.exe to compile it. The command would look similar to the following:
%systemroot%\Microsoft.NET\Framework\v3.5\csc /out:filename.exe filename.cs

Categories