C#
using AForge.Video;
using AForge.Video.DirectShow;
using System.Drawing.Imaging;
namespace Shooting
{
public partial class Form1 : Form
{
private FilterInfoCollection CaptureDevices;
private VideoCaptureDevice FinalFrame;
private bool searchPixels = false;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
CaptureDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
foreach (FilterInfo Device in CaptureDevices)
{
DeviceNameToolStripMenuItem.Items.Add(Device.Name);
}
DeviceNameToolStripMenuItem.SelectedIndex = 0;
StartVideoImage();
}
private void StartVideoImage()
{
FinalFrame = new VideoCaptureDevice(CaptureDevices[DeviceNameToolStripMenuItem.SelectedIndex].MonikerString);
FinalFrame.NewFrame += new NewFrameEventHandler(FinalFrame_NewFrame);
FinalFrame.Start();
}
private void FinalFrame_NewFrame(object sender, NewFrameEventArgs eventArgs)
{
System.Drawing.Image image = (Bitmap)eventArgs.Frame.Clone();
image = MakeGrayscale((Bitmap)image);
using (Graphics g = Graphics.FromImage(image))
{
int width = image.Width;
int height = image.Height;
int thirdWidth = width / 3;
int thirdHeight = height / 3;
g.DrawLine(Pens.Red, thirdWidth, 0, thirdWidth, height);
g.DrawLine(Pens.Red, 2 * thirdWidth, 0, 2 * thirdWidth, height);
g.DrawLine(Pens.Red, 0, thirdHeight, width, thirdHeight);
g.DrawLine(Pens.Red, 0, 2 * thirdHeight, width, 2 * thirdHeight);
}
if (searchPixels)
{
ProcessImage((Bitmap)image);
}
VideoImage.Image = image;
}
private void ProcessImage(Bitmap image)
{
Bitmap binaryImage = Binarize(image, CalculateBlackThreshold(image));
using (Graphics g = Graphics.FromImage(image))
{
Rectangle? markerRect = FindMarker(binaryImage);
if (markerRect.HasValue)
{
g.DrawRectangle(Pens.Yellow, markerRect.Value);
}
}
}
private Rectangle? FindMarker(Bitmap binaryImage)
{
int width = binaryImage.Width / 3;
int height = binaryImage.Height / 3;
int maxDistance = 3;
int? minX = null, minY = null, maxX = null, maxY = null;
for (int y = 0; y = 3)
{
if (!minX.HasValue || x maxX) maxX = x;
if (!minY.HasValue || y maxY) maxY = y;
}
}
}
}
for (int x = 0; x = 3)
{
if (!minX.HasValue || x maxX) maxX = x;
if (!minY.HasValue || y maxY) maxY = y;
}
}
}
}
if (minX.HasValue && minY.HasValue && maxX.HasValue && maxY.HasValue)
{
Rectangle marker = new Rectangle(minX.Value, minY.Value, maxX.Value - minX.Value, maxY.Value - minY.Value);
if (IsSurroundedByWhite(binaryImage, marker))
{
return marker;
}
}
return null;
}
private Bitmap Binarize(Bitmap original, int threshold)
{
Bitmap binarizedBmp = new Bitmap(original.Width, original.Height);
Rectangle rect = new Rectangle(0, 0, original.Width, original.Height);
BitmapData originalData = original.LockBits(rect, ImageLockMode.ReadOnly, original.PixelFormat);
BitmapData binarizedData = binarizedBmp.LockBits(rect, ImageLockMode.WriteOnly, binarizedBmp.PixelFormat);
unsafe
{
byte* originalPtr = (byte*)originalData.Scan0;
byte* binarizedPtr = (byte*)binarizedData.Scan0;
for (int y = 0; y = threshold ? 255 : 0);
binarizedPtr[pixelIndex] = newColor;
binarizedPtr[pixelIndex + 1] = newColor;
binarizedPtr[pixelIndex + 2] = newColor;
binarizedPtr[pixelIndex + 3] = 255;
}
}
}
original.UnlockBits(originalData);
binarizedBmp.UnlockBits(binarizedData);
return binarizedBmp;
}
private int CalculateBlackThreshold(Bitmap image)
{
double averageBrightness = CalculateAverageBrightness(image);
double standardDeviation = CalculateStandardDeviation(image, averageBrightness);
return (int)(averageBrightness - 2 * standardDeviation);
}
private double CalculateAverageBrightness(Bitmap image)
{
int sumBrightness = 0;
int count = 0;
for (int y = 0; y = binaryImage.Width) continue;
if (binaryImage.GetPixel(x, marker.Top - 1).R == 0) return false; // ????
if (binaryImage.GetPixel(x, marker.Bottom + 1).R == 0) return false; // ???
}
for (int y = marker.Top - 1; y = binaryImage.Height) continue;
if (binaryImage.GetPixel(marker.Left - 1, y).R == 0) return false; // ????
if (binaryImage.GetPixel(marker.Right + 1, y).R == 0) return false; // ?????
}
return true;
}
}
}Good afternoon. Please tell me what algorithm you can use to find the label data on the image via a webcam in real time

Local Coders197 reputationPosted Nov 2, 2024, 12:32 PM EDT
Step 1: Set Up Tesseract OCR and OpenCV
Install-Package Tesseract
Install-Package OpenCvSharp4
Step 2: Capture Webcam Feed and Preprocess Frames
using OpenCvSharp;
using Tesseract;
public class RealTimeLabelDetector
{
private VideoCapture _capture;
private TesseractEngine _tesseract;
public RealTimeLabelDetector()
{
// Initialize Tesseract engine (make sure to point to the tessdata path)
_tesseract = new TesseractEngine(@"./tessdata", "eng", EngineMode.Default);
// Initialize webcam capture
_capture = new VideoCapture(0); // 0 is the default camera
_capture.Open(0);
}
public void Start()
{
using (var window = new Window("Label Detection"))
{
while (true)
{
using (var frame = new Mat())
{
// Capture frame-by-frame
_capture.Read(frame);
if (frame.Empty())
break;
// Convert frame to grayscale
Cv2.CvtColor(frame, frame, ColorConversionCodes.BGR2GRAY);
// Apply threshold to enhance text
Cv2.Threshold(frame, frame, 120, 255, ThresholdTypes.Binary);
// Perform OCR on the processed frame
var text = DetectText(frame);
Console.WriteLine("Detected Text: " + text);
// Show the frame
window.ShowImage(frame);
// Exit loop on 'Esc' key
if (Cv2.WaitKey(1) == 27)
break;
}
}
}
}
private string DetectText(Mat frame)
{
string detectedText = string.Empty;
using (var pix = PixConverter.ToPix(frame))
{
using (var page = _tesseract.Process(pix))
{
detectedText = page.GetText();
}
}
return detectedText;
}
public void Dispose()
{
_capture?.Dispose();
_tesseract?.Dispose();
}
}
Step 3: Run the Real-Time Label Detector
static void Main()
{
using (var detector = new RealTimeLabelDetector())
{
detector.Start();
}
}
Explanation of Key Steps