寻找连通线,参考八后算法递归,可用于验证码去除连续干扰线

寻找连通线,参考八后算法递归,可用于验证码去除连续干扰线

https://github.com/isee15/captcha-ocr/blob/master/src/cn/z/NoiseLine.java

package cn.z;

import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

import cn.z.util.CommonUtil;

public class NoiseLine {
private final int M = 130;
private final int N = 36;
int a[][] = new int[this.M][this.N];
int[] b = new int[this.M];
int threshold = 20;
int offset = 11;

public NoiseLine() {
int x = 0;
int y = 0;
try {
final BufferedImage img = ImageIO.read(new File("img/noise.jpeg"));
this.renderImg = img;
final int width = img.getWidth();
final int height = img.getHeight();
System.out.println(width + ":" + height);
for (; x < height; ++x) {
for (y = 0; y < width; ++y) {
this.a[x][y] = CommonUtil.isBlack(img.getRGB(y, x), 700);
//System.out.print(this.a[x][y] + " ");
}
//System.out.println(" ");
}
} catch (final IOException e) {
e.printStackTrace();
}
}

private BufferedImage renderImg;

public void genLine(int n) {
if (n < this.offset) {
this.b[n] = -1;
this.genLine(n + 1);
}
if (n == this.M) {
for (int i = 0; i < this.M; i++) {
System.out.print(this.b[i] + " ");

}
System.out.println("");
}
if (n == this.offset) {
for (int j = 0; j < this.N; j++) {
if (this.a[this.offset][j] == 1) {
this.b[this.offset] = j;
this.genLine(n + 1);
}
}
}
if (n > 0 && n < this.M) {
int hasMore = 0;
if (this.b[n - 1] > 0 && this.b[n - 1] < this.N && this.a[n][this.b[n - 1]] == 1) {
this.b[n] = this.b[n - 1];
hasMore = 1;
this.genLine(n + 1);
} else {
if (this.b[n - 1] > 0 && this.a[n][this.b[n - 1] - 1] == 1) {
this.b[n] = this.b[n - 1] - 1;
hasMore = 1;
this.genLine(n + 1);
}
if (this.b[n - 1] < this.N - 1 && this.a[n][this.b[n - 1] + 1] == 1) {
this.b[n] = this.b[n - 1] + 1;
hasMore = 1;
this.genLine(n + 1);
}
}
if (n - this.offset > this.threshold && hasMore == 0) {
for (int i = 0; i < n; i++) {
if (this.b[i] > 0) {
this.renderImg.setRGB(this.b[i], i, Color.RED.getRGB());
}
}
}
}

}

public void saveImg() {
try {
ImageIO.write(this.renderImg, "JPG", new File("img/noiseRender.jpg"));
} catch (final IOException e) {
e.printStackTrace();
}
}

public static void main(String[] args) {
final NoiseLine line = new NoiseLine();
line.genLine(0);
line.saveImg();
System.out.println("处理后图片在img/noiseRender.jpg");
}

}

时间: 2024-08-11 18:05:24

寻找连通线,参考八后算法递归,可用于验证码去除连续干扰线的相关文章

浅析去除验证码图片中的干扰线、噪点(java)

(原创文章,转载请加转载地址)  版权声明:转载时请以超链接形式标明文章原始出处和作者信息及本声明 图片处理中最为重要的是对目标图片的特征分析,通过这些特征(点)设计图片预处理方法.针对带有干扰线.噪点特征的验证码图片,自然有它自己的处理方法,下面是个人的一点愚见,图像处理常用的是MATLAB和c++来做的,因为它们都有强大的图像处理的库,在Java中关于图像处理的就相对来说少一些. 图片像素由24位二进制的机器码表示,可以表示为ARGB,这里和色彩的RGB并不冲突,这里的A表示的是透明度,网上

[记录]Python爬虫过程中遇到的简单带干扰线验证码处理方法

