ImgcodecsTest.java 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. package org.opencv.test.imgcodecs;
  2. import org.opencv.core.Mat;
  3. import org.opencv.core.MatOfByte;
  4. import org.opencv.core.MatOfInt;
  5. import org.opencv.imgproc.Imgproc;
  6. import org.opencv.imgcodecs.Imgcodecs;
  7. import org.opencv.imgcodecs.Animation;
  8. import org.opencv.test.OpenCVTestCase;
  9. import org.opencv.test.OpenCVTestRunner;
  10. import java.util.ArrayList;
  11. import java.util.List;
  12. public class ImgcodecsTest extends OpenCVTestCase {
  13. public void testAnimation() {
  14. if (!Imgcodecs.haveImageWriter("*.apng")) {
  15. return;
  16. }
  17. Mat src = Imgcodecs.imread(OpenCVTestRunner.LENA_PATH, Imgcodecs.IMREAD_REDUCED_COLOR_4);
  18. assertFalse(src.empty());
  19. Mat rgb = new Mat();
  20. Imgproc.cvtColor(src, rgb, Imgproc.COLOR_BGR2RGB);
  21. Animation animation = new Animation();
  22. List<Mat> frames = new ArrayList<>();
  23. MatOfInt durations = new MatOfInt(100, 100);
  24. frames.add(src);
  25. frames.add(rgb);
  26. animation.set_frames(frames);
  27. animation.set_durations(durations);
  28. String filename = OpenCVTestRunner.getTempFileName("png");
  29. assertTrue(Imgcodecs.imwriteanimation(filename, animation));
  30. Animation readAnimation = new Animation();
  31. assertTrue(Imgcodecs.imreadanimation(filename, readAnimation));
  32. List<Mat> readFrames = readAnimation.get_frames();
  33. assertTrue(readFrames.size() == 2);
  34. }
  35. public void testImdecode() {
  36. fail("Not yet implemented");
  37. }
  38. public void testImencodeStringMatListOfByte() {
  39. MatOfByte buff = new MatOfByte();
  40. assertEquals(0, buff.total());
  41. assertTrue( Imgcodecs.imencode(".jpg", gray127, buff) );
  42. assertFalse(0 == buff.total());
  43. }
  44. public void testImencodeStringMatListOfByteListOfInteger() {
  45. MatOfInt params40 = new MatOfInt(Imgcodecs.IMWRITE_JPEG_QUALITY, 40);
  46. MatOfInt params90 = new MatOfInt(Imgcodecs.IMWRITE_JPEG_QUALITY, 90);
  47. /* or
  48. MatOfInt params = new MatOfInt();
  49. params.fromArray(Imgcodecs.IMWRITE_JPEG_QUALITY, 40);
  50. */
  51. MatOfByte buff40 = new MatOfByte();
  52. MatOfByte buff90 = new MatOfByte();
  53. assertTrue( Imgcodecs.imencode(".jpg", rgbLena, buff40, params40) );
  54. assertTrue( Imgcodecs.imencode(".jpg", rgbLena, buff90, params90) );
  55. assertTrue(buff40.total() > 0);
  56. assertTrue(buff40.total() < buff90.total());
  57. }
  58. public void testImreadString() {
  59. dst = Imgcodecs.imread(OpenCVTestRunner.LENA_PATH);
  60. assertFalse(dst.empty());
  61. assertEquals(3, dst.channels());
  62. assertTrue(512 == dst.cols());
  63. assertTrue(512 == dst.rows());
  64. }
  65. public void testImreadStringInt() {
  66. dst = Imgcodecs.imread(OpenCVTestRunner.LENA_PATH, Imgcodecs.IMREAD_GRAYSCALE);
  67. assertFalse(dst.empty());
  68. assertEquals(1, dst.channels());
  69. assertTrue(512 == dst.cols());
  70. assertTrue(512 == dst.rows());
  71. }
  72. public void testImwriteStringMat() {
  73. fail("Not yet implemented");
  74. }
  75. public void testImwriteStringMatListOfInteger() {
  76. fail("Not yet implemented");
  77. }
  78. }