Skip to content

Commit

Permalink
Implement grid detection
Browse files Browse the repository at this point in the history
  • Loading branch information
Blightbuster committed Mar 5, 2021
1 parent df8dfd5 commit 319fb08
Show file tree
Hide file tree
Showing 9 changed files with 154 additions and 155 deletions.
141 changes: 32 additions & 109 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -1,120 +1,43 @@
# Rules in this file were initially inferred by Visual Studio IntelliCode from the D:\Programmierung\Projects\RatScanner\RatEye codebase based on best match to current usage at 23.12.2020
# You can modify the rules from these initially generated values to suit your own policies
# You can learn more about editorconfig here: https://docs.microsoft.com/en-us/visualstudio/ide/editorconfig-code-style-settings-reference
[*.cs]


#Core editorconfig formatting - indentation

#use hard tabs for indentation
[*.{appxmanifest,asax,ascx,aspx,axaml,axml,build,c,c++,cc,cginc,compute,config,cp,cpp,cs,cshtml,csproj,css,cu,cuh,cxx,dbml,discomap,dtd,h,hh,hlsl,hlsli,hlslinc,hpp,htm,html,hxx,inc,inl,ino,ipp,js,json,jsproj,jsx,lsproj,master,mpp,mq4,mq5,mqh,njsproj,nuspec,paml,proj,props,proto,razor,resjson,resw,resx,skin,StyleCop,targets,tasks,tpp,ts,tsx,usf,ush,vb,vbproj,xaml,xamlx,xml,xoml,xsd}]
indent_style = tab
indent_size = tab
tab_width = 4

#Formatting - new line options

#place catch statements on a new line
csharp_new_line_before_catch = true
#place else statements on a new line
csharp_new_line_before_else = true
#require braces to be on a new line for types, lambdas, properties, methods, and control_blocks (also known as "Allman" style)
csharp_new_line_before_open_brace = types, lambdas, properties, methods, control_blocks

#Formatting - organize using options

#sort System.* using directives alphabetically, and place them before other usings
dotnet_sort_system_directives_first = true

#Formatting - spacing options

#require NO space between a cast and the value
csharp_space_after_cast = false
#require a space after a keyword in a control flow statement such as a for loop
csharp_space_after_keywords_in_control_flow_statements = true
#remove space within empty argument list parentheses
csharp_space_between_method_call_empty_parameter_list_parentheses = false
#remove space between method call name and opening parenthesis
csharp_space_between_method_call_name_and_opening_parenthesis = false
#do not place space characters after the opening parenthesis and before the closing parenthesis of a method call
csharp_space_between_method_call_parameter_list_parentheses = false
#remove space within empty parameter list parentheses for a method declaration
csharp_space_between_method_declaration_empty_parameter_list_parentheses = false
#place a space character after the opening parenthesis and before the closing parenthesis of a method declaration parameter list.
csharp_space_between_method_declaration_parameter_list_parentheses = false

#Formatting - wrapping options

#leave code block on separate lines
csharp_preserve_single_line_blocks = false
#leave statements and member declarations on the same line
csharp_preserve_single_line_statements = true

#Style - Code block preferences

#prefer no curly braces if allowed
csharp_prefer_braces = false:suggestion

#Style - expression bodied member options

#prefer expression-bodied members for accessors
csharp_style_expression_bodied_accessors = true:suggestion
#prefer block bodies for constructors
csharp_style_expression_bodied_constructors = false:suggestion
#prefer block bodies for methods
csharp_style_expression_bodied_methods = false:suggestion
#prefer block bodies for operators
csharp_style_expression_bodied_operators = false:suggestion
#prefer expression-bodied members for properties
csharp_style_expression_bodied_properties = true:suggestion

#Style - expression level options
[*]

#prefer out variables to be declared inline in the argument list of a method call when possible
csharp_style_inlined_variable_declaration = true:suggestion
#prefer tuple names to ItemX properties
dotnet_style_explicit_tuple_names = true:suggestion
#prefer the language keyword for member access expressions, instead of the type name, for types that have a keyword to represent them
dotnet_style_predefined_type_for_member_access = true:suggestion

#Style - Expression-level preferences

#prefer objects to not be initialized using object initializers
dotnet_style_object_initializer = false:suggestion
#prefer inferred tuple element names
dotnet_style_prefer_inferred_tuple_names = true:suggestion

#Style - implicit and explicit types
# Standard properties
end_of_line = crlf
insert_final_newline = true

#prefer var over explicit type in all cases, unless overridden by another code style rule
# Microsoft .NET properties
csharp_new_line_before_members_in_object_initializers = false
csharp_preferred_modifier_order = public, private, protected, internal, new, abstract, virtual, sealed, override, static, readonly, extern, unsafe, volatile, async:suggestion
csharp_style_var_elsewhere = true:suggestion
#prefer var is used to declare variables with built-in system types such as int
csharp_style_var_for_built_in_types = true:suggestion
#prefer var when the type is already mentioned on the right-hand side of a declaration expression
csharp_style_var_when_type_is_apparent = true:suggestion

#Style - language keyword and framework type options

