node-opencv icon indicating copy to clipboard operation
node-opencv copied to clipboard

TypeScript definition file proposal

Open troywweber7 opened this issue 8 years ago • 3 comments

I'm currently trying to work with this project and develop a typescript definition file as I go (at a minimum for the features I'm using). As I amass definitions that match your bindings, I may eventually want to publish them for other people to use/improve.

There are two ways to do this: (1) bundle it with the repository in question or (2) publish it to DefinitelyTyped. Method (1) is preferred by all developers (including Microsoft, the creators of typescript), but of course method (2) is just as valid. If I went with method (1) the only addition to your project would be a single file by the name of index.d.ts. A typescript definition file like this streamlines the usage of pure javascript modules within a typescript project and makes it easier over time to investigate module functionality. In a way it will extend the documentation which would be useful for your project because it lacks so much documentation.

If method (1) is something you'd be okay with, then I'd be happy to discuss it here or perform a pull request in the future.

Thanks for your input!

troywweber7 avatar Jun 07 '17 19:06 troywweber7

My main worry would be that the typescript definition would get out of date. I'm not willing to require contributors to keep it up to date as not all of them use typescript.

I appreciate the work, and I'd love to be kept up to date with progress, but my instinct is that method (2) would be a better fit.

peterbraden avatar Jun 08 '17 07:06 peterbraden

I understand. For information sake, it wouldn't affect non-typescript devs. In fact, I think you would just have to say "file may be out of date, but if you are a TS developer, then please feel free to adapt the definition file and contribute". Either way, whether pubbed here or on Definitely Typed, I'll likely be seeking your input since you wrote the C-wrappers and know the functions better.

That is if I even continue to develop it. I've been toying around with the idea of using python instead as they have really good coverage of opencv. But I'll post what I have below as a starter for anyone interested and if they choose to do so, they can maybe use or improve it in their own project.

troywweber7 avatar Jun 08 '17 16:06 troywweber7

For anyone interested, you can save this as opencv.d.ts anywhere in your project and it should allow you to compile.

Please note: not all the definitions are completed, and the one's which are, I cannot guarantee they are perfect or proper. Also, to bundle with this project or be published to definitely typed, these definitions may have to take a slightly different format.

Hope this helps whomever:

/**
 * Created by Troy W. on 6/7/17.
 * Contributors:
 */

declare module 'opencv'
{
	/**
	 * TODO functions with arguments of ...unknown:any[] have not yet been tested/written --- TWW
	 */
	export class VideoCapture
	{
		/**
		 * Construct a new video capture object
		 * @param deviceNumOrFilename device number (as enumerated by the system) or the filename of a saved video file.
		 */
		constructor(deviceNumOrFilename:number|string);

		/**
		 * Asynchronously read the next frame image
		 * @param cb callback for handling the matrix data
		 */
		read(cb:(err:any, mat:Matrix) => void):void;

		setWidth(...unknown:any[]):any;

		setHeight(...unknown:any[]):any;

		setPosition(...unknown:any[]):any;

		getFrameAt(...unknown:any[]):any;

		getFrameCount(...unknown:any[]):any;

		release(...unknown:any[]):any;1

		/**
		 * Synchronously read the next frame matrix
		 * @constructor
		 */
		ReadSync():Matrix;

		grab(...unknown:any[]):any;

		retrieve(...unknown:any[]):any;
	}

	export class Matrix
	{
		/**
		 * Construct a new matrix
		 * @param rowsOrHeight number of rows for the matrix
		 * @param colsOrWidth number of cols for the matrix
		 */
		constructor(rowsOrHeight:number, colsOrWidth:number);

		/**
		 * Get a certain row in the matrix
		 * @param iRow row number (somewhere along the height)
		 */
		row(iRow:number):number[];

		/**
		 * Get a certain column in the matrix
		 * @param jCol col number (somewhere along the width)
		 */
		col(jCol:number):number[];

		/**
		 * Get a certain row of pixels (rgb values) in the matrix (rgb values not grouped)
		 * @param iRow row number (somewhere along the height)
		 */
		pixelRow(iRow:number):number[];

		/**
		 * Get a certain col of pixels (rgb values) in the matrix (rgb values not grouped)
		 * @param jCol col number (somewhere along the width)
		 */
		pixelCol(jCol:number):number[];

		empty(...unknown:any[]):any;

		/**
		 * Get a certain pixel (float value) in the matrix
		 * @param iRow row number
		 * @param jCol col number
		 */
		get(iRow:number, jCol:number):number;

		set(...unknown:any[]):any;

		put(...unknown:any[]):any;

		brightness(...unknown:any[]):any;

		normalize(...unknown:any[]):any;

		norm(...unknown:any[]):any;

		getData(...unknown:any[]):any;

