feat(image-tools): 智能消除——无痕删除图中文字/水印(inpainting)

- inpaintRect(): 笔画像素检测(与环带中位色差异>42)+ 1px 抗锯齿膨胀
  + Gauss-Seidel 交替方向扩散重建(自适应迭代次数),渐变/纹理背景自然补全
- OCR 路径:点框「🧹 消除文字」(替代原纯色覆盖的只擦除)
- 选框路径:「🧹 消除选区内容」一键消除,无需 OCR;选项栏显示选区尺寸
- 结果写入独立「智能消除」画笔层:非破坏性/可撤销/可再编辑
- 修复 mask 边界:检测覆盖选区全部像素(原跳过边缘 2px,边缘字符残留)
- 修复视口偏移:尺寸变化时按中心补偿 pan(布局稳定后图片偏移致坐标错位)
- 实测:渐变底双语测试图,OCR 路径蓝字 5136→0 且红字保留;选框路径
  红字 6517→0 且蓝字保留;Python 独立复现对照验证算法一致性
This commit is contained in:
personal82555 committed 2026-10-21 20:46:58 +08:00
1 parent 83d98a348e
commit 72e06b24fe
5 files changed
+226 -20

No files matched your search

+217 -17
View File
@@ -73,7 +73,7 @@ const statusHint = computed(() => {
return '拖动文字/色块;选中画笔层后可整体移动';
}
if (tool.value === 'marquee') {
return '拖拽框选,松手后可填充/清除选区';
return '拖拽框选,松手后可「消除选区内容」(无痕去文字/水印)或填充/清除';
}
if (tool.value === 'text') {
return '点击画布放置文字层';
@@ -243,6 +243,7 @@ function scheduleRender(): void {
});
}
const lastView = { w: 0, h: 0 };
let checkerPattern: CanvasPattern | null = null;
function getChecker(ctx: CanvasRenderingContext2D): CanvasPattern {
if (!checkerPattern) {
@@ -277,6 +278,13 @@ function render(): void {
const vw = host.clientWidth;
const vh = host.clientHeight;
if (c.width !== Math.round(vw * dpr) || c.height !== Math.round(vh * dpr)) {
// 视口尺寸变化:按中心补偿平移,保持图像居中(否则布局稳定后图片会偏移)
if (lastView.w) {
panX.value += (vw - lastView.w) / 2;
panY.value += (vh - lastView.h) / 2;
}
lastView.w = vw;
lastView.h = vh;
c.width = Math.round(vw * dpr);
c.height = Math.round(vh * dpr);
c.style.width = `${vw}px`;
@@ -430,26 +438,29 @@ function normRect(a: { x: number; y: number }, b: { x: number; y: number }): { x
return { x: Math.round(Math.min(a.x, b.x)), y: Math.round(Math.min(a.y, b.y)), w: Math.round(Math.abs(b.x - a.x)), h: Math.round(Math.abs(b.y - a.y)) };
}
// ---------- 图层操作 ----------
function ensurePaintLayer(name?: string): PaintLayer {
let l = layers.value.find(v => v.id === activeLayerId.value);
if (l && l.type === 'paint') {
return l;
}
// ---------- 选区操作 ----------
function createPaintLayer(name: string): PaintLayer {
const c = document.createElement('canvas');
c.width = docW.value;
c.height = docH.value;
const pl: PaintLayer = { id: uid(), type: 'paint', name: name ?? '画笔图层', visible: true, opacity: 100, ox: 0, oy: 0, canvas: c };
// 插到文字/色块层下方(底图之上)
const pl: PaintLayer = { id: uid(), type: 'paint', name, visible: true, opacity: 100, ox: 0, oy: 0, canvas: c };
let idx = layers.value.findIndex(v => v.type === 'image');
if (idx < 0) {
idx = 0;
idx = layers.value.length - 1;
}
layers.value.splice(idx + 1, 0, pl);
activeLayerId.value = pl.id;
return pl;
}
function ensurePaintLayer(name?: string): PaintLayer {
const l = layers.value.find(v => v.id === activeLayerId.value);
if (l && l.type === 'paint') {
return l;
}
return createPaintLayer(name ?? '画笔图层');
}
function addPaintLayer(): void {
const c = document.createElement('canvas');
c.width = docW.value;
@@ -534,6 +545,25 @@ function fillSelection(): void {
scheduleRender();
}
// 智能消除选区:框住文字/水印/杂物 → 从周边背景扩散重建,无需 OCR
function inpaintSelection(): void {
const s = selection.value;
if (!s || s.w < 4 || s.h < 4) {
emit('error', '请先框选要消除的区域(不小于 4×4)');
return;
}
const ok = inpaintRect(s);
if (ok) {
selection.value = null;
commit('智能消除选区');
composite();
scheduleRender();
}
else {
emit('error', '未检出可消除的内容(区域与背景一致),可改用填充/画笔');
}
}
function clearSelection(): void {
const s = selection.value;
if (!s || s.w < 1 || s.h < 1) {
@@ -822,18 +852,182 @@ function applyHitErase(): void {
if (!hit || hit.used) {
return;
}
const color = sampleBoxColor(hit);
const padX = Math.max(1, Math.round(hit.w * 0.02));
const padY = Math.max(1, Math.round(hit.h * 0.08));
const fl: FillLayer = { id: uid(), type: 'fill', name: '擦除色块', visible: true, opacity: 100, x: Math.max(0, hit.x - padX), y: Math.max(0, hit.y - padY), w: hit.w + padX * 2, h: hit.h + padY * 2, color };
layers.value.push(fl);
// 智能消除:识别笔画像素,从周边背景扩散重建(inpainting)
const padX = Math.max(2, Math.round(hit.w * 0.04));
const padY = Math.max(2, Math.round(hit.h * 0.1));
const rect = {
x: Math.max(0, hit.x - padX),
y: Math.max(0, hit.y - padY),
w: Math.min(docW.value, hit.w + padX * 2),
h: Math.min(docH.value, hit.h + padY * 2),
};
rect.w = Math.min(rect.w, docW.value - rect.x);
rect.h = Math.min(rect.h, docH.value - rect.y);
const ok = inpaintRect(rect);
if (!ok) {
return;
}
hit.used = true;
selHitId.value = null;
commit('OCR 擦除文字');
commit('OCR 消除文字');
composite();
scheduleRender();
}
// ---------- 智能消除(inpainting) ----------
// 1) 取矩形区域(含外扩边距)像素;2) 与周边背景色差异大的判为文字笔画;
// 3) 笔画像素由四周已知像素迭代扩散重建(Gauss-Seidel),背景连续纹理/渐变都能自然补上;
// 4) 只把重建像素写入一个新的「智能消除」画笔层,非破坏性、可撤销、可再编辑。
function inpaintRect(rect: { x: number; y: number; w: number; h: number }): boolean {
if (!compCanvas || rect.w < 2 || rect.h < 2) {
return false;
}
const rw = Math.round(rect.w);
const rh = Math.round(rect.h);
const rx = Math.round(rect.x);
const ry = Math.round(rect.y);
const src = compCanvas.getContext('2d', { willReadFrequently: true })!.getImageData(rx, ry, rw, rh);
const d = src.data;
// 1) 背景色:区域外圈 2px 环带的中位色
const rs: number[] = [];
const gs: number[] = [];
const bs: number[] = [];
for (let y = 0; y < rh; y++) {
for (let x = 0; x < rw; x++) {
const onRing = x < 2 || y < 2 || x >= rw - 2 || y >= rh - 2;
if (!onRing) {
continue;
}
const i = (y * rw + x) * 4;
rs.push(d[i]);
gs.push(d[i + 1]);
bs.push(d[i + 2]);
}
}
if (!rs.length) {
return false;
}
const med = (a: number[]) => {
a.sort((p, q) => p - q);
return a[a.length >> 1];
};
const bgR = med(rs);
const bgG = med(gs);
const bgB = med(bs);
// 2) 笔画掩码:与背景色差异大的像素(全区域检测,含边缘字符)
const mask = new Uint8Array(rw * rh);
let unknown = 0;
const thr = 42;
for (let y = 0; y < rh; y++) {
for (let x = 0; x < rw; x++) {
const p = y * rw + x;
const i = p * 4;
const dist = Math.abs(d[i] - bgR) + Math.abs(d[i + 1] - bgG) + Math.abs(d[i + 2] - bgB);
if (dist > thr) {
mask[p] = 1;
unknown++;
}
}
}
if (!unknown) {
return false; // 没检出文字,无需处理
}
// 掩码向外扩 1px,把抗锯齿的半透明边缘也盖住(带边界检查)
const grown = mask.slice();
for (let y = 0; y < rh; y++) {
for (let x = 0; x < rw; x++) {
const p = y * rw + x;
if (mask[p]) {
continue;
}
if ((x > 0 && mask[p - 1]) || (x < rw - 1 && mask[p + 1]) || (y > 0 && mask[p - rw]) || (y < rh - 1 && mask[p + rw])) {
grown[p] = 1;
unknown++;
}
}
}
// 3) 扩散重建:未知像素反复取四邻均值(迭代次数随区域尺寸自适应)
const buf = new Float32Array(rw * rh * 3);
for (let p = 0; p < rw * rh; p++) {
const i = p * 4;
buf[p * 3] = d[i];
buf[p * 3 + 1] = d[i + 1];
buf[p * 3 + 2] = d[i + 2];
}
const iters = Math.min(500, Math.max(rw, rh) * 2);
for (let it = 0; it < iters; it++) {
// 交替扫描方向,消除顺序偏置
const rev = it % 2 === 1;
for (let yy = 0; yy < rh; yy++) {
const y = rev ? rh - 1 - yy : yy;
for (let xx = 0; xx < rw; xx++) {
const x = rev ? rw - 1 - xx : xx;
const p = y * rw + x;
if (!grown[p]) {
continue;
}
let sr = 0;
let sg = 0;
let sb = 0;
let c = 0;
if (x > 0) {
const q = (p - 1) * 3;
sr += buf[q];
sg += buf[q + 1];
sb += buf[q + 2];
c++;
}
if (x < rw - 1) {
const q = (p + 1) * 3;
sr += buf[q];
sg += buf[q + 1];
sb += buf[q + 2];
c++;
}
if (y > 0) {
const q = (p - rw) * 3;
sr += buf[q];
sg += buf[q + 1];
sb += buf[q + 2];
c++;
}
if (y < rh - 1) {
const q = (p + rw) * 3;
sr += buf[q];
sg += buf[q + 1];
sb += buf[q + 2];
c++;
}
if (c) {
buf[p * 3] = sr / c;
buf[p * 3 + 1] = sg / c;
buf[p * 3 + 2] = sb / c;
}
}
}
}
// 4) 写入独立画笔层(只含重建区域,其余透明)
const out = new ImageData(rw, rh);
const od = out.data;
for (let p = 0; p < rw * rh; p++) {
if (!grown[p]) {
continue;
}
const i = p * 4;
od[i] = Math.max(0, Math.min(255, Math.round(buf[p * 3])));
od[i + 1] = Math.max(0, Math.min(255, Math.round(buf[p * 3 + 1])));
od[i + 2] = Math.max(0, Math.min(255, Math.round(buf[p * 3 + 2])));
od[i + 3] = 255;
}
const tmp = document.createElement('canvas');
tmp.width = rw;
tmp.height = rh;
tmp.getContext('2d')!.putImageData(out, 0, 0);
const pl = createPaintLayer('智能消除');
pl.canvas.getContext('2d')!.drawImage(tmp, rx, ry);
return true;
}
function ocrHitTest(dx: number, dy: number): OcrHit | null {
for (const h of ocrHits.value) {
if (!h.used && dx >= h.x - 3 && dx <= h.x + h.w + 3 && dy >= h.y - 3 && dy <= h.y + h.h + 3) {
@@ -1239,6 +1433,10 @@ onBeforeUnmount(() => {
<input v-model="brushColor" type="color" class="psx-color" title="填充颜色">
</template>
<template v-else-if="tool === 'marquee' && selection">
<span class="psx-num">选区 {{ selection.w }}×{{ selection.h }}</span>
<button class="psx-btn primary" title="识别选区内的文字/水印并从背景重建(无痕消除)" @click="inpaintSelection">
🧹 消除选区内容
</button>
<button class="psx-btn" @click="fillSelection">填充选区</button>
<button class="psx-btn" @click="clearSelection">清除选区</button>
<button class="psx-btn" @click="selection = null; scheduleRender()">取消选区</button>
@@ -1253,7 +1451,9 @@ onBeforeUnmount(() => {
<template v-if="selHit">
<input v-model="hitText" class="psx-textin" :placeholder="selHit.text" @keyup.enter="applyHitReplace()">
<button class="psx-btn primary" @click="applyHitReplace()">✓ 替换</button>
<button class="psx-btn" @click="applyHitErase()">只擦除</button>
<button class="psx-btn" title="识别文字笔画并从周边背景扩散重建,无痕删除" @click="applyHitErase()">
🧹 消除文字
</button>
</template>
<span class="psx-flex" />
<button class="psx-btn" title="适应窗口 (0)" @click="fit()">适应</button>