PHPackages                             mohuishou/image-ocr - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Image &amp; Media](/categories/media)
4. /
5. mohuishou/image-ocr

ActiveLibrary[Image &amp; Media](/categories/media)

mohuishou/image-ocr
===================

验证码识别

0.1.2(7y ago)2732.8k68[2 issues](https://github.com/mohuishou/ImageOCR/issues)1MITPHPPHP &gt;5.5

Since Oct 7Pushed 6y ago14 watchersCompare

[ Source](https://github.com/mohuishou/ImageOCR)[ Packagist](https://packagist.org/packages/mohuishou/image-ocr)[ RSS](/packages/mohuishou-image-ocr/feed)WikiDiscussions master Synced 2mo ago

READMEChangelog (3)DependenciesVersions (6)Used By (1)

ImageOCR
========

[](#imageocr)

> php 验证码识别库，对于非粘连字符具有很好的识别效果，对于一般粘连字符也能有较为良好的识别 除噪算法支持孤立点除杂和连通域除噪，分割算法支持等宽分割、连通域分割以及滴水算法分割

示例效果
----

[](#示例效果)

[![示例](./img/1.png)](./img/1.png)

Install
-------

[](#install)

```
composer require mohuishou/image-ocr

```

使用方法
----

[](#使用方法)

例子详见 [example](./example)

### use docker

[](#use-docker)

```
docker run --rm -p 8088:8088 mohuishou/image-ocr
```

点击  查看效果

### 大致流程：

[](#大致流程)

```
初始化 -> 灰度化 ---> 二值化 ---> 除噪点 -> 分割 -> 标准化 -> 识别

```

#### 初始化

[](#初始化)

对象初始化

```
$image=new Image($img_path);
$image_ocr=new ImageOCR($image)
```

初始化二值化阈值

```
$image_ocr->setMaxGrey(90);
$image_ocr->setMinGrey(10);
```

初始化标准化图片宽高

```
$image_ocr->setStandardWidth(13);
$image_ocr->setStandardHeight(20);
```

#### 开启 Debug

[](#开启-debug)

```
$image_ocr->setDebug(true);
```

#### 灰度化

[](#灰度化)

```
try{
    $image_ocr->grey();
}catch (\Exception $e){
    echo $e->getMessage();
}
```

#### 二值化

[](#二值化)

注意：这一步的前提是需要先执行上一步灰度化，不然会抛出一个错误

```
try{
    $image_ocr->hash($max_grey=null,$min_grey=null);
}catch (\Exception $e){
    echo $e->getMessage();
}
```

二值化支持两种方式，第一种`$image_ocr->hash($max_grey=null,$min_grey=null)`即为上面那种固定的阈值范围，第二种为`hashByBackground($model=self::MAX_MODEL,$max_grey=null,$min_grey=null)`，通过背景图像的灰度值，动态取阈值，支持三种模式`MAX_MODEL`,`MIN_MODEL`,`BG_MODEL`分别是最大值、最小值和背景模式，最大值模式会用背景的灰度值替换阈值的上限，最小值模式替换下限，背景模式上下限都替换，即为只去除背景

#### 除噪点

[](#除噪点)

前置条件为二值化

##### 孤立点除噪法

[](#孤立点除噪法)

```
try{
    $image_ocr->removeSpots();
}catch (\Exception $e){
    echo $e->getMessage();
}
```

##### 连通域除噪法

[](#连通域除噪法)

*\[如果要使用连通域分割法，可以跳过连通域除噪点，分割的同时可以一并除噪\]*

```
try{
    //使用之前需要初始化连通域对象
    $image_ocr->setImageConnect();
    //除噪
    $image_ocr->removeSpotsByConnect();
}catch (\Exception $e){
    echo $e->getMessage();
}
```

#### 分割

[](#分割)

##### 非粘连字符串

[](#非粘连字符串)

连通域分割法

```
try{
    //使用之前需要初始化连通域对象
    $image_ocr->setImageConnect();
    //分割
    $image_ocr->splitByConnect();
}catch (\Exception $e){
    echo $e->getMessage();
}
```

##### 粘连字符串

[](#粘连字符串)

滴水算法分割

TODO: 待测试

#### 标准化

[](#标准化)

```
try{
    $standard_data=$image_ocr->standard();
}catch (\Exception $e){
    echo $e->getMessage();
}
```

#### 识别

[](#识别)

TODO:待完善

API
---

[](#api)

```
ImageOCR::__construct(Image $image)
ImageOCR::saveImage($path)
ImageOCR::grey()
ImageOCR::hash($max_grey=null,$min_grey=null)
ImageOCR::hashByBackground($model=self::MAX_MODEL,$max_grey=null,$min_grey=null)
ImageOCR::removeSpots()
ImageOCR::removeSpotsByConnect()
ImageOCR::standard()
ImageOCR::setImageConnect()
ImageOCR::setImage(Image $image)
ImageOCR::getStandardData()
ImageOCR::setMaxGrey($max_grey)
ImageOCR::setMinGrey($min_grey)
ImageOCR::setStandardWidth($standard_width)
ImageOCR::setStandardHeight($standard_height)

//ImageTool的方法均为静态方法
ImageTool::removeZero($data)
ImageTool::removeZeroColumn($hash_data)
ImageTool::drawBrowser($data)
ImageTool::transposeAndRemoveZero($hash_data)
ImageTool::hashTranspose($hash_data)
ImageTool::img2hash($img)
ImageTool::hash2img($hash_data,$padding=0)
```

CHANGELOG
---------

[](#changelog)

### 0.2 \[2017-4-1\]

[](#02-2017-4-1)

### 0.1 \[2016-10-7\]

[](#01-2016-10-7)

1. 默认模板保存方式由数据库改为文件，保存路径为./db/db.json
2. 使用 composer 安装

###  Health Score

35

—

LowBetter than 80% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity37

Limited adoption so far

Community20

Small or concentrated contributor base

Maturity52

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~413 days

Total

3

Last Release

2676d ago

PHP version history (2 changes)0.1.1PHP &gt;5.4

0.1.2PHP &gt;5.5

### Community

Maintainers

![](https://www.gravatar.com/avatar/c4310fed2a58e61c12f5855cadb7a7f70b165f93006e434cb6f93156911e66cf?d=identicon)[mohuishou](/maintainers/mohuishou)

---

Top Contributors

[![mohuishou](https://avatars.githubusercontent.com/u/15172509?v=4)](https://github.com/mohuishou "mohuishou (35 commits)")

---

Tags

captchaimageimageocrocrphpimageOCR

### Embed Badge

![Health badge](/badges/mohuishou-image-ocr/health.svg)

```
[![Health](https://phpackages.com/badges/mohuishou-image-ocr/health.svg)](https://phpackages.com/packages/mohuishou-image-ocr)
```

###  Alternatives

[intervention/image

PHP Image Processing

14.3k194.3M2.2k](/packages/intervention-image)[league/glide

Wonderfully easy on-demand image manipulation library with an HTTP based API.

2.6k51.2M116](/packages/league-glide)[liip/imagine-bundle

This bundle provides an image manipulation abstraction toolkit for Symfony-based projects.

1.7k38.3M217](/packages/liip-imagine-bundle)[spatie/image

Manipulate images with an expressive API

1.4k54.4M138](/packages/spatie-image)[intervention/image-laravel

Laravel Integration of Intervention Image

1496.5M101](/packages/intervention-image-laravel)[alimranahmed/laraocr

Laravel Optical Character Reader(OCR) package using different OCR engines like Tesseract

17081.9k](/packages/alimranahmed-laraocr)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