前言: 这些天,人力资源部有个需求:需要定期检查短信猫平台账号余额,于是乎,我向短信平台提交这个查询需求,对方给我答复是没办法.如此一来,只能看看能否通过用爬虫的方法去爬取后台数据了. 一.观察目标站点 使用开发者模式看了下目标站点,登陆是非常简单的三个选项:用户名,密码,验证码.验证码是由4位纯数字组成,每次通过get请求随机验证码的方法来刷新验证码.好了,观察到这,大致思路有了:get请求验证码图片,保存到本地进行识别,识别到的验证码加上用户名密码提交,去查询短信平台账号余额.到这里又有一个

经典回溯算法——八后问题

/************************************************************************/ /* 八后问题                                                             */ /************************************************************************/ #include <stdio.h> int count

OpenCV:二值图像连通区域分析与标记算法实现

编译环境: 操作系统:Win8.1  64位 IDE平台:Visual Studio 2013 Ultimate OpenCV:2.4.8 一.连通域 在图像中,最小的单位是像素,每个像素周围有8个邻接像素,常见的邻接关系有2种:4邻接与8邻接.4邻接一共4个点,即上下左右,如下左图所示.8邻接的点一共有8个,包括了对角线位置的点,如下右图所示.         如果像素点A与B邻接,我们称A与B连通,于是我们不加证明的有如下的结论: 如果A与B连通,B与C连通,则A与C连通. 在视觉上看来,彼

算法——递归篇——树叶下落问题

1,什么是类的拷贝控制 当我们定义一个类的时候,为了让我们定义的类类型像内置类型(char,int,double等)一样好用,我们通常需要考下面几件事: Q1:用这个类的对象去初始化另一个同类型的对象. Q2:将这个类的对象赋值给另一个同类型的对象. Q3:让这个类的对象有生命周期,比如局部对象在代码部结束的时候,需要销毁这个对象. 因此C++就定义了5种拷贝控制操作,其中2个移动操作是C++11标准新加入的特性: 拷贝构造函数(copy constructor) 移动构造函数(move con

ACM:递归与分治,最大连续和,O(n3), O(n2), O(nlogn), O(n) 算法。

题目,求一个连续的数组,最大连续和. (一)O(n3)算法: 利用穷举法的思想,这种方法的效率最差. 代码如下: #include <iostream> #include <cstdlib> #include <ctime> #include <cmath> using namespace std; const int MAXN = 1000; int A[MAXN], n; int maxsum(int *A, int n) { int beat = A[

八皇后问题——递归+回溯法

八皇后问题,是一个古老而著名的问题,是回溯算法的典型案例.该问题是国际西洋棋棋手马克斯·贝瑟尔于1848年提出:在8X8格的国际象棋上摆放八个皇后,使其不能互相攻击,即任意两个皇后都不能处于同一行.同一列或同一斜线上,问有多少种摆法. 高斯认为有76种方案.1854年在柏林的象棋杂志上不同的作者发表了40种不同的解,后来有人用图论的方法解出92种结果. 求解过程: 采用遍历的办法,就是采用将每种情况都验证的办法最终找出问题的解,但是蛮力遍历的话,需要遍历的数据量太大,计算时间花费太大,所以在遍历

图像全参考客观评价算法比较

Lin Zhang等人在论文<A COMPREHENSIVEEVALUATION OF FULL REFERENCE IMAGE QUALITY ASSESSMENT ALGORITHMS>中,比较了几种全参考图像质量评价算法,在此记录一下他们的结果. 下表所示是他们所用的图像库,包含了:TID2008database,CSIQ database,LIVEdatabase,IVCdatabase,Toyama-MICTdatabase,Cornell A57 database,以及 Wirel

PHP生成带有干扰线的验证码,干扰点、字符倾斜

PHP生成验证码的类代码,本验证码类支持生成干扰点.干扰线等干扰像素,还可以使字符倾斜.在类中你可以定义验证码宽度.高度.长度.倾斜角度等参数,后附有用法: <?php class class_authcode{ public $authcode = ''; //验证码 private $width = ''; //验证码图片宽 private $height = ''; //验证码图片高 private $len = ''; //验证码长度 private $tilt = array(-30,