#prefer the language keyword for local variables, method parameters, and class members, instead of the type name, for types that have a keyword to represent them
dotnet_style_parentheses_in_arithmetic_binary_operators = never_if_unnecessary:none
dotnet_style_parentheses_in_other_binary_operators = never_if_unnecessary:none
dotnet_style_parentheses_in_relational_binary_operators = never_if_unnecessary:none
dotnet_style_predefined_type_for_locals_parameters_members = true:suggestion

#Style - modifier options

#prefer accessibility modifiers to be declared except for public interface members. This will currently not differ from always and will act as future proofing for if C# adds default interface methods.
dotnet_style_predefined_type_for_member_access = true:suggestion
dotnet_style_qualification_for_event = false:warning
dotnet_style_qualification_for_field = false:warning
dotnet_style_qualification_for_method = false:warning
dotnet_style_qualification_for_property = false:warning
dotnet_style_require_accessibility_modifiers = for_non_interface_members:suggestion

#Style - Modifier preferences

#when this rule is set to a list of modifiers, prefer the specified ordering.
csharp_preferred_modifier_order = public,private,internal,static,readonly,override,async:suggestion

#Style - Pattern matching

#prefer pattern matching instead of is expression with type casts
csharp_style_pattern_matching_over_as_with_null_check = true:suggestion

#Style - qualification options

#prefer fields not to be prefaced with this. or Me. in Visual Basic
dotnet_style_qualification_for_field = false:suggestion
#prefer methods not to be prefaced with this. or Me. in Visual Basic
dotnet_style_qualification_for_method = false:suggestion
#prefer properties not to be prefaced with this. or Me. in Visual Basic
dotnet_style_qualification_for_property = false:suggestion
# ReSharper properties
resharper_enforce_line_ending_style = true
resharper_keep_existing_property_patterns_arrangement = false
resharper_use_indent_from_vs = false

# ReSharper inspection severities
resharper_arrange_redundant_parentheses_highlighting = hint
resharper_arrange_type_member_modifiers_highlighting = hint
resharper_arrange_type_modifiers_highlighting = hint
resharper_built_in_type_reference_style_for_member_access_highlighting = hint
resharper_built_in_type_reference_style_highlighting = hint
resharper_suggest_var_or_type_built_in_types_highlighting = hint
resharper_suggest_var_or_type_elsewhere_highlighting = hint
resharper_suggest_var_or_type_simple_types_highlighting = hint
5 changes: 5 additions & 0 deletions RatEye.sln
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "RatEye", "RatEye\RatEye.csp
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "RatEyeTest", "RatEyeTest\RatEyeTest.csproj", "{16CFB1DB-DF29-4E1A-A99E-1D806FDAA7B8}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{EC78094C-A450-4F03-A326-B7AE4C16879D}"
ProjectSection(SolutionItems) = preProject
.editorconfig = .editorconfig
EndProjectSection
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down
5 changes: 5 additions & 0 deletions RatEye/Logger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ internal static void LogDebugMat(OpenCvSharp.Mat mat, string fileName = "mat")
}
}

internal static void ShowMat(OpenCvSharp.Mat mat, string name = "")
{
new OpenCvSharp.Window(name, mat);
}

internal static void LogInfo(string message)
{
AppendToLog("[Info] " + message);
Expand Down
6 changes: 6 additions & 0 deletions RatEye/Processing/Inspection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@ public enum InspectionType
{
/// <summary>Unknown inspection type</summary>
Unknown,

/// <summary>When double clicking a item or clicking the inspect tooltip</summary>
Item,

/// <summary>When opening a container like a Keytool or a Scavbox</summary>
Container,
}
Expand Down Expand Up @@ -102,6 +104,7 @@ public Inspection(Bitmap image)
}

#region Processing state handling

private enum State
{
Default,
Expand Down Expand Up @@ -138,13 +141,16 @@ private void SatisfyState(State targetState)
}
}
}

#endregion

