加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
sh_tn.h 30.08 KB
一键复制 编辑 原始数据 按行查看 历史
ruki 提交于 2013-07-02 14:09 . ...
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099
#ifndef SH_TN_H
#define SH_TN_H
#include "prefix.h"
#include "sh_hp.h"
#include "sh_fx.h"
inline image_type sh_tn_thinning_1(image_type const& old_img, int max_n = 200)
{
double k1[9] = { 1, 0, -1
, 1, 2, -1
, 1, 0, -1
};
double k2[9] = { 1, 1, 1
, 0, 2, 0
, -1, -1, -1
};
double k3[9] = { -1, 0, 1
, -1, 2, 1
, -1, 0, 1
};
double k4[9] = { -1, -1, -1
, 0, 2, 0
, 1, 1, 1
};
double k5[9] = { 1, 1, 0
, 1, 2, -1
, 0, -1, -1
};
double k6[9] = { 0, 1, 1
, -1, 2, 1
, -1, -1, 0
};
double k7[9] = { -1, -1, 0
, -1, 2, 1
, 0, 1, 1
};
double k8[9] = { 0, -1, -1
, 1, 2, -1
, 1, 1, 0
};
double k9[9] = { 0, 1, 0
, 1, 2, -1
, 0, -1, -1
};
double k10[9] = { 0, 1, 0
, -1, 2, 1
, -1, -1, 0
};
double k11[9] = { -1, -1, 0
, -1, 2, 1
, 0, 1, 0
};
double k12[9] = { 0, -1, -1
, 1, 2, -1
, 0, 1, 0
};
int kn = 3;
image_type new_img(old_img);
image_type tmp(new_img);
while (max_n--)
{
int matched_n = 0;
bool is_matched_k1, is_matched_k2, is_matched_k3, is_matched_k4;
bool is_matched_k5, is_matched_k6, is_matched_k7, is_matched_k8;
bool is_matched_k9, is_matched_k10, is_matched_k11, is_matched_k12;
for (int py = 0; py < tmp.height() - kn; ++py)
{
for (int px = 0; px < tmp.width() - kn; ++px)
{
is_matched_k1 = sh_hp_match(tmp, new_img, px, py, k1, 3, 3);
is_matched_k2 = sh_hp_match(tmp, new_img, px, py, k2, 3, 3);
is_matched_k3 = sh_hp_match(tmp, new_img, px, py, k3, 3, 3);
is_matched_k4 = sh_hp_match(tmp, new_img, px, py, k4, 3, 3);
is_matched_k5 = sh_hp_match(tmp, new_img, px, py, k5, 3, 3);
is_matched_k6 = sh_hp_match(tmp, new_img, px, py, k6, 3, 3);
is_matched_k7 = sh_hp_match(tmp, new_img, px, py, k7, 3, 3);
is_matched_k8 = sh_hp_match(tmp, new_img, px, py, k8, 3, 3);
is_matched_k9 = sh_hp_match(tmp, new_img, px, py, k9, 3, 3);
is_matched_k10 = sh_hp_match(tmp, new_img, px, py, k10, 3, 3);
is_matched_k11 = sh_hp_match(tmp, new_img, px, py, k11, 3, 3);
is_matched_k12 = sh_hp_match(tmp, new_img, px, py, k12, 3, 3);
if ( is_matched_k1
|| is_matched_k2
|| is_matched_k3
|| is_matched_k4
|| is_matched_k5
|| is_matched_k6
|| is_matched_k7
|| is_matched_k8
|| is_matched_k9
|| is_matched_k10
|| is_matched_k11
|| is_matched_k12
)
{
matched_n++;
}
}
}
if (matched_n <= 0) break;
else tmp = new_img;
}
return new_img;
}
inline image_type sh_tn_thinning_4(image_type const& old_img, int max_n = 200)
{
static e_byte_t erase_table[256] =
{
0,0,1,1,0,0,1,1, 1,1,0,1,1,1,0,1,
1,1,0,0,1,1,1,1, 0,0,0,0,0,0,0,1,
0,0,1,1,0,0,1,1, 1,1,0,1,1,1,0,1,
1,1,0,0,1,1,1,1, 0,0,0,0,0,0,0,1,
1,1,0,0,1,1,0,0, 0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
1,1,0,0,1,1,0,0, 1,1,0,1,1,1,0,1,
0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
0,0,1,1,0,0,1,1, 1,1,0,1,1,1,0,1,
1,1,0,0,1,1,1,1, 0,0,0,0,0,0,0,1,
0,0,1,1,0,0,1,1, 1,1,0,1,1,1,0,1,
1,1,0,0,1,1,1,1, 0,0,0,0,0,0,0,0,
1,1,0,0,1,1,0,0, 0,0,0,0,0,0,0,0,
1,1,0,0,1,1,1,1, 0,0,0,0,0,0,0,0,
1,1,0,0,1,1,0,0, 1,1,0,1,1,1,0,0,
1,1,0,0,1,1,1,0, 1,1,0,0,1,0,0,0
};
image_type new_img(old_img);
bool is_horizontal = true;
while (max_n--)
{
image_type tmp_img(new_img);
int erase_n = 0;
if (is_horizontal)
{
for (int py = 0; py < (int)tmp_img.height() - 3; ++py)
{
for (int px = 0; px < (int)tmp_img.width() - 3; ++px)
{
if (tmp_img.at(px + 1, py + 1).is_black())
{
e_byte_t p1 = !tmp_img.at(px + 0, py + 0).is_black();
e_byte_t p2 = !tmp_img.at(px + 0, py + 1).is_black();
e_byte_t p3 = !tmp_img.at(px + 0, py + 2).is_black();
e_byte_t p4 = !tmp_img.at(px + 1, py + 0).is_black();
e_byte_t p6 = !tmp_img.at(px + 1, py + 2).is_black();
e_byte_t p7 = !tmp_img.at(px + 2, py + 0).is_black();
e_byte_t p8 = !tmp_img.at(px + 2, py + 1).is_black();
e_byte_t p9 = !tmp_img.at(px + 2, py + 2).is_black();
e_byte_t index = p1 + (p2 << 1) + (p3 << 2) + (p4 << 3) + (p6 << 4) + (p7 << 5) + (p8 << 6) + (p9 << 7);
EXTL_ASSERT(index < 256);
if (!(!p4 && !p6) && erase_table[index])
{
new_img.at(px + 1, py + 1).is_black(false);
++erase_n;
}
}
}
}
}
else
{
for (int px = 0; px < (int)tmp_img.width() - 3; ++px)
{
for (int py = 0; py < (int)tmp_img.height() - 3; ++py)
{
if (tmp_img.at(px + 1, py + 1).is_black())
{
e_byte_t p1 = !tmp_img.at(px + 0, py + 0).is_black();
e_byte_t p2 = !tmp_img.at(px + 0, py + 1).is_black();
e_byte_t p3 = !tmp_img.at(px + 0, py + 2).is_black();
e_byte_t p4 = !tmp_img.at(px + 1, py + 0).is_black();
e_byte_t p6 = !tmp_img.at(px + 1, py + 2).is_black();
e_byte_t p7 = !tmp_img.at(px + 2, py + 0).is_black();
e_byte_t p8 = !tmp_img.at(px + 2, py + 1).is_black();
e_byte_t p9 = !tmp_img.at(px + 2, py + 2).is_black();
e_byte_t index = p1 + (p2 << 1) + (p3 << 2) + (p4 << 3) + (p6 << 4) + (p7 << 5) + (p8 << 6) + (p9 << 7);
EXTL_ASSERT(index < 256);
if (!(!p2 && !p8) && erase_table[index])
{
new_img.at(px + 1, py + 1).is_black(false);
++erase_n;
}
}
}
}
}
is_horizontal = !is_horizontal;
if (erase_n == 0) break;
}
QMessageBox::warning(NULL, "", QObject::tr("%1").arg(max_n));
return new_img;
}
inline image_type sh_tn_thinnin_6(image_type const& old_img, int max_n = 200)
{
image_type new_img(old_img);
while (max_n--)
{
bool is_modified = false;
image_type tmp_img(new_img);
for (int py = 0; py < (int)tmp_img.height() - 5; ++py)
{
for (int px = 0; px < (int)tmp_img.width() - 5; ++px)
{
bool con1 = false, con2 = false, con3 = false, con4 = false;
int count = 0;
if (tmp_img.at(px + 2, py + 2).is_black())
{
e_byte_t p01 = tmp_img.at(px + 0, py + 1).is_black();
e_byte_t p02 = tmp_img.at(px + 0, py + 2).is_black();
e_byte_t p03 = tmp_img.at(px + 0, py + 3).is_black();
e_byte_t p10 = tmp_img.at(px + 1, py + 0).is_black();
e_byte_t p11 = tmp_img.at(px + 1, py + 1).is_black();
e_byte_t p12 = tmp_img.at(px + 1, py + 2).is_black();
e_byte_t p13 = tmp_img.at(px + 1, py + 3).is_black();
e_byte_t p20 = tmp_img.at(px + 2, py + 0).is_black();
e_byte_t p21 = tmp_img.at(px + 2, py + 1).is_black();
e_byte_t p22 = tmp_img.at(px + 2, py + 2).is_black();
e_byte_t p23 = tmp_img.at(px + 2, py + 3).is_black();
e_byte_t p30 = tmp_img.at(px + 3, py + 0).is_black();
e_byte_t p31 = tmp_img.at(px + 3, py + 1).is_black();
e_byte_t p32 = tmp_img.at(px + 3, py + 2).is_black();
e_byte_t p33 = tmp_img.at(px + 3, py + 3).is_black();
count = p11 + p12 + p13 + p21 + p23 + p31 + p32 + p33;
if(count >= 2 && count <= 6) con1 = true;
count = 0;
if (p12 == 0 && p11 == 1) ++count;
if (p11 == 0 && p21 == 1) ++count;
if (p21 == 0 && p31 == 1) ++count;
if (p31 == 0 && p32 == 1) ++count;
if (p32 == 0 && p33 == 1) ++count;
if (p33 == 0 && p23 == 1) ++count;
if (p23 == 0 && p13 == 1) ++count;
if (p13 == 0 && p12 == 1) ++count;
if(count == 1) con2 = true;
if (p12 * p21 * p23 == 0) con3 = true;
else
{
count = 0;
if (p02 == 0 && p01 == 1) ++count;
if (p01 == 0 && p11 == 1) ++count;
if (p11 == 0 && p21 == 1) ++count;
if (p21 == 0 && p22 == 1) ++count;
if (p22 == 0 && p23 == 1) ++count;
if (p23 == 0 && p13 == 1) ++count;
if (p13 == 0 && p03 == 1) ++count;
if (p03 == 0 && p02 == 1) ++count;
if(count != 1) con3 = true;
}
if (p12 * p21 * p32 == 0) con4 = true;
else
{
count = 0;
if (p11 == 0 && p10 == 1) ++count;
if (p10 == 0 && p20 == 1) ++count;
if (p20 == 0 && p30 == 1) ++count;
if (p30 == 0 && p31 == 1) ++count;
if (p31 == 0 && p32 == 1) ++count;
if (p32 == 0 && p22 == 1) ++count;
if (p22 == 0 && p12 == 1) ++count;
if (p12 == 0 && p11 == 1) ++count;
if(count != 1) con4 = true;
}
if (con1 && con2 &&
con3 && con4)
{
new_img.at(px + 2, py + 2).is_black(false);
is_modified = true;
}
}
}
}
if (!is_modified) break;
}
QMessageBox::warning(NULL, "", QObject::tr("%1").arg(max_n));
return new_img;
}
inline image_type sh_tn_thinning_pruning(image_type const& old_img, int max_n = 1)
{
image_type new_img(old_img);
while (max_n--)
{
image_type tmp_img(new_img);
for (int py = 0; py < (int)tmp_img.height() - 3; ++py)
{
for (int px = 0; px < (int)tmp_img.width() - 3; ++px)
{
int count = 0;
if (tmp_img.at(px + 1, py + 1).is_black())
{
e_byte_t p00 = tmp_img.at(px + 0, py + 0).is_black();
e_byte_t p01 = tmp_img.at(px + 0, py + 1).is_black();
e_byte_t p02 = tmp_img.at(px + 0, py + 2).is_black();
e_byte_t p10 = tmp_img.at(px + 1, py + 0).is_black();
e_byte_t p12 = tmp_img.at(px + 1, py + 2).is_black();
e_byte_t p20 = tmp_img.at(px + 2, py + 0).is_black();
e_byte_t p21 = tmp_img.at(px + 2, py + 1).is_black();
e_byte_t p22 = tmp_img.at(px + 2, py + 2).is_black();
if (p01 == 0 && p00 == 1) ++count;
if (p00 == 0 && p10 == 1) ++count;
if (p10 == 0 && p20 == 1) ++count;
if (p20 == 0 && p21 == 1) ++count;
if (p21 == 0 && p22 == 1) ++count;
if (p22 == 0 && p12 == 1) ++count;
if (p12 == 0 && p02 == 1) ++count;
if (p02 == 0 && p01 == 1) ++count;
if (count == 1)
{
new_img.at(px + 1, py + 1).is_black(false);
}
}
}
}
}
return new_img;
}
// zhang-suen thinning
inline image_type sh_tn_thinning_zhang_suen(image_type const& old_img, int max_n = 200)
{
int w = (int)old_img.width();
int h = (int)old_img.height();
image_type new_img(old_img);
while (max_n--)
{
bool is_modified = false;
image_type tmp_img(new_img);
for (int py = -1; py < h - 1; ++py)
{
for (int px = -1; px < w - 1; ++px)
{
bool con1 = false, con2 = false, con3 = false, con4 = false;
int count = 0;
if (tmp_img.at(px + 1, py + 1).is_black())
{
e_byte_t p00 = (px < 0 || py < 0) ? 0 : tmp_img.at(px + 0, py + 0).is_black();
e_byte_t p01 = (px < 0) ? 0 : tmp_img.at(px + 0, py + 1).is_black();
e_byte_t p02 = (px < 0 || (py + 2) > (h - 1)) ? 0 : tmp_img.at(px + 0, py + 2).is_black();
e_byte_t p10 = (py < 0) ? 0 : tmp_img.at(px + 1, py + 0).is_black();
e_byte_t p12 = ((py + 2) > (h - 1)) ? 0 : tmp_img.at(px + 1, py + 2).is_black();
e_byte_t p20 = ((px + 2) > (w - 1) || py < 0) ? 0 : tmp_img.at(px + 2, py + 0).is_black();
e_byte_t p21 = ((px + 2) > (w - 1)) ? 0 : tmp_img.at(px + 2, py + 1).is_black();
e_byte_t p22 = ((px + 2) > (w - 1) || (py + 2) > (h - 1)) ? 0 : tmp_img.at(px + 2, py + 2).is_black();
// not inner point & isolated point
count = p00 + p01 + p02 + p10 + p12 + p21 + p22;
if(count >= 2 && count <= 6) con1 = true;
// the point can be removed if it is a boundary point
// and it's connectivity is not increased when removing it
count = 0;
if (p01 == 0 && p00 == 1) ++count;
if (p00 == 0 && p10 == 1) ++count;
if (p10 == 0 && p20 == 1) ++count;
if (p20 == 0 && p21 == 1) ++count;
if (p21 == 0 && p22 == 1) ++count;
if (p22 == 0 && p12 == 1) ++count;
if (p12 == 0 && p02 == 1) ++count;
if (p02 == 0 && p01 == 1) ++count;
if(count == 1) con2 = true;
if (max_n % 2)
{
if (p01 * p10 * p12 == 0) con3 = true;
if (p01 * p10 * p21 == 0) con4 = true;
}
else
{
if (p21 * p10 * p12 == 0) con3 = true;
if (p01 * p12 * p21 == 0) con4 = true;
}
if (con1 && con2 && con3 && con4)
{
new_img.at(px + 1, py + 1).is_black(false);
is_modified = true;
}
}
}
}
if (!is_modified) break;
}
//new_img = sh_tn_thinning_pruning(new_img);
return new_img;
}
// holt
inline image_type sh_tn_holt(image_type const& old_img, int max_n = 2)
{
// delete
double k1[9] = { -1, 1, 0
, 1, 2, 0
, 0, 0, -1
};
double k2[9] = { 0, 1, -1
, 0, 2, 1
, -1, 0, 0
};
double k3[9] = { -1, 0, 0
, 0, 2, 1
, 0, 1, -1
};
double k4[9] = { 0, 0, -1
, 1, 2, 0
, -1, 1, 0
};
int kn = 3;
image_type new_img(old_img);
image_type tmp(new_img);
while (max_n--)
{
int matched_n = 0;
bool is_matched_k1, is_matched_k2, is_matched_k3, is_matched_k4;
for (int py = 0; py < tmp.height() - kn; ++py)
{
for (int px = 0; px < tmp.width() - kn; ++px)
{
is_matched_k1 = sh_hp_match(tmp, new_img, px, py, k1, 3, 3);
is_matched_k2 = sh_hp_match(tmp, new_img, px, py, k2, 3, 3);
is_matched_k3 = sh_hp_match(tmp, new_img, px, py, k3, 3, 3);
is_matched_k4 = sh_hp_match(tmp, new_img, px, py, k4, 3, 3);
if ( is_matched_k1
|| is_matched_k2
|| is_matched_k3
|| is_matched_k4
)
{
matched_n++;
}
}
}
if (matched_n <= 0) break;
else tmp = new_img;
}
return new_img;
}
#if 0
inline image_type sh_tn_acute_angle_emphasis(image_type const& old_img, int max_n = 100)
{
// top
double kt1[25] = { 1, 1, -1, 1, 1
, 1, 1, -1, 1, 1
, 1, 1, 2, 1, 1
, 1, 1, 1, 1, 1
, 1, 1, 1, 1, 1
};
double kt2[25] = { 1, -1, -1, 1, 1
, 1, 1, -1, 1, 1
, 1, 1, 2, 1, 1
, 1, 1, 1, 1, 1
, 1, 1, 1, 1, 1
};
double kt3[25] = { 1, 1, -1, -1, 1
, 1, 1, -1, 1, 1
, 1, 1, 2, 1, 1
, 1, 1, 1, 1, 1
, 1, 1, 1, 1, 1
};
double kt4[25] = { 1, -1, -1, 1, 1
, 1, -1, -1, 1, 1
, 1, 1, 2, 1, 1
, 1, 1, 1, 1, 1
, 1, 1, 1, 1, 1
};
double kt5[25] = { 1, 1, -1, -1, 1
, 1, 1, -1, -1, 1
, 1, 1, 2, 1, 1
, 1, 1, 1, 1, 1
, 1, 1, 1, 1, 1
};
double kt6[25] = { 1, -1, -1, -1, 1
, 1, 1, -1, 1, 1
, 1, 1, 2, 1, 1
, 1, 1, 1, 1, 1
, 1, 1, 1, 1, 1
};
// bottom
double kb1[25] = { 1, 1, 1, 1, 1
, 1, 1, 1, 1, 1
, 1, 1, 2, 1, 1
, 1, 1, -1, 1, 1
, 1, 1, -1, 1, 1
};
double kb2[25] = { 1, 1, 1, 1, 1
, 1, 1, 1, 1, 1
, 1, 1, 2, 1, 1
, 1, 1, -1, 1, 1
, 1, 1, -1, -1, 1
};
double kb3[25] = { 1, 1, 1, 1, 1
, 1, 1, 1, 1, 1
, 1, 1, 2, 1, 1
, 1, 1, -1, 1, 1
, 1, -1, -1, 1, 1
};
double kb4[25] = { 1, 1, 1, 1, 1
, 1, 1, 1, 1, 1
, 1, 1, 2, 1, 1
, 1, 1, -1, -1, 1
, 1, 1, -1, -1, 1
};
double kb5[25] = { 1, 1, 1, 1, 1
, 1, 1, 1, 1, 1
, 1, 1, 2, 1, 1
, 1, -1, -1, 1, 1
, 1, -1, -1, 1, 1
};
double kb6[25] = { 1, 1, 1, 1, 1
, 1, 1, 1, 1, 1
, 1, 1, 2, 1, 1
, 1, 1, -1, 1, 1
, 1, -1, -1, -1, 1
};
// left
double kf1[25] = { 1, 1, 1, 1, 1
, 1, 1, 1, 1, 1
, -1, -1, 2, 1, 1
, 1, 1, 1, 1, 1
, 1, 1, 1, 1, 1
};
double kf2[25] = { 1, 1, 1, 1, 1
, 1, 1, 1, 1, 1
, -1,- 1, 2, 1, 1
, -1, 1, 1, 1, 1
, 1, 1, 1, 1, 1
};
double kf3[25] = { 1, 1, 1, 1, 1
, -1, 1, 1, 1, 1
, -1,- 1, 2, 1, 1
, 1, 1, 1, 1, 1
, 1, 1, 1, 1, 1
};
double kf4[25] = { 1, 1, 1, 1, 1
, -1, -1, 1, 1, 1
, -1, -1, 2, 1, 1
, 1, 1, 1, 1, 1
, 1, 1, 1, 1, 1
};
double kf5[25] = { 1, 1, 1, 1, 1
, 1, 1, 1, 1, 1
, -1, -1, 2, 1, 1
, -1, -1, 1, 1, 1
, 1, 1, 1, 1, 1
};
double kf6[25] = { 1, 1, 1, 1, 1
, -1, 1, 1, 1, 1
, -1, -1, 2, 1, 1
, -1, 1, 1, 1, 1
, 1, 1, 1, 1, 1
};
// right
double kr1[25] = { 1, 1, 1, 1, 1
, 1, 1, 1, 1, 1
, 1, 1, 2, -1, -1
, 1, 1, 1, 1, 1
, 1, 1, 1, 1, 1
};
double kr2[25] = { 1, 1, 1, 1, 1
, 1, 1, 1, 1, -1
, 1, 1, 2, -1, -1
, 1, 1, 1, 1, 1
, 1, 1, 1, 1, 1
};
double kr3[25] = { 1, 1, 1, 1, 1
, 1, 1, 1, 1, 1
, 1, 1, 2, -1, -1
, 1, 1, 1, 1, -1
, 1, 1, 1, 1, 1
};
double kr4[25] = { 1, 1, 1, 1, 1
, 1, 1, 1, -1, -1
, 1, 1, 2, -1, -1
, 1, 1, 1, 1, 1
, 1, 1, 1, 1, 1
};
double kr5[25] = { 1, 1, 1, 1, 1
, 1, 1, 1, 1, 1
, 1, 1, 2, -1, -1
, 1, 1, 1, -1, -1
, 1, 1, 1, 1, 1
};
double kr6[25] = { 1, 1, 1, 1, 1
, 1, 1, 1, 1, -1
, 1, 1, 2, -1, -1
, 1, 1, 1, 1, -1
, 1, 1, 1, 1, 1
};
// left-top
double kft1[25] = { -1, 0, 1, 1, 1
, 2, -1, 2, 1, 1
, 1, 2, 2, 1, 1
, 1, 1, 1, 1, 1
, 1, 1, 1, 1, 1
};
double kft2[25] = { -1, -1, 0, 1, 1
, -1, -1, -1, 1, 1
, 0, -1, 2, 1, 1
, 1, 1, 1, 1, 1
, 1, 1, 1, 1, 1
};
double kft3[25] = { -1, -1, -1, 0, 1
, -1, -1, -1, 1, 1
, -1, -1, 2, 1, 1
, 0, 1, 1, 1, 1
, 1, 1, 1, 1, 1
};
double kft4[25] = { -1, 1, 1, 1, 1
, -1, -1, 1, 1, 1
, 0, -1, 2, 1, 1
, 1, 1, 1, 1, 1
, 1, 1, 1, 1, 1
};
double kft5[25] = { -1, -1, 0, 1, 1
, 1, -1, -1, 1, 1
, 1, 1, 2, 1, 1
, 1, 1, 1, 1, 1
, 1, 1, 1, 1, 1
};
// right-top
double krt1[25] = { 1, 1, 1, 2, -1
, 1, 1, 2, -1, 0
, 1, 1, 2, 2, 1
, 1, 1, 1, 1, 1
, 1, 1, 1, 1, 1
};
double krt2[25] = { 1, 1, 0, -1, -1
, 1, 1, -1, -1, -1
, 1, 1, 2, -1, 0
, 1, 1, 1, 1, 1
, 1, 1, 1, 1, 1
};
double krt3[25] = { 1, 0, -1, -1, -1
, 1, 1, -1, -1, -1
, 1, 1, 2, -1, -1
, 1, 1, 1, 1, 0
, 1, 1, 1, 1, 1
};
double krt4[25] = { 1, 1, 0, -1, -1
, 1, 1, -1, -1, 1
, 1, 1, 2, 1, 1
, 1, 1, 1, 1, 1
, 1, 1, 1, 1, 1
};
double krt5[25] = { 1, 1, 1, 1, -1
, 1, 1, 1, -1, -1
, 1, 1, 2, -1, 0
, 1, 1, 1, 1, 1
, 1, 1, 1, 1, 1
};
// left-bottom
double kfb1[25] = { 1, 1, 1, 1, 1
, 1, 1, 1, 1, 1
, 1, 2, 2, 1, 1
, 0, -1, 2, 1, 1
, -1, 2, 1, 1, 1
};
double kfb2[25] = { 1, 1, 1, 1, 1
, 1, 1, 1, 1, 1
, 0, -1, 2, 1, 1
, -1, -1, -1, 1, 1
, -1, -1, 0, 1, 1
};
double kfb3[25] = { 1, 1, 1, 1, 1
, 0, 1, 1, 1, 1
, -1, -1, 2, 1, 1
, -1, -1, -1, 1, 1
, -1, -1, -1, 0, 1
};
double kfb4[25] = { 1, 1, 1, 1, 1
, 1, 1, 1, 1, 1
, 1, 1, 2, 1, 1
, 1, -1, -1, 1, 1
, -1, -1, 0, 1, 1
};
double kfb5[25] = { 1, 1, 1, 1, 1
, 1, 1, 1, 1, 1
, 0, -1, 2, 1, 1
, -1, -1, 1, 1, 1
, -1, 1, 1, 1, 1
};
// right-bottom
double krb1[25] = { 1, 1, 1, 1, 1
, 1, 1, 1, 1, 1
, 1, 1, 2, 2, 1
, 1, 1, 2, -1, 2
, 1, 1, 1, 0, -1
};
double krb2[25] = { 1, 1, 1, 1, 1
, 1, 1, 1, 1, 1
, 1, 1, 2, -1, 0
, 1, 1, -1, -1, -1
, 1, 1, 0, -1, -1
};
double krb3[25] = { 1, 0, 1, 1, 1
, 1, 1, 1, 1, 0
, 1, 1, 2, -1, -1
, 1, 1, -1, -1, -1
, 1, 0, -1, -1, -1
};
double krb4[25] = { 1, 1, 1, 1, 1
, 1, 1, 1, 1, 1
, 1, 1, 2, 1, 1
, 1, 1, -1, -1, 1
, 1, 1, 0, -1, -1
};
double krb5[25] = { 1, 1, 1, 1, 1
, 1, 1, 1, 1, 1
, 1, 1, 2, -1, 0
, 1, 1, 1, -1, -1
, 1, 1, 1, 1, -1
};
//test
double kts1[25] = { 1, 1, 0, -1, -1
, 1, 1, -1, -1, -1
, 1, -3, -1, -1, 0
, 1, 2, -3, 1, 1
, 1, 1, 1, 1, 1
};
double kts2[25] = { 1, 1, 1, 1, 0
, 1, 1, -3, -1, -1
, 1, 2, -1, -1, -1
, 1, 1, -3, -1, -1
, 1, 1, 1, 1, 0
};
double kts3[25] = { 1, 1, 1, 1, 1
, 1, 2, -3, 1, 1
, 1, -3, -1, -1, 0
, 1, 1, -1, -1, -1
, 1, 1, 0, -1, -1
};
double kts4[25] = { 1, 1, 1, 1, 1
, 1, 1, 2, 1, 1
, 1, -3, -1, -3, 1
, 1, -1, -1, -1, 1
, 0, -1, -1, -1, 0
};
double kts5[25] = { 1, 1, 1, 1, 1
, 1, 1, -3, 2, 1
, 0, -1, -1, -3, 1
, -1, -1, -1, 1, -1
, -1, -1, 0, 1, 1
};
double kts6[25] = { 0, 1, 1, 1, 1
, -1, -1, -3, 1, 1
, -1, -1, -1, 2, 1
, -1, -1, -3, 1, 1
, 0, 1, 1, 1, 1
};
double kts7[25] = { -1, -1, 0, 1, 1
, -1, -1, -1, 1, 1
, 0, -1, -1, -3, 1
, 1, 1, -3, 2, 1
, 1, 1, 1, 1, 1
};
double kts8[25] = { 0, -1, -1, -1, 0
, 1, -1, -1, -1, 1
, 1, -3, -1, -3, 1
, 1, 1, 2, 1, 1
, 1, 1, 1, 1, 1
};
double kts9[25] = { 1, -1, -1, -1, -1
, 1, -1, -1, -1, -1
, 1, -1, -1, -1, -1
, 1, 2, -3, -3, -3
, 1, 1, 1, 0, 0
};
double kts10[25] = { 1, 1, 1, 0, 0
, 1, 2, -3, -3, -3
, 1, -1, -1, -1, -1
, 1, -1, -1, -1, -1
, 1, -1, -1, -1, -1
};
int px = 0, py = 0;
image_type new_img(old_img);
image_type tmp_img(new_img);
while (max_n--)
{
int matched_n = 0;
bool is_matched_kt1, is_matched_kt2, is_matched_kt3, is_matched_kt4, is_matched_kt5, is_matched_kt6;
bool is_matched_kb1, is_matched_kb2, is_matched_kb3, is_matched_kb4, is_matched_kb5, is_matched_kb6;
bool is_matched_kf1, is_matched_kf2, is_matched_kf3, is_matched_kf4, is_matched_kf5, is_matched_kf6;
bool is_matched_kr1, is_matched_kr2, is_matched_kr3, is_matched_kr4, is_matched_kr5, is_matched_kr6;
bool is_matched_kft1, is_matched_kft2, is_matched_kft3, is_matched_kft4, is_matched_kft5;
bool is_matched_krt1, is_matched_krt2, is_matched_krt3, is_matched_krt4, is_matched_krt5;
bool is_matched_kfb1, is_matched_kfb2, is_matched_kfb3, is_matched_kfb4, is_matched_kfb5;
bool is_matched_krb1, is_matched_krb2, is_matched_krb3, is_matched_krb4, is_matched_krb5;
bool is_matched_kts1, is_matched_kts2, is_matched_kts3, is_matched_kts4;
bool is_matched_kts5, is_matched_kts6, is_matched_kts7, is_matched_kts8;
bool is_matched_kts9, is_matched_kts10, is_matched_kts11, is_matched_kts12;
for (py = 0; py < tmp_img.height() - 5; ++py)
{
for (px = 0; px < tmp_img.width() - 5; ++px)
{
/*is_matched_kt1 = sh_hp_match(tmp_img, new_img, px, py, kt1, 5, 5);
is_matched_kt2 = sh_hp_match(tmp_img, new_img, px, py, kt2, 5, 5);
is_matched_kt3 = sh_hp_match(tmp_img, new_img, px, py, kt3, 5, 5);
is_matched_kt4 = sh_hp_match(tmp_img, new_img, px, py, kt4, 5, 5);
is_matched_kt5 = sh_hp_match(tmp_img, new_img, px, py, kt5, 5, 5);
is_matched_kt6 = sh_hp_match(tmp_img, new_img, px, py, kt6, 5, 5);
is_matched_kb1 = sh_hp_match(tmp_img, new_img, px, py, kb1, 5, 5);
is_matched_kb2 = sh_hp_match(tmp_img, new_img, px, py, kb2, 5, 5);
is_matched_kb3 = sh_hp_match(tmp_img, new_img, px, py, kb3, 5, 5);
is_matched_kb4 = sh_hp_match(tmp_img, new_img, px, py, kb4, 5, 5);
is_matched_kb5 = sh_hp_match(tmp_img, new_img, px, py, kb5, 5, 5);
is_matched_kb6 = sh_hp_match(tmp_img, new_img, px, py, kb6, 5, 5);
is_matched_kf1 = sh_hp_match(tmp_img, new_img, px, py, kf1, 5, 5);
is_matched_kf2 = sh_hp_match(tmp_img, new_img, px, py, kf2, 5, 5);
is_matched_kf3 = sh_hp_match(tmp_img, new_img, px, py, kf3, 5, 5);
is_matched_kf4 = sh_hp_match(tmp_img, new_img, px, py, kf4, 5, 5);
is_matched_kf5 = sh_hp_match(tmp_img, new_img, px, py, kf5, 5, 5);
is_matched_kf6 = sh_hp_match(tmp_img, new_img, px, py, kf6, 5, 5);
is_matched_kr1 = sh_hp_match(tmp_img, new_img, px, py, kr1, 5, 5);
is_matched_kr2 = sh_hp_match(tmp_img, new_img, px, py, kr2, 5, 5);
is_matched_kr3 = sh_hp_match(tmp_img, new_img, px, py, kr3, 5, 5);
is_matched_kr4 = sh_hp_match(tmp_img, new_img, px, py, kr4, 5, 5);
is_matched_kr5 = sh_hp_match(tmp_img, new_img, px, py, kr5, 5, 5);
is_matched_kr6 = sh_hp_match(tmp_img, new_img, px, py, kr6, 5, 5);*/
/*is_matched_kft1 = sh_hp_match(tmp_img, new_img, px, py, kft1, 5, 5);
is_matched_kft2 = sh_hp_match(tmp_img, new_img, px, py, kft2, 5, 5);
is_matched_kft3 = sh_hp_match(tmp_img, new_img, px, py, kft3, 5, 5);
is_matched_kft4 = sh_hp_match(tmp_img, new_img, px, py, kft4, 5, 5);
is_matched_kft5 = sh_hp_match(tmp_img, new_img, px, py, kft5, 5, 5);*/
/*is_matched_krt1 = sh_hp_match(tmp_img, new_img, px, py, krt1, 5, 5);
is_matched_krt2 = sh_hp_match(tmp_img, new_img, px, py, krt2, 5, 5);
is_matched_krt3 = sh_hp_match(tmp_img, new_img, px, py, krt3, 5, 5);
is_matched_krt4 = sh_hp_match(tmp_img, new_img, px, py, krt4, 5, 5);
is_matched_krt5 = sh_hp_match(tmp_img, new_img, px, py, krt5, 5, 5);*/
/*is_matched_kfb1 = sh_hp_match(tmp_img, new_img, px, py, kfb1, 5, 5);
is_matched_kfb2 = sh_hp_match(tmp_img, new_img, px, py, kfb2, 5, 5);
is_matched_kfb3 = sh_hp_match(tmp_img, new_img, px, py, kfb3, 5, 5);
is_matched_kfb4 = sh_hp_match(tmp_img, new_img, px, py, kfb4, 5, 5);
is_matched_kfb5 = sh_hp_match(tmp_img, new_img, px, py, kfb5, 5, 5);*/
/*is_matched_krb1 = sh_hp_match(tmp_img, new_img, px, py, krb1, 5, 5);
is_matched_krb2 = sh_hp_match(tmp_img, new_img, px, py, krb2, 5, 5);
is_matched_krb3 = sh_hp_match(tmp_img, new_img, px, py, krb3, 5, 5);
is_matched_krb4 = sh_hp_match(tmp_img, new_img, px, py, krb4, 5, 5);
is_matched_krb5 = sh_hp_match(tmp_img, new_img, px, py, krb5, 5, 5);*/
is_matched_kts1 = sh_hp_match(tmp_img, new_img, px, py, kts1, 5, 5);
is_matched_kts2 = sh_hp_match(tmp_img, new_img, px, py, kts2, 5, 5);
is_matched_kts3 = sh_hp_match(tmp_img, new_img, px, py, kts3, 5, 5);
is_matched_kts4 = sh_hp_match(tmp_img, new_img, px, py, kts4, 5, 5);
is_matched_kts5 = sh_hp_match(tmp_img, new_img, px, py, kts5, 5, 5);
is_matched_kts6 = sh_hp_match(tmp_img, new_img, px, py, kts6, 5, 5);
is_matched_kts7 = sh_hp_match(tmp_img, new_img, px, py, kts7, 5, 5);
is_matched_kts8 = sh_hp_match(tmp_img, new_img, px, py, kts8, 5, 5);
//is_matched_kts9 = sh_hp_match(tmp_img, new_img, px, py, kts9, 5, 5);
//is_matched_kts10 = sh_hp_match(tmp_img, new_img, px, py, kts10, 5, 5);
//is_matched_kts2 = sh_hp_match(tmp_img, new_img, px, py, kts2, 7, 7);
if ( /*is_matched_kt1
|| is_matched_kt2
|| is_matched_kt3
|| is_matched_kt4
|| is_matched_kt5
|| is_matched_kt6
|| is_matched_kb1
|| is_matched_kb2
|| is_matched_kb3
|| is_matched_kb4
|| is_matched_kb5
|| is_matched_kb6
|| is_matched_kf1
|| is_matched_kf2
|| is_matched_kf3
|| is_matched_kf4
|| is_matched_kf5
|| is_matched_kf6
|| is_matched_kr1
|| is_matched_kr2
|| is_matched_kr3
|| is_matched_kr4
|| is_matched_kr5
|| is_matched_kr6
|| is_matched_kft1
|| is_matched_kft2
|| is_matched_kft3
|| is_matched_kft4
|| is_matched_kft5
|| is_matched_krt1
|| is_matched_krt2
|| is_matched_krt3
|| is_matched_krt4
|| is_matched_krt5
|| is_matched_kfb1
|| is_matched_kfb2
|| is_matched_kfb3
|| is_matched_kfb4
|| is_matched_kfb5
|| is_matched_krb1
|| is_matched_krb2
|| is_matched_krb3
|| is_matched_krb4
|| is_matched_krb5
||*/ is_matched_kts1
|| is_matched_kts2
|| is_matched_kts3
|| is_matched_kts4
|| is_matched_kts5
|| is_matched_kts6
|| is_matched_kts7
|| is_matched_kts8
//|| is_matched_kts9
//|| is_matched_kts10
)
{
matched_n++;
}
}
}
if (matched_n > 0) tmp_img = new_img;
//else break;
/*}
max_n = 100;
while (max_n--)
{*/
bool is_modified = false;
for (py = 0; py < (int)tmp_img.height() - 3; ++py)
{
for (px = 0; px < (int)tmp_img.width() - 3; ++px)
{
bool con1 = false, con2 = false, con3 = false, con4 = false;
int count = 0;
if (tmp_img.at(px + 1, py + 1).is_black())
{
e_byte_t p00 = tmp_img.at(px + 0, py + 0).is_black();
e_byte_t p01 = tmp_img.at(px + 0, py + 1).is_black();
e_byte_t p02 = tmp_img.at(px + 0, py + 2).is_black();
e_byte_t p10 = tmp_img.at(px + 1, py + 0).is_black();
e_byte_t p12 = tmp_img.at(px + 1, py + 2).is_black();
e_byte_t p20 = tmp_img.at(px + 2, py + 0).is_black();
e_byte_t p21 = tmp_img.at(px + 2, py + 1).is_black();
e_byte_t p22 = tmp_img.at(px + 2, py + 2).is_black();
// not inner point & isolated point
count = p00 + p01 + p02 + p10 + p12 + p21 + p22;
if(count >= 2 && count <= 6) con1 = true;
// the point can be removed if it is a boundary point
// and it's connectivity is not increased when removing it
count = 0;
if (p01 == 0 && p00 == 1) ++count;
if (p00 == 0 && p10 == 1) ++count;
if (p10 == 0 && p20 == 1) ++count;
if (p20 == 0 && p21 == 1) ++count;
if (p21 == 0 && p22 == 1) ++count;
if (p22 == 0 && p12 == 1) ++count;
if (p12 == 0 && p02 == 1) ++count;
if (p02 == 0 && p01 == 1) ++count;
if(count == 1) con2 = true;
//
if (max_n % 2)
{
if (p01 * p10 * p12 == 0) con3 = true;
if (p01 * p10 * p21 == 0) con4 = true;
}
else
{
if (p21 * p10 * p12 == 0) con3 = true;
if (p01 * p12 * p21 == 0) con4 = true;
}
if (con1 && con2 && con3 && con4)
{
new_img.at(px + 1, py + 1).is_black(false);
is_modified = true;
}
}
}
}
if (!is_modified) break;
tmp_img = new_img;
}
return new_img;
}
#endif
// thinning
inline image_type sh_tn_thinning(image_type const& old_img, int max_n = 200)
{
image_type new_img(old_img);
//new_img = sh_fx_fixes(new_img);
//new_img = sh_tn_acute_angle_emphasis(new_img);
new_img = sh_tn_thinning_zhang_suen(new_img);
//new_img = sh_tn_holt(new_img);
return new_img;
}
#endif // SH_TN_H
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化