- 論壇徽章:
- 0
|
程序說(shuō)明
需要使用了兩個(gè)庫(kù) 1. Image::Magick 用來(lái)進(jìn)行圖片處理 2. imgseek 進(jìn)行圖片比較 求出相似度
程序因?yàn)槭褂玫?imgseek 所以只能在*nix下運(yùn)行
程序設(shè)計(jì)時(shí) 分兩個(gè)部分 1.建里特征圖片庫(kù) 2. 用隨即圖片與特征圖片庫(kù)進(jìn)行比較 求出相似度最高的值
這個(gè)程序僅僅是為了供大家研究參考 # 為了不惹麻煩 把測(cè)試網(wǎng)站刪除了
一. 建立特征圖片庫(kù)
1. 下載 包含 0到9 共10個(gè)數(shù)字的圖片
我這里是 手工下載了10張第一個(gè)數(shù)字不同的圖片
2. 對(duì)圖片進(jìn)行處理
轉(zhuǎn)換成灰度圖 -> 2值化(我這里設(shè)定的闕值是20300)-> 把第一個(gè)數(shù)字切割下來(lái)
-
- use Image::Magick;
- my $image = new Image::Magick;
- for my $num qw(00 01 02 03 04 05 06 07 08 09){
- my $file = './image/'.$num.'.bmp';
- $image->Read($file);
- }
- $image->Quantize(colorspace=>'gray'); #灰度圖
- $image->Threshold(threshold=>'20300',channel=>'All'); # 2值化 闕值需要自己手工調(diào)整
- $image->Crop(width=>18, height=>25, x=>0, y=>0); #切割圖片
- my $out = './image/I.bmp';
- $image->Write($out);
- $image = undef;
- exit;
復(fù)制代碼
3. 提取出僅含有數(shù)字的區(qū)域 (即去掉四周的空白)
-
- use List::Util qw(max min);
- use Image::Magick;
- for my $num (0..9){
- my $image = new Image::Magick;
- my $file = './image/I-'.$num.'.bmp';
- $image->Read($file);
- my ($w,$h)=$image -> Get('width','height');
- my (@px_x,@px_y);
- for my $x(0..$w){
- for my $y (0..$h){
- my $px = $image -> GetPixel(x=>$x,y=>$y);
- if($px == 0){
- push @px_x,$x;
- push @px_y,$y;
- }
- }
- }
- my ($xmax,$xmin,$ymax,$ymin) = (max(@px_x),min(@px_x),max(@px_y),min(@px_y));
- my $xw = $xmax-$xmin + 2;
- my $yh = $ymax-$ymin + 2;
- $xmin -= 1;
- $ymin -= 1;
- $image->Crop(width=>$xw, height=>$yh, x=>$xmin, y=>$ymin);
- #$image->Magnify;
- my $out = './flag/II-'.$num.'.bmp';
- $image->Write($out);
- $image = undef;
- }
- exit;
復(fù)制代碼
4.存入到 圖片庫(kù)里
-
- use Imager;
- use Image::Seek qw(loaddb add_image query_id savedb);
- loaddb("imgdata.db");
- for my $num (0..9){
- my $img = Imager->new();
- my $file = './flag/II-'.$num.'.bmp';
- $img->open(file => $file);
- add_image($img,$num);
- savedb("imgdata.db");
-
- $img = undef;
- }
-
復(fù)制代碼
二 隨即認(rèn)證碼 圖片識(shí)別
1.隨機(jī)圖片下載加工
-
- use List::Util qw(max min);
- use Image::Magick;
- use LWP::Simple;
- my $url = '****'; # 為了不惹麻煩 把測(cè)試網(wǎng)站刪除了
- my $file = './cut/tmp.bmp';
- getstore($url, $file);
- for my $num (0..3){
- my $image = new Image::Magick;
- $image->Read($file);
- my ($w, $h,) = $image->Get('width', 'height');
- $w /= 4;
- my $xxx = $w * $num;
-
- $image->Quantize(colorspace=>'gray');
- $image->Threshold(threshold=>'20300',channel=>'All');
- $image->Crop(width=>$w, height=>$h, x=>$xxx, y=>0);
-
- $image->Write('./cut/t.bmp');
- $image = undef;
-
- my $img = new Image::Magick;
- $img->Read('./cut/t.bmp');
- my (@px_x,@px_y);
- for my $x(0..$w){
- for my $y (0..$h){
- my $px = $img -> GetPixel(x=>$x,y=>$y);
- if($px == 0){
- push @px_x,$x;
- push @px_y,$y;
- }
- }
- }
- my ($xmax,$xmin,$ymax,$ymin) = (max(@px_x),min(@px_x),max(@px_y),min(@px_y));
- my $xw = $xmax-$xmin + 2;
- my $yh = $ymax-$ymin + 2;
- $xmin -= 1;
- $ymin -= 1;
- $img->Crop(width=>$xw, height=>$yh, x=>$xmin, y=>$ymin);
-
-
- my $out = './cut/'.$num.'.bmp';
- $img->Write($out);
- $img = undef;
- }
復(fù)制代碼
2. 識(shí)別
-
- use Imager;
- use Image::Seek qw(loaddb add_image query_id savedb remove_id);
- loaddb("imgdata.db");
- for my $num (0..3){
- my $img = Imager->new();
- my $file = './cut/'.$num.'.bmp';
- $img->open(file => $file);
- add_image($img,10);
- savedb("imgdata.db");
- my @results = query_id(10); # What looks like this photo?
- remove_id(10); # Just remove id from database.
- print $results[1]->[0];
- }
復(fù)制代碼
[ 本帖最后由 hitsubunnu 于 2009-2-3 17:27 編輯 ] |
|