		/**
		 * Get a certain pixel (rgb value) in the matrix
		 * @param iRow
		 * @param jCol
		 */
		pixel(iRow:number, jCol:number):[number, number, number];

		/**
		 * Return the width of the matrix (number of columns)
		 */
		width():number;

		/**
		 * Return the height of the matrix (number of rows)
		 */
		height():number;

		/**
		 * Return the height of the matrix as [rows, cols]
		 */
		size():[number, number];

		clone(...unknown:any[]):any;

		crop(...unknown:any[]):any;

		toBuffer():Buffer;

		toBufferAsync(...unknown:any[]):any;

		ellipse(...unknown:any[]):any;

		rectangle(...unknown:any[]):any;

		/**
		 * Draw a red line on the matrix
		 * @param pt1 point 1, format of [ jCol, iRow ]
		 * @param pt2 point 2, format of [ jCol, iRow ]
		 */
		line(pt1:[number, number], pt2:[number, number]):void;

		fillPoly(...unknown:any[]):any;

		/**
		 * Save the matrix as a jpg image
		 * @param filename ex. './test_image.jpg' or path.join(__dirname, 'test_image.jpg');
		 */
		save(filename:string):number;

		saveAsync(...unknown:any[]):any;

		resize(...unknown:any[]):any;

		rotate(...unknown:any[]):any;

		warpAffine(...unknown:any[]):any;

		copyTo(...unknown:any[]):any;

		convertTo(...unknown:any[]):any;

		pyrDown(...unknown:any[]):any;

		pyrUp(...unknown:any[]):any;

		channels(...unknown:any[]):any;

		convertGrayscale():void;

		convertHSVscale(...unknown:any[]):any;

		gaussianBlur(...unknown:any[]):any;

		medianBlur(...unknown:any[]):any;

		bilateralFilter(...unknown:any[]):any;

		sobel(...unknown:any[]):any;

		copy(...unknown:any[]):any;

		flip(...unknown:any[]):any;

		roi(...unknown:any[]):any;

		ptr(...unknown:any[]):any;

		absDiff(...unknown:any[]):any;

		addWeighted(...unknown:any[]):any;

		bitwiseXor(...unknown:any[]):any;

		bitwiseNot(...unknown:any[]):any;

		bitwiseAnd(...unknown:any[]):any;

		countNonZero(...unknown:any[]):any;

		moments(...unknown:any[]):any;

		canny(...unknown:any[]):any;

		dilate(...unknown:any[]):any;

		erode(...unknown:any[]):any;

		findContours(...unknown:any[]):any;

		drawContour(...unknown:any[]):any;

		drawAllContours(...unknown:any[]):any;

		goodFeaturesToTrack(...unknown:any[]):any;

		houghLinesP(...unknown:any[]):any;

		crop(...unknown:any[]):any;

		houghCircles(...unknown:any[]):any;

		inRange(...unknown:any[]):any;

		adjustROI(...unknown:any[]):any;

		locateROI(...unknown:any[]):any;

		threshold(...unknown:any[]):any;

		adaptiveThreshold(...unknown:any[]):any;

		meanStdDev(...unknown:any[]):any;

		cvtColor(...unknown:any[]):any;

		split(...unknown:any[]):any;

		merge(...unknown:any[]):any;

		equalizeHist(...unknown:any[]):any;

		floodFill(...unknown:any[]):any;

		matchTemplate(...unknown:any[]):any;

		matchTemplateByMatrix(...unknown:any[]):any;

		templateMatches(...unknown:any[]):any;

		minMaxLoc(...unknown:any[]):any;

		pushBack(...unknown:any[]):any;

		putText(...unknown:any[]):any;

		getPerspectiveTransform(...unknown:any[]):any;

		warpPerspective(...unknown:any[]):any;

		copyWithMask(...unknown:any[]):any;

		setWithMask(...unknown:any[]):any;

		meanWithMask(...unknown:any[]):any;

		mean(...unknown:any[]):any;

		shift(...unknown:any[]):any;

		reshape(...unknown:any[]):any;

		release(...unknown:any[]):any;

		subtract(...unknown:any[]):any;

		static Zeros(...unknown:any[]):any;

		static Ones(...unknown:any[]):any;

		/**
		 * Return an identity matrix
		 * @param rows
		 * @param cols
		 * @constructor
		 */
		static Eye(rows:number, cols:number):Matrix;

		static getRotationMatrix2D(...unknown:any[]):any;
	}

	/**
	 * Read a filename/buffer into a matrix
	 * @param filenameOrBuffer name of the file or a buffer object to load
	 * @param cb callback for handling the matrix data
	 */
	export function readImage(filenameOrBuffer:string|Buffer, cb:(err:any, mat:Matrix) => void):void;
}

troywweber7 avatar Jun 08 '17 16:06 troywweber7