ppu-ocv 1.2.2 → 1.2.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/image-processor.d.ts +7 -0
- package/image-processor.js +1 -1
- package/operations/convert.d.ts +12 -0
- package/operations/convert.js +1 -0
- package/package.json +1 -1
- package/pipeline/index.d.ts +2 -0
- package/pipeline/index.js +1 -1
package/image-processor.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { Canvas, cv } from "./index";
|
|
2
2
|
import type { AdaptiveThresholdOptions, BlurOptions, BorderOptions, CannyOptions, DilateOptions, ErodeOptions, GrayscaleOptions, InvertOptions, MorphologicalGradientOptions, OperationName, OperationOptions, RequiredOptions, ResizeOptions, ThresholdOptions, WarpOptions } from "./index";
|
|
3
|
+
import type { ConvertOptions } from "./pipeline";
|
|
3
4
|
type NameWithRequiredOptions = {
|
|
4
5
|
[N in OperationName]: OperationOptions<N> extends RequiredOptions ? N : never;
|
|
5
6
|
}[OperationName];
|
|
@@ -100,6 +101,12 @@ export declare class ImageProcessor {
|
|
|
100
101
|
* @param options Warp configuration options
|
|
101
102
|
*/
|
|
102
103
|
warp(options: WarpOptions): this;
|
|
104
|
+
/**
|
|
105
|
+
* Convert image matrix into new matrix type
|
|
106
|
+
* @description Usage order: independent
|
|
107
|
+
* @param options Convert configuration options
|
|
108
|
+
*/
|
|
109
|
+
convert(options: ConvertOptions): this;
|
|
103
110
|
/**
|
|
104
111
|
* Destroy the image (cv.Mat) stored in image processor state
|
|
105
112
|
* @kind non-chainable
|
package/image-processor.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export class ImageProcessor{img;width;height;constructor(source){if(source instanceof Canvas){let ctx=source.getContext("2d");let imageData=ctx.getImageData(0,0,source.width,source.height);this.img=cv.matFromImageData(imageData);this.width=source.width;this.height=source.height}else if(source instanceof cv.Mat){this.img=source;this.width=source.cols;this.height=source.rows}else{throw new Error("Invalid source type. Must be either Canvas or cv.Mat.")}}static async prepareCanvas(file){let img=await loadImage(file);let canvas=createCanvas(img.width,img.height);let ctx=canvas.getContext("2d");ctx.drawImage(img,0,0);return canvas}static async initRuntime(){return new Promise((res)=>{if(cv&&cv.Mat){res()}else{cv["onRuntimeInitialized"]=()=>{res()}}})}execute(operationName,options){if(!registry.hasOperation(operationName)){throw new Error(`Operation "${operationName}" not found`)}try{let result=executeOperation(operationName,this.img,options);this.img=result.img;this.width=result.width;this.height=result.height}catch(error){console.error(`Error executing operation "${operationName}":`,error);throw error}return this}grayscale(options={}){return this.execute("grayscale",options)}invert(options={}){return this.execute("invert",options)}border(options={}){return this.execute("border",options)}blur(options={}){return this.execute("blur",options)}threshold(options={}){return this.execute("threshold",options)}adaptiveThreshold(options={}){return this.execute("adaptiveThreshold",options)}canny(options={}){return this.execute("canny",options)}morphologicalGradient(options={}){return this.execute("morphologicalGradient",options)}erode(options={}){return this.execute("erode",options)}dilate(options={}){return this.execute("dilate",options)}resize(options){return this.execute("resize",options)}warp(options){return this.execute("warp",options)}destroy(){this.img.delete()}toMat(){return this.img}toCanvas(){let canvas=createCanvas(this.width,this.height);let ctx=canvas.getContext("2d");let imgData=ctx.createImageData(this.width,this.height);if(this.img.channels()===1){let data=imgData.data;let gray=new Uint8Array(this.img.data);for(let i=0;i<gray.length;i++){data[i*4]=gray[i];data[i*4+1]=gray[i];data[i*4+2]=gray[i];data[i*4+3]=255}}else{imgData.data.set(new Uint8ClampedArray(this.img.data))}ctx.putImageData(imgData,0,0);return canvas}}import{Canvas,createCanvas,cv,loadImage}from"./index";import{executeOperation,registry}from"./index";
|
|
1
|
+
export class ImageProcessor{img;width;height;constructor(source){if(source instanceof Canvas){let ctx=source.getContext("2d");let imageData=ctx.getImageData(0,0,source.width,source.height);this.img=cv.matFromImageData(imageData);this.width=source.width;this.height=source.height}else if(source instanceof cv.Mat){this.img=source;this.width=source.cols;this.height=source.rows}else{throw new Error("Invalid source type. Must be either Canvas or cv.Mat.")}}static async prepareCanvas(file){let img=await loadImage(file);let canvas=createCanvas(img.width,img.height);let ctx=canvas.getContext("2d");ctx.drawImage(img,0,0);return canvas}static async initRuntime(){return new Promise((res)=>{if(cv&&cv.Mat){res()}else{cv["onRuntimeInitialized"]=()=>{res()}}})}execute(operationName,options){if(!registry.hasOperation(operationName)){throw new Error(`Operation "${operationName}" not found`)}try{let result=executeOperation(operationName,this.img,options);this.img=result.img;this.width=result.width;this.height=result.height}catch(error){console.error(`Error executing operation "${operationName}":`,error);throw error}return this}grayscale(options={}){return this.execute("grayscale",options)}invert(options={}){return this.execute("invert",options)}border(options={}){return this.execute("border",options)}blur(options={}){return this.execute("blur",options)}threshold(options={}){return this.execute("threshold",options)}adaptiveThreshold(options={}){return this.execute("adaptiveThreshold",options)}canny(options={}){return this.execute("canny",options)}morphologicalGradient(options={}){return this.execute("morphologicalGradient",options)}erode(options={}){return this.execute("erode",options)}dilate(options={}){return this.execute("dilate",options)}resize(options){return this.execute("resize",options)}warp(options){return this.execute("warp",options)}convert(options){return this.execute("convert",options)}destroy(){this.img.delete()}toMat(){return this.img}toCanvas(){let canvas=createCanvas(this.width,this.height);let ctx=canvas.getContext("2d");let imgData=ctx.createImageData(this.width,this.height);if(this.img.channels()===1){let data=imgData.data;let gray=new Uint8Array(this.img.data);for(let i=0;i<gray.length;i++){data[i*4]=gray[i];data[i*4+1]=gray[i];data[i*4+2]=gray[i];data[i*4+3]=255}}else{imgData.data.set(new Uint8ClampedArray(this.img.data))}ctx.putImageData(imgData,0,0);return canvas}}import{Canvas,createCanvas,cv,loadImage}from"./index";import{executeOperation,registry}from"./index";
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { OperationResult, RequiredOptions } from "../index";
|
|
2
|
+
import { cv } from "../index";
|
|
3
|
+
declare module "../index" {
|
|
4
|
+
interface RegisteredOperations {
|
|
5
|
+
convert: ConvertOptions;
|
|
6
|
+
}
|
|
7
|
+
}
|
|
8
|
+
export interface ConvertOptions extends RequiredOptions {
|
|
9
|
+
/** Desired matrix type (cv.CV_...) if negative, it will be the same as input */
|
|
10
|
+
rtype: number;
|
|
11
|
+
}
|
|
12
|
+
export declare function convert(img: cv.Mat, options: ConvertOptions): OperationResult;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{cv,registry}from"../index";export function convert(img,options){if(options.rtype===undefined){throw new Error("Invalid options: rtype is required")}let imgConvert=new cv.Mat;img.convertTo(imgConvert,options.rtype);img.delete();return{img:imgConvert,width:imgConvert.cols,height:imgConvert.rows}}registry.register("convert",convert);
|
package/package.json
CHANGED
package/pipeline/index.d.ts
CHANGED
|
@@ -4,6 +4,7 @@ import "../operations/adaptive-threshold";
|
|
|
4
4
|
import "../operations/blur";
|
|
5
5
|
import "../operations/border";
|
|
6
6
|
import "../operations/canny";
|
|
7
|
+
import "../operations/convert";
|
|
7
8
|
import "../operations/dilate";
|
|
8
9
|
import "../operations/erode";
|
|
9
10
|
import "../operations/grayscale";
|
|
@@ -16,6 +17,7 @@ export type { AdaptiveThresholdOptions } from "../operations/adaptive-threshold"
|
|
|
16
17
|
export type { BlurOptions } from "../operations/blur";
|
|
17
18
|
export type { BorderOptions } from "../operations/border";
|
|
18
19
|
export type { CannyOptions } from "../operations/canny";
|
|
20
|
+
export type { ConvertOptions } from "../operations/convert";
|
|
19
21
|
export type { DilateOptions } from "../operations/dilate";
|
|
20
22
|
export type { ErodeOptions } from "../operations/erode";
|
|
21
23
|
export type { GrayscaleOptions } from "../operations/grayscale";
|
package/pipeline/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export{executeOperation,OperationRegistry,registry}from"./registry";import"../operations/adaptive-threshold";import"../operations/blur";import"../operations/border";import"../operations/canny";import"../operations/dilate";import"../operations/erode";import"../operations/grayscale";import"../operations/invert";import"../operations/morphological-gradient";import"../operations/resize";import"../operations/threshold";import"../operations/warp";
|
|
1
|
+
export{executeOperation,OperationRegistry,registry}from"./registry";import"../operations/adaptive-threshold";import"../operations/blur";import"../operations/border";import"../operations/canny";import"../operations/convert";import"../operations/dilate";import"../operations/erode";import"../operations/grayscale";import"../operations/invert";import"../operations/morphological-gradient";import"../operations/resize";import"../operations/threshold";import"../operations/warp";
|