839 lines
35 KiB
HTML
839 lines
35 KiB
HTML
|
|
<!DOCTYPE html>
|
|
|
|
<html class="no-js" lang="en">
|
|
<head>
|
|
<meta charset="utf-8"/>
|
|
<meta content="width=device-width,initial-scale=1" name="viewport"/>
|
|
<meta content="Rohmer Maxime" name="author"/>
|
|
<link href="../assets/images/favicon.png" rel="icon"/>
|
|
<meta content="mkdocs-1.4.3, mkdocs-material-8.5.0" name="generator"/>
|
|
<title>OcrImage.cs - Documentation Track Trends</title>
|
|
<link href="../assets/stylesheets/main.2e8b5541.min.css" rel="stylesheet"/>
|
|
<link href="../assets/stylesheets/palette.cbb835fc.min.css" rel="stylesheet"/>
|
|
<link crossorigin="" href="https://fonts.gstatic.com" rel="preconnect"/>
|
|
<link href="https://fonts.googleapis.com/css?family=Roboto:300,300i,400,400i,700,700i%7CRoboto+Mono:400,400i,700,700i&display=fallback" rel="stylesheet"/>
|
|
<style>:root{--md-text-font:"Roboto";--md-code-font:"Roboto Mono"}</style>
|
|
<script>__md_scope=new URL("..",location),__md_hash=e=>[...e].reduce((e,_)=>(e<<5)-e+_.charCodeAt(0),0),__md_get=(e,_=localStorage,t=__md_scope)=>JSON.parse(_.getItem(t.pathname+"."+e)),__md_set=(e,_,t=localStorage,a=__md_scope)=>{try{t.setItem(a.pathname+"."+e,JSON.stringify(_))}catch(e){}}</script>
|
|
<link href="../assets/stylesheets/glightbox.min.css" rel="stylesheet"/><style>html.glightbox-open { overflow: initial; height: 100%; }</style><script src="../assets/javascripts/glightbox.min.js"></script></head>
|
|
<body data-md-color-accent="" data-md-color-primary="" data-md-color-scheme="default" dir="ltr">
|
|
<script>var palette=__md_get("__palette");if(palette&&"object"==typeof palette.color)for(var key of Object.keys(palette.color))document.body.setAttribute("data-md-color-"+key,palette.color[key])</script>
|
|
<input autocomplete="off" class="md-toggle" data-md-toggle="drawer" id="__drawer" type="checkbox"/>
|
|
<input autocomplete="off" class="md-toggle" data-md-toggle="search" id="__search" type="checkbox"/>
|
|
<label class="md-overlay" for="__drawer"></label>
|
|
<div data-md-component="skip">
|
|
<a class="md-skip" href="#ocrimagecs">
|
|
Skip to content
|
|
</a>
|
|
</div>
|
|
<div data-md-component="announce">
|
|
</div>
|
|
<header class="md-header" data-md-component="header">
|
|
<nav aria-label="Header" class="md-header__inner md-grid">
|
|
<a aria-label="Documentation Track Trends" class="md-header__button md-logo" data-md-component="logo" href="../index.html" title="Documentation Track Trends">
|
|
<svg viewbox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path d="M12 8a3 3 0 0 0 3-3 3 3 0 0 0-3-3 3 3 0 0 0-3 3 3 3 0 0 0 3 3m0 3.54C9.64 9.35 6.5 8 3 8v11c3.5 0 6.64 1.35 9 3.54 2.36-2.19 5.5-3.54 9-3.54V8c-3.5 0-6.64 1.35-9 3.54Z"></path></svg>
|
|
</a>
|
|
<label class="md-header__button md-icon" for="__drawer">
|
|
<svg viewbox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path d="M3 6h18v2H3V6m0 5h18v2H3v-2m0 5h18v2H3v-2Z"></path></svg>
|
|
</label>
|
|
<div class="md-header__title" data-md-component="header-title">
|
|
<div class="md-header__ellipsis">
|
|
<div class="md-header__topic">
|
|
<span class="md-ellipsis">
|
|
Documentation Track Trends
|
|
</span>
|
|
</div>
|
|
<div class="md-header__topic" data-md-component="header-topic">
|
|
<span class="md-ellipsis">
|
|
|
|
OcrImage.cs
|
|
|
|
</span>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<form class="md-header__option" data-md-component="palette">
|
|
<input aria-hidden="true" class="md-option" data-md-color-accent="" data-md-color-media="(prefers-color-scheme: light)" data-md-color-primary="" data-md-color-scheme="default" id="__palette_1" name="__palette" type="radio"/>
|
|
<input aria-hidden="true" class="md-option" data-md-color-accent="" data-md-color-media="(prefers-color-scheme: dark)" data-md-color-primary="" data-md-color-scheme="slate" id="__palette_2" name="__palette" type="radio"/>
|
|
</form>
|
|
<label class="md-header__button md-icon" for="__search">
|
|
<svg viewbox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path d="M9.5 3A6.5 6.5 0 0 1 16 9.5c0 1.61-.59 3.09-1.56 4.23l.27.27h.79l5 5-1.5 1.5-5-5v-.79l-.27-.27A6.516 6.516 0 0 1 9.5 16 6.5 6.5 0 0 1 3 9.5 6.5 6.5 0 0 1 9.5 3m0 2C7 5 5 7 5 9.5S7 14 9.5 14 14 12 14 9.5 12 5 9.5 5Z"></path></svg>
|
|
</label>
|
|
<div class="md-search" data-md-component="search" role="dialog">
|
|
<label class="md-search__overlay" for="__search"></label>
|
|
<div class="md-search__inner" role="search">
|
|
<form class="md-search__form" name="search">
|
|
<input aria-label="Search" autocapitalize="off" autocomplete="off" autocorrect="off" class="md-search__input" data-md-component="search-query" name="query" placeholder="Search" required="" spellcheck="false" type="text"/>
|
|
<label class="md-search__icon md-icon" for="__search">
|
|
<svg viewbox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path d="M9.5 3A6.5 6.5 0 0 1 16 9.5c0 1.61-.59 3.09-1.56 4.23l.27.27h.79l5 5-1.5 1.5-5-5v-.79l-.27-.27A6.516 6.516 0 0 1 9.5 16 6.5 6.5 0 0 1 3 9.5 6.5 6.5 0 0 1 9.5 3m0 2C7 5 5 7 5 9.5S7 14 9.5 14 14 12 14 9.5 12 5 9.5 5Z"></path></svg>
|
|
<svg viewbox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"></path></svg>
|
|
</label>
|
|
<nav aria-label="Search" class="md-search__options">
|
|
<button aria-label="Clear" class="md-search__icon md-icon" tabindex="-1" title="Clear" type="reset">
|
|
<svg viewbox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path d="M19 6.41 17.59 5 12 10.59 6.41 5 5 6.41 10.59 12 5 17.59 6.41 19 12 13.41 17.59 19 19 17.59 13.41 12 19 6.41Z"></path></svg>
|
|
</button>
|
|
</nav>
|
|
</form>
|
|
<div class="md-search__output">
|
|
<div class="md-search__scrollwrap" data-md-scrollfix="">
|
|
<div class="md-search-result" data-md-component="search-result">
|
|
<div class="md-search-result__meta">
|
|
Initializing search
|
|
</div>
|
|
<ol class="md-search-result__list"></ol>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</nav>
|
|
</header>
|
|
<div class="md-container" data-md-component="container">
|
|
<main class="md-main" data-md-component="main">
|
|
<div class="md-main__inner md-grid">
|
|
<div class="md-sidebar md-sidebar--primary" data-md-component="sidebar" data-md-type="navigation">
|
|
<div class="md-sidebar__scrollwrap">
|
|
<div class="md-sidebar__inner">
|
|
<nav aria-label="Navigation" class="md-nav md-nav--primary" data-md-level="0">
|
|
<label class="md-nav__title" for="__drawer">
|
|
<a aria-label="Documentation Track Trends" class="md-nav__button md-logo" data-md-component="logo" href="../index.html" title="Documentation Track Trends">
|
|
<svg viewbox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path d="M12 8a3 3 0 0 0 3-3 3 3 0 0 0-3-3 3 3 0 0 0-3 3 3 3 0 0 0 3 3m0 3.54C9.64 9.35 6.5 8 3 8v11c3.5 0 6.64 1.35 9 3.54 2.36-2.19 5.5-3.54 9-3.54V8c-3.5 0-6.64 1.35-9 3.54Z"></path></svg>
|
|
</a>
|
|
Documentation Track Trends
|
|
</label>
|
|
<ul class="md-nav__list" data-md-scrollfix="">
|
|
<li class="md-nav__item">
|
|
<a class="md-nav__link" href="../index.html">
|
|
Rapport Track Trends V1.0
|
|
</a>
|
|
</li>
|
|
<li class="md-nav__item">
|
|
<a class="md-nav__link" href="../CahierDesCharges.html">
|
|
Cahier des charges
|
|
</a>
|
|
</li>
|
|
<li class="md-nav__item">
|
|
<a class="md-nav__link" href="../jdb.html">
|
|
Journal de bord
|
|
</a>
|
|
</li>
|
|
<li class="md-nav__item md-nav__item--active md-nav__item--nested">
|
|
<input checked="" class="md-nav__toggle md-toggle" data-md-toggle="__nav_4" id="__nav_4" type="checkbox"/>
|
|
<label class="md-nav__link" for="__nav_4">
|
|
Code
|
|
<span class="md-nav__icon md-icon"></span>
|
|
</label>
|
|
<nav aria-label="Code" class="md-nav" data-md-level="1">
|
|
<label class="md-nav__title" for="__nav_4">
|
|
<span class="md-nav__icon md-icon"></span>
|
|
Code
|
|
</label>
|
|
<ul class="md-nav__list" data-md-scrollfix="">
|
|
<li class="md-nav__item">
|
|
<a class="md-nav__link" href="ConfigurationTool.html">
|
|
ConfigurationTool.cs
|
|
</a>
|
|
</li>
|
|
<li class="md-nav__item">
|
|
<a class="md-nav__link" href="DriverGapToLeaderWindow.html">
|
|
DriverGapToLeaderWindow.cs
|
|
</a>
|
|
</li>
|
|
<li class="md-nav__item">
|
|
<a class="md-nav__link" href="DriverPositionWindow.html">
|
|
DriverPositionWindow.cs
|
|
</a>
|
|
</li>
|
|
<li class="md-nav__item">
|
|
<a class="md-nav__link" href="F1TVEmulator.html">
|
|
F1TVEmulator.cs
|
|
</a>
|
|
</li>
|
|
<li class="md-nav__item">
|
|
<a class="md-nav__link" href="Program.html">
|
|
Program.cs
|
|
</a>
|
|
</li>
|
|
<li class="md-nav__item">
|
|
<a class="md-nav__link" href="Window.html">
|
|
Window.cs
|
|
</a>
|
|
</li>
|
|
<li class="md-nav__item">
|
|
<a class="md-nav__link" href="DriverData.html">
|
|
DriverData.cs
|
|
</a>
|
|
</li>
|
|
<li class="md-nav__item">
|
|
<a class="md-nav__link" href="DriverLapTimeWindow.html">
|
|
DriverLapTimeWindow.cs
|
|
</a>
|
|
</li>
|
|
<li class="md-nav__item">
|
|
<a class="md-nav__link" href="DriverSectorWindow.html">
|
|
DriverSectorWindow.cs
|
|
</a>
|
|
</li>
|
|
<li class="md-nav__item">
|
|
<a class="md-nav__link" href="Form1.html">
|
|
Form1.cs
|
|
</a>
|
|
</li>
|
|
<li class="md-nav__item">
|
|
<a class="md-nav__link" href="Reader.html">
|
|
Reader.cs
|
|
</a>
|
|
</li>
|
|
<li class="md-nav__item">
|
|
<a class="md-nav__link" href="Zone.html">
|
|
Zone.cs
|
|
</a>
|
|
</li>
|
|
<li class="md-nav__item">
|
|
<a class="md-nav__link" href="DriverDrsWindow.html">
|
|
DriverDrsWindow.cs
|
|
</a>
|
|
</li>
|
|
<li class="md-nav__item">
|
|
<a class="md-nav__link" href="DriverNameWindow.html">
|
|
DriverNameWindow.cs
|
|
</a>
|
|
</li>
|
|
<li class="md-nav__item">
|
|
<a class="md-nav__link" href="DriverTyresWindow.html">
|
|
DriverTyresWindow.cs
|
|
</a>
|
|
</li>
|
|
<li class="md-nav__item md-nav__item--active">
|
|
<input class="md-nav__toggle md-toggle" data-md-toggle="toc" id="__toc" type="checkbox"/>
|
|
<a class="md-nav__link md-nav__link--active" href="OcrImage.html">
|
|
OcrImage.cs
|
|
</a>
|
|
</li>
|
|
<li class="md-nav__item">
|
|
<a class="md-nav__link" href="Settings.html">
|
|
Settings.cs
|
|
</a>
|
|
</li>
|
|
<li class="md-nav__item">
|
|
<a class="md-nav__link" href="recoverCookiesCSV.html">
|
|
recoverCookiesCSV.py
|
|
</a>
|
|
</li>
|
|
</ul>
|
|
</nav>
|
|
</li>
|
|
</ul>
|
|
</nav>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<div class="md-sidebar md-sidebar--secondary" data-md-component="sidebar" data-md-type="toc">
|
|
<div class="md-sidebar__scrollwrap">
|
|
<div class="md-sidebar__inner">
|
|
<nav aria-label="Table of contents" class="md-nav md-nav--secondary">
|
|
</nav>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<div class="md-content" data-md-component="content">
|
|
<article class="md-content__inner md-typeset">
|
|
<h1 id="ocrimagecs">OcrImage.cs</h1>
|
|
<pre><code class="language-cs">/// Author : Maxime Rohmer
|
|
/// Date : 08/05/2023
|
|
/// File : OcrImage.cs
|
|
/// Brief : Class containing all the methods used to enhance images for OCR
|
|
/// Version : 0.1
|
|
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Threading.Tasks;
|
|
using System.Drawing;
|
|
using System.Drawing.Drawing2D;
|
|
using System.Drawing.Imaging;
|
|
|
|
namespace Test_Merge
|
|
{
|
|
public class OcrImage
|
|
{
|
|
//this is a hardcoded value based on the colors of the F1TV data channel background you can change it if sometime in the future the color changes
|
|
//Any color that has any of its R,G or B channel higher than the treshold will be considered as being usefull information
|
|
public static Color F1TV_BACKGROUND_TRESHOLD = Color.FromArgb(0x50, 0x50, 0x50);
|
|
Bitmap InputBitmap;
|
|
public enum WindowType
|
|
{
|
|
LapTime,
|
|
Text,
|
|
Sector,
|
|
Gap,
|
|
Tyre,
|
|
}
|
|
|
|
/// <summary>
|
|
/// Create a new Ocr image to help enhance the given bitmap for OCR
|
|
/// </summary>
|
|
/// <param name="inputBitmap">The image you want to enhance</param>
|
|
public OcrImage(Bitmap inputBitmap)
|
|
{
|
|
InputBitmap = inputBitmap;
|
|
}
|
|
/// <summary>
|
|
/// Enhances the image depending on wich type of window the image comes from
|
|
/// </summary>
|
|
/// <param name="type">The type of the window. Depending on it different enhancing features will be applied</param>
|
|
/// <returns>The enhanced Bitmap</returns>
|
|
public Bitmap Enhance(WindowType type = WindowType.Text)
|
|
{
|
|
Bitmap outputBitmap = (Bitmap)InputBitmap.Clone();
|
|
switch (type)
|
|
{
|
|
case WindowType.LapTime:
|
|
outputBitmap = Tresholding(outputBitmap, 185);
|
|
outputBitmap = Resize(outputBitmap, 2);
|
|
outputBitmap = Dilatation(outputBitmap, 1);
|
|
outputBitmap = Erode(outputBitmap, 1);
|
|
break;
|
|
case WindowType.Text:
|
|
outputBitmap = InvertColors(outputBitmap);
|
|
outputBitmap = Tresholding(outputBitmap, 165);
|
|
outputBitmap = Resize(outputBitmap, 2);
|
|
outputBitmap = Dilatation(outputBitmap, 1);
|
|
break;
|
|
case WindowType.Tyre:
|
|
outputBitmap = RemoveUseless(outputBitmap);
|
|
outputBitmap = Resize(outputBitmap, 4);
|
|
outputBitmap = Dilatation(outputBitmap, 1);
|
|
break;
|
|
default:
|
|
outputBitmap = Tresholding(outputBitmap, 165);
|
|
outputBitmap = Resize(outputBitmap, 4);
|
|
outputBitmap = Erode(outputBitmap, 1);
|
|
break;
|
|
}
|
|
return outputBitmap;
|
|
}
|
|
/// <summary>
|
|
/// Method that convert a colored RGB bitmap into a GrayScale image
|
|
/// </summary>
|
|
/// <param name="inputBitmap">The Bitmap you want to convert</param>
|
|
/// <returns>The bitmap in grayscale</returns>
|
|
public static Bitmap Grayscale(Bitmap inputBitmap)
|
|
{
|
|
Rectangle rect = new Rectangle(0, 0, inputBitmap.Width, inputBitmap.Height);
|
|
BitmapData bmpData = inputBitmap.LockBits(rect, ImageLockMode.ReadWrite, inputBitmap.PixelFormat);
|
|
int bytesPerPixel = Bitmap.GetPixelFormatSize(inputBitmap.PixelFormat) / 8;
|
|
|
|
unsafe
|
|
{
|
|
byte* ptr = (byte*)bmpData.Scan0.ToPointer();
|
|
for (int y = 0; y < inputBitmap.Height; y++)
|
|
{
|
|
byte* currentLine = ptr + (y * bmpData.Stride);
|
|
for (int x = 0; x < inputBitmap.Width; x++)
|
|
{
|
|
byte* pixel = currentLine + (x * bytesPerPixel);
|
|
|
|
byte blue = pixel[0];
|
|
byte green = pixel[1];
|
|
byte red = pixel[2];
|
|
|
|
//Those a specific values to correct the weights so its more pleasing to the human eye
|
|
int gray = (int)(red * 0.3 + green * 0.59 + blue * 0.11);
|
|
|
|
pixel[0] = pixel[1] = pixel[2] = (byte)gray;
|
|
}
|
|
}
|
|
}
|
|
inputBitmap.UnlockBits(bmpData);
|
|
|
|
return inputBitmap;
|
|
}
|
|
/// <summary>
|
|
/// Method that binaries the input image up to a certain treshold given
|
|
/// </summary>
|
|
/// <param name="inputBitmap">the bitmap you want to convert to binary colors</param>
|
|
/// <param name="threshold">The floor at wich the color is considered as white or black</param>
|
|
/// <returns>The binarised bitmap</returns>
|
|
public static Bitmap Tresholding(Bitmap inputBitmap, int threshold)
|
|
{
|
|
Rectangle rect = new Rectangle(0, 0, inputBitmap.Width, inputBitmap.Height);
|
|
BitmapData bmpData = inputBitmap.LockBits(rect, ImageLockMode.ReadWrite, inputBitmap.PixelFormat);
|
|
int bytesPerPixel = Bitmap.GetPixelFormatSize(inputBitmap.PixelFormat) / 8;
|
|
|
|
unsafe
|
|
{
|
|
byte* ptr = (byte*)bmpData.Scan0.ToPointer();
|
|
int bmpHeight = inputBitmap.Height;
|
|
int bmpWidth = inputBitmap.Width;
|
|
Parallel.For(0, bmpHeight, y =>
|
|
{
|
|
byte* currentLine = ptr + (y * bmpData.Stride);
|
|
for (int x = 0; x < bmpWidth; x++)
|
|
{
|
|
byte* pixel = currentLine + (x * bytesPerPixel);
|
|
|
|
byte blue = pixel[0];
|
|
byte green = pixel[1];
|
|
byte red = pixel[2];
|
|
//Those a specific values to correct the weights so its more pleasing to the human eye
|
|
int gray = (int)(red * 0.3 + green * 0.59 + blue * 0.11);
|
|
int value = gray < threshold ? 0 : 255;
|
|
|
|
pixel[0] = pixel[1] = pixel[2] = (byte)value;
|
|
}
|
|
});
|
|
}
|
|
inputBitmap.UnlockBits(bmpData);
|
|
|
|
return inputBitmap;
|
|
}
|
|
/// <summary>
|
|
/// Method that removes the pixels that are flagged as background
|
|
/// </summary>
|
|
/// <param name="inputBitmap">The bitmap you want to remove the background from</param>
|
|
/// <returns>The Bitmap without the background</returns>
|
|
public static Bitmap RemoveBG(Bitmap inputBitmap)
|
|
{
|
|
Rectangle rect = new Rectangle(0, 0, inputBitmap.Width, inputBitmap.Height);
|
|
BitmapData bmpData = inputBitmap.LockBits(rect, ImageLockMode.ReadWrite, inputBitmap.PixelFormat);
|
|
int bytesPerPixel = Bitmap.GetPixelFormatSize(inputBitmap.PixelFormat) / 8;
|
|
|
|
unsafe
|
|
{
|
|
byte* ptr = (byte*)bmpData.Scan0.ToPointer();
|
|
for (int y = 0; y < inputBitmap.Height; y++)
|
|
{
|
|
byte* currentLine = ptr + (y * bmpData.Stride);
|
|
for (int x = 0; x < inputBitmap.Width; x++)
|
|
{
|
|
byte* pixel = currentLine + (x * bytesPerPixel);
|
|
|
|
int B = pixel[0];
|
|
int G = pixel[1];
|
|
int R = pixel[2];
|
|
|
|
if (R <= F1TV_BACKGROUND_TRESHOLD.R && G <= F1TV_BACKGROUND_TRESHOLD.G && B <= F1TV_BACKGROUND_TRESHOLD.B)
|
|
pixel[0] = pixel[1] = pixel[2] = 0;
|
|
}
|
|
}
|
|
}
|
|
inputBitmap.UnlockBits(bmpData);
|
|
|
|
return inputBitmap;
|
|
}
|
|
/// <summary>
|
|
/// Method that removes all the useless things from the image and returns hopefully only the numbers
|
|
/// </summary>
|
|
/// <param name="inputBitmap">The bitmap you want to remove useless things from (Expects a cropped part of the TyreWindow)</param>
|
|
/// <returns>The bitmap with (hopefully) only the digits</returns>
|
|
public unsafe static Bitmap RemoveUseless(Bitmap inputBitmap)
|
|
{
|
|
//Note you can use something else than a cropped tyre window but I would recommend checking the code first to see if it fits your intended use
|
|
Rectangle rect = new Rectangle(0, 0, inputBitmap.Width, inputBitmap.Height);
|
|
BitmapData bmpData = inputBitmap.LockBits(rect, ImageLockMode.ReadWrite, inputBitmap.PixelFormat);
|
|
int bytesPerPixel = Bitmap.GetPixelFormatSize(inputBitmap.PixelFormat) / 8;
|
|
|
|
byte* ptr = (byte*)bmpData.Scan0.ToPointer();
|
|
for (int y = 0; y < inputBitmap.Height; y++)
|
|
{
|
|
byte* currentLine = ptr + (y * bmpData.Stride);
|
|
|
|
List<int> pixelsToRemove = new List<int>();
|
|
|
|
bool fromBorder = true;
|
|
|
|
for (int x = 0; x < inputBitmap.Width; x++)
|
|
{
|
|
byte* pixel = currentLine + (x * bytesPerPixel);
|
|
|
|
int B = pixel[0];
|
|
int G = pixel[1];
|
|
int R = pixel[2];
|
|
|
|
if (fromBorder && B < F1TV_BACKGROUND_TRESHOLD.B && G < F1TV_BACKGROUND_TRESHOLD.G && R < F1TV_BACKGROUND_TRESHOLD.R)
|
|
{
|
|
pixelsToRemove.Add(x);
|
|
}
|
|
else
|
|
{
|
|
if (fromBorder)
|
|
{
|
|
fromBorder = false;
|
|
pixelsToRemove.Add(x);
|
|
}
|
|
}
|
|
}
|
|
fromBorder = true;
|
|
for (int x = inputBitmap.Width - 1; x > 0; x--)
|
|
{
|
|
byte* pixel = currentLine + (x * bytesPerPixel);
|
|
|
|
int B = pixel[0];
|
|
int G = pixel[1];
|
|
int R = pixel[2];
|
|
|
|
if (fromBorder && B < F1TV_BACKGROUND_TRESHOLD.B && G < F1TV_BACKGROUND_TRESHOLD.G && R < F1TV_BACKGROUND_TRESHOLD.R)
|
|
{
|
|
pixelsToRemove.Add(x);
|
|
}
|
|
else
|
|
{
|
|
if (fromBorder)
|
|
{
|
|
fromBorder = false;
|
|
pixelsToRemove.Add(x);
|
|
}
|
|
}
|
|
}
|
|
|
|
foreach (int pxPos in pixelsToRemove)
|
|
{
|
|
byte* pixel = currentLine + (pxPos * bytesPerPixel);
|
|
|
|
pixel[0] = 0xFF;
|
|
pixel[1] = 0xFF;
|
|
pixel[2] = 0xFF;
|
|
}
|
|
}
|
|
|
|
//Removing the color parts
|
|
for (int y = 0; y < inputBitmap.Height; y++)
|
|
{
|
|
byte* currentLine = ptr + (y * bmpData.Stride);
|
|
for (int x = 0; x < inputBitmap.Width; x++)
|
|
{
|
|
byte* pixel = currentLine + (x * bytesPerPixel);
|
|
|
|
int B = pixel[0];
|
|
int G = pixel[1];
|
|
int R = pixel[2];
|
|
|
|
if (R >= F1TV_BACKGROUND_TRESHOLD.R + 15 || G >= F1TV_BACKGROUND_TRESHOLD.G + 15 || B >= F1TV_BACKGROUND_TRESHOLD.B + 15)
|
|
{
|
|
pixel[0] = 0xFF;
|
|
pixel[1] = 0xFF;
|
|
pixel[2] = 0xFF;
|
|
}
|
|
}
|
|
}
|
|
|
|
inputBitmap.UnlockBits(bmpData);
|
|
return inputBitmap;
|
|
}
|
|
/// <summary>
|
|
/// Recovers the average colors from the Image. NOTE : It wont take in account colors that are lower than the background
|
|
/// </summary>
|
|
/// <param name="inputBitmap">The bitmap you want to get the average color from</param>
|
|
/// <returns>The average color of the bitmap</returns>
|
|
public static Color GetAvgColorFromBitmap(Bitmap inputBitmap)
|
|
{
|
|
Rectangle rect = new Rectangle(0, 0, inputBitmap.Width, inputBitmap.Height);
|
|
BitmapData bmpData = inputBitmap.LockBits(rect, ImageLockMode.ReadWrite, inputBitmap.PixelFormat);
|
|
int bytesPerPixel = Bitmap.GetPixelFormatSize(inputBitmap.PixelFormat) / 8;
|
|
|
|
int totR = 0;
|
|
int totG = 0;
|
|
int totB = 0;
|
|
|
|
int totPixels = 1;
|
|
|
|
unsafe
|
|
{
|
|
byte* ptr = (byte*)bmpData.Scan0.ToPointer();
|
|
int bmpHeight = inputBitmap.Height;
|
|
int bmpWidth = inputBitmap.Width;
|
|
Parallel.For(0, bmpHeight, y =>
|
|
{
|
|
byte* currentLine = ptr + (y * bmpData.Stride);
|
|
for (int x = 0; x < bmpWidth; x++)
|
|
{
|
|
byte* pixel = currentLine + (x * bytesPerPixel);
|
|
|
|
int B = pixel[0];
|
|
int G = pixel[1];
|
|
int R = pixel[2];
|
|
|
|
if (R >= F1TV_BACKGROUND_TRESHOLD.R || G >= F1TV_BACKGROUND_TRESHOLD.G || B >= F1TV_BACKGROUND_TRESHOLD.B)
|
|
{
|
|
totPixels++;
|
|
totB += pixel[0];
|
|
totG += pixel[1];
|
|
totR += pixel[2];
|
|
}
|
|
}
|
|
});
|
|
}
|
|
inputBitmap.UnlockBits(bmpData);
|
|
|
|
return Color.FromArgb(255, Convert.ToInt32((float)totR / (float)totPixels), Convert.ToInt32((float)totG / (float)totPixels), Convert.ToInt32((float)totB / (float)totPixels));
|
|
}
|
|
/// <summary>
|
|
/// This method simply inverts all the colors in a Bitmap
|
|
/// </summary>
|
|
/// <param name="inputBitmap">the bitmap you want to invert the colors from</param>
|
|
/// <returns>The bitmap with inverted colors</returns>
|
|
public static Bitmap InvertColors(Bitmap inputBitmap)
|
|
{
|
|
Rectangle rect = new Rectangle(0, 0, inputBitmap.Width, inputBitmap.Height);
|
|
BitmapData bmpData = inputBitmap.LockBits(rect, ImageLockMode.ReadWrite, inputBitmap.PixelFormat);
|
|
int bytesPerPixel = Bitmap.GetPixelFormatSize(inputBitmap.PixelFormat) / 8;
|
|
|
|
unsafe
|
|
{
|
|
byte* ptr = (byte*)bmpData.Scan0.ToPointer();
|
|
for (int y = 0; y < inputBitmap.Height; y++)
|
|
{
|
|
byte* currentLine = ptr + (y * bmpData.Stride);
|
|
for (int x = 0; x < inputBitmap.Width; x++)
|
|
{
|
|
byte* pixel = currentLine + (x * bytesPerPixel);
|
|
|
|
pixel[0] = (byte)(255 - pixel[0]);
|
|
pixel[1] = (byte)(255 - pixel[1]);
|
|
pixel[2] = (byte)(255 - pixel[2]);
|
|
}
|
|
}
|
|
}
|
|
inputBitmap.UnlockBits(bmpData);
|
|
|
|
return inputBitmap;
|
|
}
|
|
/// <summary>
|
|
/// Methods that applies Bicubic interpolation to increase the size and resolution of an image
|
|
/// </summary>
|
|
/// <param name="inputBitmap">The bitmap you want to resize</param>
|
|
/// <param name="resizeFactor">The factor of resizing you want to use. I recommend using even numbers</param>
|
|
/// <returns>The bitmap witht the new size</returns>
|
|
public static Bitmap Resize(Bitmap inputBitmap, int resizeFactor)
|
|
{
|
|
var resultBitmap = new Bitmap(inputBitmap.Width * resizeFactor, inputBitmap.Height * resizeFactor);
|
|
|
|
using (var graphics = Graphics.FromImage(resultBitmap))
|
|
{
|
|
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
|
|
graphics.DrawImage(inputBitmap, new Rectangle(0, 0, resultBitmap.Width, resultBitmap.Height));
|
|
}
|
|
|
|
return resultBitmap;
|
|
}
|
|
/// <summary>
|
|
/// method that Highlights the countours of a Bitmap
|
|
/// </summary>
|
|
/// <param name="inputBitmap">The bitmap you want to highlight the countours of</param>
|
|
/// <returns>The bitmap with countours highlighted</returns>
|
|
public static Bitmap HighlightContours(Bitmap inputBitmap)
|
|
{
|
|
Bitmap outputBitmap = new Bitmap(inputBitmap.Width, inputBitmap.Height);
|
|
|
|
Bitmap grayscale = Grayscale(inputBitmap);
|
|
Bitmap thresholded = Tresholding(grayscale, 128);
|
|
Bitmap dilated = Dilatation(thresholded, 3);
|
|
Bitmap eroded = Erode(dilated, 3);
|
|
|
|
for (int y = 0; y < inputBitmap.Height; y++)
|
|
{
|
|
for (int x = 0; x < inputBitmap.Width; x++)
|
|
{
|
|
Color pixel = inputBitmap.GetPixel(x, y);
|
|
Color dilatedPixel = dilated.GetPixel(x, y);
|
|
Color erodedPixel = eroded.GetPixel(x, y);
|
|
|
|
int gray = (int)(pixel.R * 0.3 + pixel.G * 0.59 + pixel.B * 0.11);
|
|
int threshold = dilatedPixel.R;
|
|
|
|
if (gray > threshold)
|
|
{
|
|
outputBitmap.SetPixel(x, y, Color.FromArgb(255, 255, 255));
|
|
}
|
|
else if (gray <= threshold && erodedPixel.R == 0)
|
|
{
|
|
outputBitmap.SetPixel(x, y, Color.FromArgb(255, 0, 0));
|
|
}
|
|
else
|
|
{
|
|
outputBitmap.SetPixel(x, y, Color.FromArgb(0, 0, 0));
|
|
}
|
|
}
|
|
}
|
|
|
|
return outputBitmap;
|
|
}
|
|
/// <summary>
|
|
/// Method that that erodes the morphology of a bitmap
|
|
/// </summary>
|
|
/// <param name="inputBitmap">The bitmap you want to erode</param>
|
|
/// <param name="kernelSize">The amount of Erosion you want (be carefull its expensive on ressources)</param>
|
|
/// <returns>The Bitmap with the eroded contents</returns>
|
|
public static Bitmap Erode(Bitmap inputBitmap, int kernelSize)
|
|
{
|
|
Bitmap outputBitmap = new Bitmap(inputBitmap.Width, inputBitmap.Height);
|
|
|
|
int[,] kernel = new int[kernelSize, kernelSize];
|
|
|
|
for (int i = 0; i < kernelSize; i++)
|
|
{
|
|
for (int j = 0; j < kernelSize; j++)
|
|
{
|
|
kernel[i, j] = 1;
|
|
}
|
|
}
|
|
|
|
for (int y = kernelSize / 2; y < inputBitmap.Height - kernelSize / 2; y++)
|
|
{
|
|
for (int x = kernelSize / 2; x < inputBitmap.Width - kernelSize / 2; x++)
|
|
{
|
|
bool flag = true;
|
|
|
|
for (int i = -kernelSize / 2; i <= kernelSize / 2; i++)
|
|
{
|
|
for (int j = -kernelSize / 2; j <= kernelSize / 2; j++)
|
|
{
|
|
Color pixel = inputBitmap.GetPixel(x + i, y + j);
|
|
int gray = (int)(pixel.R * 0.3 + pixel.G * 0.59 + pixel.B * 0.11);
|
|
|
|
if (gray >= 128 && kernel[i + kernelSize / 2, j + kernelSize / 2] == 1)
|
|
{
|
|
flag = false;
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (!flag)
|
|
{
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (flag)
|
|
{
|
|
outputBitmap.SetPixel(x, y, Color.FromArgb(255, 255, 255));
|
|
}
|
|
else
|
|
{
|
|
outputBitmap.SetPixel(x, y, Color.FromArgb(0, 0, 0));
|
|
}
|
|
}
|
|
}
|
|
|
|
return outputBitmap;
|
|
}
|
|
/// <summary>
|
|
/// Method that that use dilatation of the morphology of a bitmap
|
|
/// </summary>
|
|
/// <param name="inputBitmap">The bitmap you want to use dilatation on</param>
|
|
/// <param name="kernelSize">The amount of dilatation you want (be carefull its expensive on ressources)</param>
|
|
/// <returns>The Bitmap after Dilatation</returns>
|
|
public static Bitmap Dilatation(Bitmap inputBitmap, int kernelSize)
|
|
{
|
|
Bitmap outputBitmap = new Bitmap(inputBitmap.Width, inputBitmap.Height);
|
|
|
|
int[,] kernel = new int[kernelSize, kernelSize];
|
|
|
|
for (int i = 0; i < kernelSize; i++)
|
|
{
|
|
for (int j = 0; j < kernelSize; j++)
|
|
{
|
|
kernel[i, j] = 1;
|
|
}
|
|
}
|
|
|
|
for (int y = kernelSize / 2; y < inputBitmap.Height - kernelSize / 2; y++)
|
|
{
|
|
for (int x = kernelSize / 2; x < inputBitmap.Width - kernelSize / 2; x++)
|
|
{
|
|
bool flag = false;
|
|
|
|
for (int i = -kernelSize / 2; i <= kernelSize / 2; i++)
|
|
{
|
|
for (int j = -kernelSize / 2; j <= kernelSize / 2; j++)
|
|
{
|
|
Color pixel = inputBitmap.GetPixel(x + i, y + j);
|
|
int gray = (int)(pixel.R * 0.3 + pixel.G * 0.59 + pixel.B * 0.11);
|
|
|
|
if (gray < 128 && kernel[i + kernelSize / 2, j + kernelSize / 2] == 1)
|
|
{
|
|
flag = true;
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (flag)
|
|
{
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (flag)
|
|
{
|
|
outputBitmap.SetPixel(x, y, Color.FromArgb(0, 0, 0));
|
|
}
|
|
else
|
|
{
|
|
outputBitmap.SetPixel(x, y, Color.FromArgb(255, 255, 255));
|
|
}
|
|
}
|
|
}
|
|
|
|
return outputBitmap;
|
|
}
|
|
}
|
|
}
|
|
|
|
</code></pre>
|
|
</article>
|
|
</div>
|
|
</div>
|
|
</main>
|
|
<footer class="md-footer">
|
|
<nav aria-label="Footer" class="md-footer__inner md-grid">
|
|
<a aria-label="Previous: DriverTyresWindow.cs" class="md-footer__link md-footer__link--prev" href="DriverTyresWindow.html" rel="prev">
|
|
<div class="md-footer__button md-icon">
|
|
<svg viewbox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"></path></svg>
|
|
</div>
|
|
<div class="md-footer__title">
|
|
<div class="md-ellipsis">
|
|
<span class="md-footer__direction">
|
|
Previous
|
|
</span>
|
|
DriverTyresWindow.cs
|
|
</div>
|
|
</div>
|
|
</a>
|
|
<a aria-label="Next: Settings.cs" class="md-footer__link md-footer__link--next" href="Settings.html" rel="next">
|
|
<div class="md-footer__title">
|
|
<div class="md-ellipsis">
|
|
<span class="md-footer__direction">
|
|
Next
|
|
</span>
|
|
Settings.cs
|
|
</div>
|
|
</div>
|
|
<div class="md-footer__button md-icon">
|
|
<svg viewbox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"></path></svg>
|
|
</div>
|
|
</a>
|
|
</nav>
|
|
<div class="md-footer-meta md-typeset">
|
|
<div class="md-footer-meta__inner md-grid">
|
|
<div class="md-copyright">
|
|
<div class="md-copyright__highlight">
|
|
©CFPTI Tech2
|
|
</div>
|
|
|
|
|
|
Made with
|
|
<a href="https://squidfunk.github.io/mkdocs-material/" rel="noopener" target="_blank">
|
|
Material for MkDocs
|
|
</a>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</footer>
|
|
</div>
|
|
<div class="md-dialog" data-md-component="dialog">
|
|
<div class="md-dialog__inner md-typeset"></div>
|
|
</div>
|
|
<script id="__config" type="application/json">{"base": "..", "features": [], "search": "../assets/javascripts/workers/search.ecf98df9.min.js", "translations": {"clipboard.copied": "Copied to clipboard", "clipboard.copy": "Copy to clipboard", "search.config.lang": "en", "search.config.pipeline": "trimmer, stopWordFilter", "search.config.separator": "[\\s\\-]+", "search.placeholder": "Search", "search.result.more.one": "1 more on this page", "search.result.more.other": "# more on this page", "search.result.none": "No matching documents", "search.result.one": "1 matching document", "search.result.other": "# matching documents", "search.result.placeholder": "Type to start searching", "search.result.term.missing": "Missing", "select.version.title": "Select version"}}</script>
|
|
<script src="../assets/javascripts/bundle.48f2be22.min.js"></script>
|
|
<script>document$.subscribe(() => {const lightbox = GLightbox({"touchNavigation": true, "loop": false, "width": "100%", "height": "auto", "zoomable": true, "draggable": true, "openEffect": "zoom", "closeEffect": "zoom"});})</script></body>
|
|
</html> |