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