/// <summary>
/// Search for all different marker types and pick the best matching one
/// </summary>
private void SearchMarker()
{
SatisfyState(State.Default);

var item = GetMarkerPosition(InspectionConfig.GetScaledMarker(InspectionType.Item));
var container = GetMarkerPosition(InspectionConfig.GetScaledMarker(InspectionType.Container));

Expand Down
128 changes: 84 additions & 44 deletions RatEye/Processing/Inventory.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Drawing;
using OpenCvSharp;
using OpenCvSharp.Extensions;

Expand All @@ -8,28 +7,29 @@ namespace RatEye.Processing
public class Inventory
{
private Mat _image;
private Mat _scaledImage;
private Mat _grid;
private int _slotSize;
private float _scale;

private enum Direction
{
North,
East,
South,
West,
}

/// <summary>
/// Constructor for inventory view processing object
/// </summary>
/// <param name="image">Image of the inventory which will be processed</param>
/// <param name="scale">Scale of the image. 1 when the image is from a FullHD screen. 2 when 4k.</param>
/// <param name="slotSize">Pixel size of a single slot in pixel</param>
public Inventory(Bitmap image, float scale = 1, int slotSize = 63)
public Inventory(System.Drawing.Bitmap image)
{
_image = image.ToMat();
_slotSize = slotSize;
_scale = scale;
}

private enum State
{
Default,
Rescaled,
//Rescaled,
GridDetected,
}

Expand All @@ -45,9 +45,9 @@ private void SatisfyState(State targetState)
{
case State.Default:
break;
case State.Rescaled:
RescaleInventory();
break;
//case State.Rescaled:
// RescaleInventory();
// break;
case State.GridDetected:
DetectInventoryGrid();
break;
Expand All @@ -63,53 +63,93 @@ private void SatisfyState(State targetState)
}
}

private void RescaleInventory()
{
_scaledImage = _image.ToBitmap().Rescale(_scale).ToMat();
}
//private void RescaleInventory()
//{
// _scaledImage = _image.ToBitmap().Rescale(Config.Processing.Scale).ToMat();
//}

private void DetectInventoryGrid()
{
SatisfyState(State.Rescaled);
SatisfyState(State.Default);

var colorFilter = _image.InRange(new Scalar(84, 81, 73), new Scalar(108, 117, 112));

// TODO implement
var scaledSlotSize = Config.Processing.ScaledSlotSize;

var lineStructure = Mat.Ones(MatType.CV_8U, new[] { (int)(scaledSlotSize), 1 });

var verticalLines = colorFilter.Erode(lineStructure);
verticalLines = verticalLines.Dilate(lineStructure);
var horizontalLines = colorFilter.Erode(lineStructure.T());
horizontalLines = horizontalLines.Dilate(lineStructure.T());

var filteredLines = new Mat();
Cv2.BitwiseOr(horizontalLines, verticalLines, filteredLines);
_grid = filteredLines;
}

/// <summary>
/// Finds the position and size of the icon at a given position
/// Finds the origin and size of the icon at a given origin
/// </summary>
/// <param name="origin">Position at which to locate the icon. null = center</param>
/// <returns>A tuple containing position and size in pixel</returns>
/// <param name="origin">Position at which to locate the icon. <see langword="null"/> = center</param>
/// <returns>A tuple containing the position and size in pixel</returns>
/// <remarks><see cref="origin"/> of (0, 0) means top left corner of the image</remarks>
public (Vector2 position, Vector2 size) LocateIcon(Vector2 origin = null)
{
SatisfyState(State.GridDetected);

if (origin == null) origin = new Vector2(_image.Size()) / 2;
if (origin == null) origin = new Vector2(_grid.Size()) / 2;

// TODO re-implement

// --- Below is non-working leftover ---
var mat = _image.Clone();
Logger.LogDebugMat(mat, "capture");
_grid = mat.InRange(new Scalar(84, 81, 73), new Scalar(104, 101, 93));
Logger.LogDebugMat(_grid, "in_range");
var mask = mat.Canny(100, 100);
Logger.LogDebugMat(mask, "canny_edge");
mask = mask.Dilate(Mat.Ones(10, 3, MatType.CV_8UC1));
Logger.LogDebugMat(mask, "canny_edge_dilate");
Cv2.BitwiseAnd(_grid, mask, _grid);
Logger.LogDebugMat(_grid, "in_range_and_canny_edge_dilate");

var rightBorderDist = 30;
var leftBorderDist = 30;
var topBorderDist = 30;
var bottomBorderDist = 30;
var topBorderDist = FindGridEdgeDistance(origin, Direction.North);
var rightBorderDist = FindGridEdgeDistance(origin, Direction.East);
var bottomBorderDist = FindGridEdgeDistance(origin, Direction.South);
var leftBorderDist = FindGridEdgeDistance(origin, Direction.West);

var position = origin - new Vector2(leftBorderDist, topBorderDist);
var size = new Vector2(rightBorderDist + leftBorderDist, topBorderDist + bottomBorderDist);
//LocatedIcon = (position, size);
//return LocatedIcon;
return (null, null);
return (position, size);
}

/// <summary>
/// Find the distance to the first white pixel in a give direction
/// </summary>
/// <param name="origin">Position at which to locate the icon.</param>
/// <param name="direction"></param>
/// <returns></returns>
private int FindGridEdgeDistance(Vector2 origin, Direction direction)
{
var maxSteps = direction switch {
Direction.North => origin.Y,
Direction.East => _grid.Width - origin.X,
Direction.South => _grid.Height - origin.Y,
Direction.West => origin.X,
};

var (xDiff, yDiff) = direction switch {
Direction.North => (0, -1),
Direction.East => (1, 0),
Direction.South => (0, 1),
Direction.West => (-1, 0),
};

var x = origin.X;
var y = origin.Y;

var indexer = _grid.GetGenericIndexer<byte>();
for (var i = 0; i < maxSteps; i++)
{
if (indexer[y, x].Equals(0xFF))
{
Logger.LogDebug($"Edge at: ({x}, {y}) | Distance from origin: {i}");
return i;
}

x += xDiff;
y += yDiff;
}

Logger.LogDebug($"No edge found! Origin: ({x}, {y}) | Direction: {direction}");
return 0;
}
}
}
Loading

0 comments on commit 319fb08

Please sign in to comment.