简体   繁体   中英

OpenCV.js GaussianBlur function is not working?

I'm trying to process image with GaussianBlur.

But I can't make it work. It says:

Uncaught TypeError: fields[fieldName].write is not a function
    at Object.toWireType (opencv.js:30)
    at Object.GaussianBlur (eval at new_ (opencv.js:30), <anonymous>:9:26)
    at Object.proto.<computed> [as GaussianBlur] (opencv.js:30)
    at HTMLImageElement.img.onload (pen.js:110)

Here is codepen: https://codepen.io/sundowatch/pen/jOEqrqY?editors=1010

I can't find what is the problem with that.

It works for me, here is a working example:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Hello OpenCV.js</title>
</head>
<body>
<h2>Hello OpenCV.js</h2>
<p id="status">OpenCV.js is loading...</p>
<div>
  <div class="inputoutput">
    <img id="imageSrc" alt="No Image" />
    <div class="caption">imageSrc <input type="file" id="fileInput" name="file" /></div>
  </div>
  <div class="inputoutput">
    <canvas id="canvasOutput" ></canvas>
    <div class="caption">canvasOutput</div>
  </div>
</div>
<script type="text/javascript">
let imgElement = document.getElementById('imageSrc');
let inputElement = document.getElementById('fileInput');
inputElement.addEventListener('change', (e) => {
  imgElement.src = URL.createObjectURL(e.target.files[0]);
}, false);
imgElement.onload = function() {
  let src = cv.imread(imgElement);
  let dst = new cv.Mat();
  let ksize = new cv.Size(5, 5);
  cv.GaussianBlur(src, dst, ksize, 0, 0, cv.BORDER_DEFAULT);
  cv.imshow('canvasOutput', dst);
  src.delete(); dst.delete();
};
function onOpenCvReady() {
  document.getElementById('status').innerHTML = 'OpenCV.js is ready.';
}
</script>
<script async src="https://docs.opencv.org/master/opencv.js" onload="onOpenCvReady();" type="text/javascript"></script>
</body>
</html>

Your problem is elsewhere. In developer tools on your snippet if I try to get the cv element I get an error. Can you double check that you are loading opencv.js <script async src="https://docs.opencv.org/master/opencv.js" onload="onOpenCvReady();" type="text/javascript"></script> <script async src="https://docs.opencv.org/master/opencv.js" onload="onOpenCvReady();" type="text/javascript"></script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM