惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园 - 三生石上(FineUI控件)
U
Unit 42
人人都是产品经理
人人都是产品经理
罗磊的独立博客
Recent Announcements
Recent Announcements
云风的 BLOG
云风的 BLOG
aimingoo的专栏
aimingoo的专栏
T
Tailwind CSS Blog
GbyAI
GbyAI
Blog — PlanetScale
Blog — PlanetScale
I
InfoQ
Last Week in AI
Last Week in AI
宝玉的分享
宝玉的分享
B
Blog RSS Feed
WordPress大学
WordPress大学
腾讯CDC
H
Help Net Security
博客园 - Franky
博客园 - 【当耐特】
博客园 - 聂微东
Stack Overflow Blog
Stack Overflow Blog
B
Blog
Vercel News
Vercel News
博客园 - 司徒正美

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant
Building a License Plate Recognition Engine in C++ — Part...
Edward Obar · 2026-05-16 · via DEV Community

In the previous article, we loaded an image, converted it into grayscale, and introduced the core data structures used by the recognition engine.

In this part, we begin building the preprocessing stage of the LPR pipeline.

The goal of preprocessing is to enhance image regions that are likely to contain license plate characters.

We will implement:

  • Integral image generation
  • Local contrast analysis
  • Edge map extraction
  • Ternary edge image conversion

These operations are designed for high-speed processing and are suitable for real-time systems.

Why preprocessing matters in LPR

License plates typically contain:

  • High-contrast characters
  • Dense vertical and horizontal transitions
  • Repetitive edge structures

Instead of applying expensive operations across the entire image, preprocessing helps emphasize candidate regions before plate detection begins.

The pipeline implemented in this article focuses on local intensity differences.

Integral Image

The first optimization step is building an integral image.

An integral image allows us to compute the sum of any rectangular region in constant time.

Without an integral image:

Region Sum = O(width × height)

Enter fullscreen mode Exit fullscreen mode

With an integral image:

Region Sum = O(1)

Enter fullscreen mode Exit fullscreen mode

This becomes extremely important when processing every pixel in large images.

Computing the integral image

void CImageProc::ComputeIntegralImage(
    unsigned char* pbGray,
    int nWidth,
    int nHeight,
    int* pnSum)
{
    int nW = nWidth + 1;
    int nH = nHeight + 1;

    int partialsum;

    memset(pnSum, 0, nH * nW * sizeof(int));

    for (int y = 1; y < nH; y++)
    {
        pnSum[y * nW] = 0;
        partialsum = 0;

        for (int x = 1; x < nW; x++)
        {
            partialsum +=
                (int)pbGray[
                    (y - 1) * nWidth + (x - 1)];

            pnSum[y * nW + x] =
                pnSum[(y - 1) * nW + x]
                + partialsum;
        }
    }
}

Enter fullscreen mode Exit fullscreen mode

Fast local contrast edge map

void CImageProc::ComputeLocalContrastEdgeMap(
    unsigned char* pbGray, 
    int* pnSum, 
    int* pnEdge, 
    int nWidth, 
    int nHeight)
{
    if (!pbGray || !pnSum || !pnEdge || nWidth <= 0 || nHeight <= 0)
        return;

    const int rw = 3;
    const int rh = 3;
    const int nW = nWidth + 1;


    const int centerCount = 5;
    const int windowCount = (rw * 2 + 1) * (rh * 2 + 1);
    const int surroundCount = windowCount - centerCount;

    if (nWidth <= rw * 2 || nHeight <= rh * 2)
        return;

    for (int y = rh; y < nHeight - rh; y++)
    {
        const int row = y * nWidth;
        const int prevRow = (y - 1) * nWidth;
        const int nextRow = (y + 1) * nWidth;

        const int top = y - rh;
        const int bottom = y + rh + 1;
        for (int x = (rw + 1); x < nWidth - rw; x++)
        {
            const int left = x - rw;
            const int right = x + rw + 1;

            int centerSum =
                pbGray[prevRow + x] +
                pbGray[row + x - 1] +
                pbGray[row + x] +
                pbGray[row + x + 1] +
                pbGray[nextRow + x];

            int windowSum =
                pnSum[bottom * nW + right]
                - pnSum[top * nW + right]
                - pnSum[bottom * nW + left]
                + pnSum[top * nW + left];

            int surroundSum = windowSum - centerSum;

            double centerAvg = (double)centerSum / centerCount;
            double surroundAvg = (double)surroundSum / surroundCount;

            pnEdge[row + x] = (int)(surroundAvg - centerAvg);
        }
    }
    ComputeBorderLocalContrast(pbGray, pnSum, pnEdge, nWidth, nHeight, 0, nWidth - 1, 0, rh - 1, rw, rh);
    ComputeBorderLocalContrast(pbGray, pnSum, pnEdge, nWidth, nHeight, 0, nWidth - 1, nHeight - rh, nHeight - 1, rw, rh);
    ComputeBorderLocalContrast(pbGray, pnSum, pnEdge, nWidth, nHeight, 0, rw, rh, nHeight - rh, rw, rh);
    ComputeBorderLocalContrast(pbGray, pnSum, pnEdge, nWidth, nHeight, nWidth - rw, nWidth - 1, rh, nHeight - rh, rw, rh);
}

