@@ -797,57 +797,63 @@ export class Tensor {
797
797
}
798
798
799
799
/**
800
- * A morphological operation that performs a dilation on the input tensor.
801
- * A kernel will be applied to each element and maximum value will be used.
800
+ * Mutates the data through a dilation morphological operation.
802
801
*
803
- * @param {DataArray } data The input tensor data.
804
802
* @param {KernelSize } kernelSize The width and height of the kernel.
805
803
* @param {Shape } [shape='RECT'] The shape of the kernel.
806
804
* @param {Point } [anchor={x: -1, y: -1}] The central position of the kernel.
807
- * @returns {Promise<DataArray > } The cloned, modified output tensor .
805
+ * @returns {Promise<Tensor > } Returns `this` .
808
806
*/
809
- async _dilate ( data , kernelSize = 3 , shape = 'RECT' , anchor = { x : - 1 , y : - 1 } ) {
810
- return this . morphologicalOperation ( 'DILATE' , data , kernelSize , shape , anchor ) ;
807
+ async dilate_ ( kernelSize = 3 , shape = 'RECT' , anchor = { x : - 1 , y : - 1 } ) {
808
+ const this_data = this . data ;
809
+ const data = await this . morphologicalOperation ( 'DILATE' , this_data , kernelSize , shape , anchor ) ;
810
+ for ( let i = 0 ; i < this_data . length ; ++ i ) {
811
+ this . data [ i ] = data [ i ] ;
812
+ }
813
+ return this ;
811
814
}
812
815
813
816
/**
814
- * Performs {@link Tensor._dilate} and returns a new Tensor.
817
+ * Returns a new Tensor where the data is mutated through a dilation
818
+ * morphological operation.
815
819
*
816
820
* @param {KernelSize } kernelSize The width and height of the kernel.
817
821
* @param {Shape } [shape='RECT'] The shape of the kernel.
818
822
* @param {Point } [anchor={x: -1, y: -1}] The central position of the kernel.
819
- * @returns {Promise<Tensor> } The cloned, modified output tensor .
823
+ * @returns {Promise<Tensor> } The new Tensor .
820
824
*/
821
825
async dilate ( kernelSize = 3 , shape = 'RECT' , anchor = { x : - 1 , y : - 1 } ) {
822
- const data = await this . _dilate ( this . data , kernelSize , shape , anchor ) ;
823
- return new Tensor ( this . type , data , this . dims ) ;
826
+ return this . clone ( ) . dilate_ ( kernelSize , shape , anchor ) ;
824
827
}
825
828
826
829
/**
827
- * A morphological operation that performs an erosion on the input tensor.
828
- * A kernel will be applied to each element and minimum value will be used.
830
+ * * Mutates the data through a erosion morphological operation.
829
831
*
830
- * @param {DataArray } data The input tensor data.
831
832
* @param {KernelSize } kernelSize The width and height of the kernel.
832
833
* @param {Shape } [shape='RECT'] The shape of the kernel.
833
834
* @param {Point } [anchor={x: -1, y: -1}] The central position of the kernel.
834
- * @returns {Promise<DataArray > } The cloned, modified output tensor .
835
+ * @returns {Promise<Tensor > } Returns `this` .
835
836
*/
836
- async _erode ( data , kernelSize = 3 , shape = 'RECT' , anchor = { x : - 1 , y : - 1 } ) {
837
- return this . morphologicalOperation ( 'ERODE' , data , kernelSize , shape , anchor ) ;
837
+ async erode_ ( kernelSize = 3 , shape = 'RECT' , anchor = { x : - 1 , y : - 1 } ) {
838
+ const this_data = this . data ;
839
+ const data = await this . morphologicalOperation ( 'ERODE' , this_data , kernelSize , shape , anchor ) ;
840
+ for ( let i = 0 ; i < this_data . length ; ++ i ) {
841
+ this . data [ i ] = data [ i ] ;
842
+ }
843
+ return this ;
838
844
}
839
845
840
846
/**
841
- * Performs {@link Tensor._erode} and returns a new Tensor.
847
+ * Returns a new Tensor where the data is mutated through a erosion
848
+ * morphological operation.
842
849
*
843
850
* @param {KernelSize } kernelSize The width and height of the kernel.
844
851
* @param {Shape } [shape='RECT'] The shape of the kernel.
845
852
* @param {Point } [anchor={x: -1, y: -1}] The central position of the kernel.
846
- * @returns {Promise<Tensor> } The cloned, modified output tensor .
853
+ * @returns {Promise<Tensor> } The new Tensor .
847
854
*/
848
855
async erode ( kernelSize = 3 , shape = 'RECT' , anchor = { x : - 1 , y : - 1 } ) {
849
- const data = await this . _erode ( this . data , kernelSize , shape , anchor ) ;
850
- return new Tensor ( this . type , data , this . dims ) ;
856
+ return this . clone ( ) . erode_ ( kernelSize , shape , anchor ) ;
851
857
}
852
858
853
859
/**
@@ -958,19 +964,15 @@ export class Tensor {
958
964
case 'DILATE' :
959
965
return this . dilate ( kernelSize , shape , anchor ) ;
960
966
961
- case 'OPEN' : {
962
- let data = this . data ;
963
- data = await this . _erode ( data , kernelSize , shape , anchor ) ;
964
- data = await this . _dilate ( data , kernelSize , shape , anchor ) ;
965
- return new Tensor ( this . type , data , this . dims ) ;
966
- }
967
+ case 'OPEN' :
968
+ return ( await this
969
+ . erode_ ( kernelSize , shape , anchor ) )
970
+ . dilate_ ( kernelSize , shape , anchor ) ;
967
971
968
- case 'CLOSE' : {
969
- let data = this . data ;
970
- data = await this . _dilate ( data , kernelSize , shape , anchor ) ;
971
- data = await this . _erode ( data , kernelSize , shape , anchor ) ;
972
- return new Tensor ( this . type , data , this . dims ) ;
973
- }
972
+ case 'CLOSE' :
973
+ return ( await this
974
+ . dilate_ ( kernelSize , shape , anchor ) )
975
+ . erode_ ( kernelSize , shape , anchor ) ;
974
976
975
977
default :
976
978
throw new Error ( "Unknown morphological operation" ) ;
0 commit comments