TechStark / opencv-js

OpenCV JavaScript version for node.js or browser
Apache License 2.0
353 stars 31 forks source link

error cv.LUT is not a function #50

Open ghost opened 6 months ago

ghost commented 6 months ago

got an error when using cv.Lut here is my code

static async changeAndSaveExposure(
    inputImagePath: string,
    outputImagePath: string,
    value: number
  ) {
    if (value < -100 || value > 100) {
      throw new Error("Exposure value must be between -100 and 100");
    }

    try {
      const image = await Jimp.read(inputImagePath);
      const { data, width, height } = image.bitmap;
      const imageData = new ImageData(
        new Uint8ClampedArray(data),
        width,
        height
      );
      let src = cv.matFromImageData(imageData);
      const gammaTable = new Uint8Array(imageData.data.length);

      for (let x = 0; x < imageData.data.length; x++) {
          gammaTable[x] = Math.round(Math.pow(x / 255.0, (value * -1) + 1) * 255.0);
      }

      const gammaMat = cv.matFromArray(1, imageData.data.length, cv.CV_8U, gammaTable);
      const dst = new cv.Mat();
      cv.LUT(src, gammaMat, dst)
      new Jimp({
        width: dst.cols,
        height: dst.rows,
        data: Buffer.from(dst.data),
      }).write(outputImagePath);
      console.log(`Success`);
    } catch (error) {
      console.error("Error:", error);
    }
  }