-
Notifications
You must be signed in to change notification settings - Fork 323
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
305 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
from Base import all | ||
import Image.Data.Color | ||
import Image.Data.Color.Internal as Color_Internal | ||
import Image.Data.Matrix | ||
|
||
polyglot java import org.enso.image.data.Matrix as Java_Matrix | ||
polyglot java import org.opencv.core.Mat | ||
polyglot java import org.opencv.core.Scalar | ||
|
||
## PRIVATE | ||
create rows cols mat_type values = | ||
opencv_scalar = Scalar.new values.to_array | ||
opencv_mat = Mat.new rows cols mat_type opencv_scalar | ||
Matrix.Matrix opencv_mat | ||
|
||
## PRIVATE | ||
core_op mat value op = | ||
new_mat = Java_Matrix.zeros mat | ||
scalar = case value of | ||
Vector.Vector arr -> | ||
Color_Internal.to_scalar (Color.from_array arr) . catch (Panic.throw _) | ||
Color.Channels_1 c1 -> | ||
Scalar.new c1 | ||
Color.Channels_2 c1 c2 -> | ||
Scalar.new c1 c2 | ||
Color.Channels_3 c1 c2 c3 -> | ||
Scalar.new c1 c2 c3 | ||
Color.Channels_4 c1 c2 c3 c4 -> | ||
Scalar.new c1 c2 c3 c4 | ||
Matrix.Matrix m -> | ||
m | ||
_ -> | ||
Scalar.new value | ||
op mat scalar new_mat | ||
Matrix.Matrix new_mat | ||
|
||
## PRIVATE | ||
core_op_handler error = case error of | ||
Color.Unsupported_Channels_Number _ -> Error.throw error | ||
_ -> Panic.throw error |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,75 @@ | ||
package org.enso.image.data; | ||
|
||
import org.opencv.core.CvType; | ||
import org.opencv.core.Mat; | ||
|
||
public class Matrix { | ||
|
||
// type depth constants | ||
public static final int | ||
CV_8U = CvType.CV_8U, | ||
CV_8UC1 = CvType.CV_8UC1, | ||
CV_8S = CvType.CV_8S, | ||
CV_16U = CvType.CV_16U, | ||
CV_16S = CvType.CV_16S, | ||
CV_32S = CvType.CV_32S, | ||
CV_32F = CvType.CV_32F, | ||
CV_64F = CvType.CV_64F, | ||
CV_16F = CvType.CV_16F; | ||
|
||
public static int CV_8UC(int channels) { | ||
return CvType.CV_8UC(channels); | ||
} | ||
|
||
public static Mat zeros(Mat m) { | ||
return Mat.zeros(m.size(), m.type()); | ||
} | ||
|
||
public static Mat zeros(int rows, int cols, int type) { | ||
return Mat.zeros(rows, cols, type); | ||
} | ||
|
||
public static Mat ones(int rows, int cols, int type) { | ||
return Mat.ones(rows, cols, type); | ||
} | ||
|
||
public static Mat eye(int rows, int cols, int type) { | ||
return Mat.eye(rows, cols, type); | ||
} | ||
|
||
public static Object to_vector(Mat mat) { | ||
switch (dataSize(mat.type())) { | ||
case CvType.CV_8U: | ||
case CvType.CV_8S: | ||
byte[] buf8 = new byte[(int) mat.total() * mat.channels()]; | ||
mat.get(0, 0, buf8); | ||
return buf8; | ||
case CvType.CV_16U: | ||
case CvType.CV_16S: | ||
short[] buf16 = new short[(int) mat.total() * mat.channels()]; | ||
mat.get(0, 0, buf16); | ||
return buf16; | ||
case CvType.CV_32S: | ||
int[] buf32s = new int[(int) mat.total() * mat.channels()]; | ||
mat.get(0, 0, buf32s); | ||
return buf32s; | ||
case CvType.CV_32F: | ||
float[] buf32f = new float[(int) mat.total() * mat.channels()]; | ||
mat.get(0, 0, buf32f); | ||
return buf32f; | ||
case CvType.CV_64F: | ||
double[] buf64f = new double[(int) mat.total() * mat.channels()]; | ||
mat.get(0, 0, buf64f); | ||
return buf64f; | ||
case CvType.CV_16F: | ||
short[] buf16f = new short[(int) mat.total() * mat.channels()]; | ||
mat.get(0, 0, buf16f); | ||
return buf16f; | ||
} | ||
return null; | ||
} | ||
|
||
private static int dataSize(int type) { | ||
return CvType.ELEM_SIZE(type) / CvType.channels(type); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
from Base import all | ||
import Image.Data.Matrix | ||
import Image.Data.Color | ||
import Test | ||
|
||
polyglot java import org.enso.image.data.Matrix as Java_Matrix | ||
|
||
spec = | ||
Test.group "Matrix channels 1" <| | ||
zeros = Matrix.Matrix (Java_Matrix.zeros 3 3 0) | ||
ones = Matrix.Matrix (Java_Matrix.ones 3 3 0) | ||
eye = Matrix.Matrix (Java_Matrix.eye 3 3 0) | ||
Test.specify "should convert to vector" <| | ||
zeros.to_vector . should_equal (Vector.fill 9 0) | ||
ones.to_vector . should_equal (Vector.fill 9 1) | ||
eye.to_vector . should_equal [1, 0, 0, 0, 1, 0, 0, 0, 1] | ||
Test.specify "should get value" <| | ||
eye.get 0 0 . should_equal [1] | ||
eye.get 1 0 . should_equal [0] | ||
eye.get 1 1 . should_equal [1] | ||
Test.expect_error_with (eye.get 10 10) Matrix.Index_Out_Of_Bounds_Error | ||
Test.expect_error_with (eye.get -1 -1) Matrix.Index_Out_Of_Bounds_Error | ||
|
||
Test.specify "should add scalar" <| | ||
(zeros + 1).to_vector . should_equal (Vector.fill 9 1) | ||
(ones + 1).to_vector . should_equal (Vector.fill 9 2) | ||
Test.specify "should add vector" <| | ||
(zeros + [1]).to_vector . should_equal (Vector.fill 9 1) | ||
(ones + [1, 1]).to_vector . should_equal (Vector.fill 9 2) | ||
Test.expect_error_with (eye + []) Color.Unsupported_Channels_Number | ||
Test.specify "should add color" <| | ||
c1 = Color.from_vector [1] | ||
(zeros + c1).to_vector . should_equal (Vector.fill 9 1) | ||
Test.specify "should add matrix" <| | ||
(zeros + ones).to_vector . should_equal (Vector.fill 9 1) | ||
(eye + eye).to_vector . should_equal [2, 0, 0, 0, 2, 0, 0, 0, 2] | ||
(ones + eye).to_vector . should_equal [2, 1, 1, 1, 2, 1, 1, 1, 2] | ||
|
||
Test.specify "should subtract scalar" <| | ||
(ones - 1).to_vector . should_equal (Vector.fill 9 0) | ||
(zeros - 1).to_vector . should_equal (Vector.fill 9 0) | ||
Test.specify "should subtract vector" <| | ||
(zeros - [1]).to_vector . should_equal (Vector.fill 9 0) | ||
(ones - [1, 1]).to_vector . should_equal (Vector.fill 9 0) | ||
Test.expect_error_with (eye - []) Color.Unsupported_Channels_Number | ||
Test.specify "should subtract color" <| | ||
c1 = Color.from_vector [1] | ||
(ones - c1).to_vector . should_equal (Vector.fill 9 0) | ||
Test.specify "should subtract matrix" <| | ||
(ones - zeros).to_vector . should_equal (Vector.fill 9 1) | ||
(ones - eye).to_vector . should_equal [0, 1, 1, 1, 0, 1, 1, 1, 0] | ||
|
||
Test.specify "should multiply scalar" <| | ||
(ones * 3).to_vector . should_equal (Vector.fill 9 3) | ||
(zeros * 4).to_vector . should_equal (Vector.fill 9 0) | ||
(eye * 5).to_vector . should_equal [5, 0, 0, 0, 5, 0, 0, 0, 5] | ||
Test.specify "should multiply vector" <| | ||
(zeros * [2]).to_vector . should_equal (Vector.fill 9 0) | ||
(ones + [2, 2]).to_vector . should_equal (Vector.fill 9 3) | ||
Test.expect_error_with (eye * []) Color.Unsupported_Channels_Number | ||
Test.specify "should multiply color" <| | ||
c1 = Color.from_vector [2] | ||
(ones * c1).to_vector . should_equal (Vector.fill 9 2) | ||
Test.specify "should multiply matrix" <| | ||
(ones * zeros).to_vector . should_equal (Vector.fill 9 0) | ||
(ones * ones).to_vector . should_equal (Vector.fill 9 1) | ||
(ones * eye).to_vector . should_equal [1, 0, 0, 0, 1, 0, 0, 0, 1] | ||
|
||
Test.specify "should divide scalar" <| | ||
(zeros / 2).to_vector . should_equal (Vector.fill 9 0) | ||
(ones / 2).to_vector . should_equal (Vector.fill 9 0) | ||
Test.specify "should divide vector" <| | ||
(zeros / [2]).to_vector . should_equal (Vector.fill 9 0) | ||
(ones / [2, 2]).to_vector . should_equal (Vector.fill 9 0) | ||
Test.expect_error_with (eye / []) Color.Unsupported_Channels_Number | ||
Test.specify "should divide color" <| | ||
c1 = Color.from_vector [1] | ||
(ones / c1).to_vector . should_equal (Vector.fill 9 1) | ||
Test.specify "should divide matrix" <| | ||
(zeros / ones).to_vector . should_equal (Vector.fill 9 0) | ||
(ones / ones).to_vector . should_equal (Vector.fill 9 1) | ||
|
||
Test.specify "should sum" <| | ||
zeros.sum . should_equal (Color.from_vector [0, 0, 0, 0]) | ||
ones.sum . should_equal (Color.from_vector [9, 0, 0, 0]) | ||
|
||
Test.group "Matrix channels 3" <| | ||
zeros = Matrix.Matrix (Java_Matrix.zeros 3 3 (Java_Matrix.CV_8UC 3)) | ||
ones = Matrix.Matrix (Java_Matrix.ones 3 3 (Java_Matrix.CV_8UC 3)) | ||
eye = Matrix.Matrix (Java_Matrix.eye 3 3 (Java_Matrix.CV_8UC 3)) | ||
zeros_idx = Vector.fill 9 0 | ||
#ones_idx = Vector.fill 1 0 | ||
eye_idx = [1, 0, 0, 0, 1, 0, 0, 0, 1] | ||
Test.specify "should convert to vector" <| | ||
zeros.to_vector . should_equal (Vector.fill 3*3*3 0) | ||
ones.to_vector . should_equal (zeros_idx.flat_map (_ -> [1, 0, 0])) | ||
eye.to_vector . should_equal (eye_idx.flat_map (i -> if i == 1 then [1, 0, 0] else [0, 0, 0])) | ||
Test.specify "should get value" <| | ||
eye.get 0 0 . should_equal [1, 0, 0] | ||
eye.get 1 0 . should_equal [0, 0, 0] | ||
eye.get 1 1 . should_equal [1, 0, 0] | ||
Test.expect_error_with (eye.get 10 10) Matrix.Index_Out_Of_Bounds_Error | ||
Test.expect_error_with (eye.get -1 -1) Matrix.Index_Out_Of_Bounds_Error |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
import Test | ||
import Image_Tests.Io_Spec | ||
import Image_Tests.Data.Color_Spec | ||
import Image_Tests.Data.Matrix_Spec | ||
|
||
main = Test.Suite.runMain <| | ||
Io_Spec.spec | ||
Color_Spec.spec | ||
Matrix_Spec.spec |
Oops, something went wrong.