void CImageProc::ComputeBorderLocalContrast(
    unsigned char* pbGray, 
    int* pnSum, 
    int* lpOut, 
    int nWidth, 
    int nHeight, 
    int x0, 
    int x1, 
    int y0, 
    int y1, 
    int rw, 
    int rh)
{
    if (!pbGray || !pnSum || !lpOut || nWidth <= 0 || nHeight <= 0)
        return;

    int nW = nWidth + 1;

    if (x0 < 0) x0 = 0;
    if (y0 < 0) y0 = 0;
    if (x1 >= nWidth) x1 = nWidth - 1;
    if (y1 >= nHeight) y1 = nHeight - 1;

    for (int y = y0; y <= y1; y++)
    {
        int row = y * nWidth;

        for (int x = x0; x <= x1; x++)
        {
            int left = x - rw;
            int right = x + rw;
            int top = y - rh;
            int bottom = y + rh;

            if (left < 0) left = 0;
            if (top < 0) top = 0;
            if (right >= nWidth) right = nWidth - 1;
            if (bottom >= nHeight) bottom = nHeight - 1;

            int surroundCount = (right - left + 1) * (bottom - top + 1);
            int surroundSum = 
                pnSum[(bottom + 1) * nW + (right + 1)] 
                - pnSum[top * nW + (right + 1)] 
                - pnSum[(bottom + 1) * nW + left] 
                + pnSum[top * nW + left];

            int centerSum = 0;
            int centerCount = 0;

            // Center pixel
            centerSum += pbGray[row + x];
            surroundSum -= pbGray[row + x];
            centerCount++;
            surroundCount--;

            // Left
            if (x - 1 >= 0)
            {
                centerSum += pbGray[row + (x - 1)];
                surroundSum -= pbGray[row + (x - 1)];
                centerCount++;
                surroundCount--;
            }

            // Right
            if (x + 1 < nWidth)
            {
                centerSum += pbGray[row + (x + 1)];
                surroundSum -= pbGray[row + (x + 1)];
                centerCount++;
                surroundCount--;
            }

            // Top
            if (y - 1 >= 0)
            {
                centerSum += pbGray[(y - 1) * nWidth + x];
                surroundSum -= pbGray[(y - 1) * nWidth + x];
                centerCount++;
                surroundCount--;
            }

            // Bottom
            if (y + 1 < nHeight)
            {
                centerSum += pbGray[(y + 1) * nWidth + x];
                surroundSum -= pbGray[(y + 1) * nWidth + x];
                centerCount++;
                surroundCount--;
            }

            if (centerCount == 0 || surroundCount <= 0)
            {
                lpOut[row + x] = 0;
                continue;
            }

            double centerAvg = (double)centerSum / centerCount;
            double surroundAvg = (double)surroundSum / surroundCount;
            lpOut[row + x] = (int)(surroundAvg - centerAvg);
        }
    }
}

Enter fullscreen mode Exit fullscreen mode

The algorithm performs:

  1. Local neighborhood extraction
  2. Center intensity averaging
  3. Surrounding intensity averaging
  4. Difference calculation

Result:

Strong intensity transitions
        ↓
High edge responses

Enter fullscreen mode Exit fullscreen mode

This is particularly effective for license plate characters because they contain strong contrast boundaries.

Current preprocessing pipeline
Our LPR engine now performs:

Input Image
      ↓
Grayscale Conversion
      ↓
Integral Image
      ↓
Local Contrast Analysis
      ↓
Edge Map Generation
      ↓
Ternary Edge Image

Enter fullscreen mode Exit fullscreen mode

At this point, the engine can already highlight regions containing dense character-like structures.

In the next article, we will begin locating candidate license plate regions using the generated edge information.