JS:在另一个 canvas 内找到 image/canvas,然后是矩形

JS: Locate image/canvas inside of another canvas, and then rectangle

我怎样才能做这样的事情? 图一:

图二:

我该怎么做:

const image1 = document.querySelector("#imgone");
const image2 = document.querySelector("#imgtwo");

let ctx1 = image1.getContext("2d");
let ctx2 = image2.getContext("2d");

function locate(context1, context2) {
    #?????? 
    return "..."
}

function rectangle(context, corner1,corner2,corner3,corner4) {
    #??????
    return "..."
}



[corner1,corner2,corner3,corner4] = locate(ctx,ctx2);
rectangle(ctx2, corner1,corner2,corner3,corner4);



基本上是一项计算机视觉任务。 可能吗?你怎么做到的?

是我在另一个帐户上使用的。我已经弄明白了,想给你看。

我的解决方案非常低效,但我有这段代码。

function canvas(w = 300, h = 150) {
  let r = document.createElement("canvas")
  r.height = h
  r.width = w
  return r
}

function points(startx, starty, w, h) {
  return [
    [startx, starty],
    [startx + w, starty],
    [startx + w, starty + h],
    [startx, starty + h]
  ]
}


function getPixelColor(canvas, x, y) {
  return Array.from(canvas.getContext("2d").getImageData(x, y, 1, 1).data)
}

function gpc(canvas, x, y, sx, sy) {
  return Array.from(canvas.getContext("2d").getImageData(x, y, sx, sy).data)
}

function gap(canvas) {
  return gpc(canvas, 0, 0, canvas.width, canvas.height)
}

function gx(index, w = 300, h = 150, nx = 5, ny = 5) {
  let fpd = []
  for (let i = 0; i < w; i += nx) {
    for (let ii = 0; ii < h; ii += ny) {
      fpd.push([i, ii])
    }
  }

  return fpd[index]
}

// function genPixels(canvas,nx=5,ny=5) {
//    let ctx = canvas.getContext("2d")
//    let fpd = []
//    for (let i = 0; i < canvas.width; i+=nx) {
//     for (let ii = 0; ii < canvas.height; ii+=ny) {
//         fpd.push(Array.from(ctx.getImageData(i,ii,nx,ny).data))
//     }
//    }
//    return fpd
// }

function chunk(n, c) {
  return n.reduce((n, r, t) => {
    const o = Math.floor(t / c);
    return n[o] = [].concat(n[o] || [], r), n
  }, [])
}


function findImg(parent, canvas, ih, iw) {
  if (parent.tagName !== "canvas") {
    console.log("parent argument must be canvas. use canvas.content.drawImage on a new canvas to convert img to canvas.")
  }
  if (canvas.tagName === "img") {
    console.log("canvas argument must be type canvas. use canvas.context.drawImage on a new canvas to convert img to canvas.")
  }
  let allPixels = gap(parent)
  let cp = gap(canvas)
  let chunks = chunk(allPixels, ih * iw * 4)
  for (let i = 0; i < chunks.length; i += 1) {
    if (String(chunks[i]) === String(cp)) {
      let start = gx(i, parent.width, parent.height, iw, ih)
      return points(start[0], start[1], iw, ih)
    }
  }
  return false
}

let pointsc = findImg(canvas(), canvas(5, 5), 5, 5);
let dim = pointsc[2].reverse()
this.strokeStyle = "black"
let yours = document.querySelector("#yours")
yours.getContext("2d").strokeRect(0, 0, dim[0], dim[1])
<canvas id="yours"></canvas>

缩小:

function canvas(t=300,n=150){let e=document.createElement("canvas");return e.height=n,e.width=t,e}function points(t,n,e,a){return[[t,n],[t+e,n],[t+e,n+a],[t,n+a]]}function getPixelColor(t,n,e){return Array.from(t.getContext("2d").getImageData(n,e,1,1).data)}function gpc(t,n,e,a,r){return Array.from(t.getContext("2d").getImageData(n,e,a,r).data)}function gap(t){return gpc(t,0,0,t.width,t.height)}function gx(t,n=300,e=150,a=5,r=5){let o=[];for(let t=0;t<n;t+=a)for(let n=0;n<e;n+=r)o.push([t,n]);return o[t]}function chunk(t,n){return t.reduce((t,e,a)=>{const r=Math.floor(a/n);return t[r]=[].concat(t[r]||[],e),t},[])}function findImg(t,n,e,a){"CANVAS"!==t.tagName&&console.log("parent argument must be canvas. use canvas.content.drawImage on a new canvas to convert img to canvas."),"IMG"===n.tagName&&console.log("canvas argument must be type canvas. use canvas.context.drawImage on a new canvas to convert img to canvas.");let r=gap(t),o=gap(n),c=chunk(r,e*a*4);for(let n=0;n<c.length;n+=1)if(String(c[n])===String(o)){let r=gx(n,t.width,t.height,a,e);return points(r[0],r[1],a,e)}return!1}

我将用两个白色图像做一个例子。

let pointsc = findImg(canvas(), canvas(5,5), 5, 5);
let dim = pointsc[2].reverse()
this.strokeStyle="black"
document.querySelector("#yours").getContext("2d").strokeRect(0,0,dim[0],dim[1])

这是非常低效的,我正在寻求建议,例如如何不使用尽可能多的循环。