1 package org.opencv.test.android; 2 3 import org.opencv.android.Utils; 4 import org.opencv.core.Core; 5 import org.opencv.core.CvType; 6 import org.opencv.core.Mat; 7 import org.opencv.core.Scalar; 8 import org.opencv.imgcodecs.Imgcodecs; 9 import org.opencv.imgproc.Imgproc; 10 import org.opencv.test.OpenCVTestCase; 11 import org.opencv.test.OpenCVTestRunner; 12 13 import android.graphics.Bitmap; 14 import android.graphics.BitmapFactory; 15 import android.graphics.Color; 16 import android.util.Log; 17 18 public class UtilsTest extends OpenCVTestCase { 19 20 public void testBitmapToMat() { 21 BitmapFactory.Options opt16 = new BitmapFactory.Options(); 22 opt16.inPreferredConfig = Bitmap.Config.RGB_565; 23 Bitmap bmp16 = BitmapFactory.decodeFile(OpenCVTestRunner.LENA_PATH, opt16); 24 Mat m16 = new Mat(); 25 Utils.bitmapToMat(bmp16, m16); 26 assertTrue(m16.rows() == 512 && m16.cols() == 512 && m16.type() == CvType.CV_8UC4); 27 28 /*BitmapFactory.Options opt32 = new BitmapFactory.Options(); 29 opt32.inPreferredConfig = Bitmap.Config.ARGB_8888; 30 Bitmap bmp32 = BitmapFactory.decodeFile(OpenCVTestRunner.LENA_PATH, opt32);*/ 31 Bitmap bmp32 = bmp16.copy(Bitmap.Config.ARGB_8888, false); 32 Mat m32 = new Mat(); 33 Utils.bitmapToMat(bmp32, m32); 34 35 assertTrue(m16.rows() == m32.rows() && m16.cols() == m32.cols() && m16.type() == m32.type()); 36 37 double maxDiff = Core.norm(m16, m32, Core.NORM_INF); 38 Log.d("Bmp->Mat", "bmp16->Mat vs bmp32->Mat diff = " + maxDiff); 39 40 assertTrue(maxDiff <= 8 /* 8 == 2^8 / 2^5 */); 41 } 42 43 public void testExportResourceContextInt() { 44 fail("Not yet implemented"); 45 } 46 47 public void testExportResourceContextIntString() { 48 fail("Not yet implemented"); 49 } 50 51 public void testLoadResourceContextInt() { 52 fail("Not yet implemented"); 53 } 54 55 public void testLoadResourceContextIntInt() { 56 fail("Not yet implemented"); 57 } 58 59 public void testMatToBitmap() { 60 Mat imgBGR = Imgcodecs.imread( OpenCVTestRunner.LENA_PATH ); 61 assertTrue(imgBGR != null && !imgBGR.empty() && imgBGR.channels() == 3); 62 63 Mat m16 = new Mat(imgBGR.rows(), imgBGR.cols(), CvType.CV_8UC4); 64 Mat m32 = new Mat(imgBGR.rows(), imgBGR.cols(), CvType.CV_8UC4); 65 66 Bitmap bmp16 = Bitmap.createBitmap(imgBGR.cols(), imgBGR.rows(), Bitmap.Config.RGB_565); 67 Bitmap bmp32 = Bitmap.createBitmap(imgBGR.cols(), imgBGR.rows(), Bitmap.Config.ARGB_8888); 68 69 double maxDiff; 70 Scalar s0 = new Scalar(0); 71 Scalar s255 = Scalar.all(255); 72 73 74 // RGBA 75 Mat imgRGBA = new Mat(); 76 Imgproc.cvtColor(imgBGR, imgRGBA, Imgproc.COLOR_BGR2RGBA); 77 assertTrue(!imgRGBA.empty() && imgRGBA.channels() == 4); 78 79 bmp16.eraseColor(Color.BLACK); m16.setTo(s0); 80 Utils.matToBitmap(imgRGBA, bmp16); Utils.bitmapToMat(bmp16, m16); 81 maxDiff = Core.norm(imgRGBA, m16, Core.NORM_INF); 82 Log.d("RGBA->bmp16->RGBA", "maxDiff = " + maxDiff); 83 assertTrue(maxDiff <= 8 /* 8 == 2^8 / 2^5 */); 84 85 bmp32.eraseColor(Color.WHITE); m32.setTo(s255); 86 Utils.matToBitmap(imgRGBA, bmp32); Utils.bitmapToMat(bmp32, m32); 87 maxDiff = Core.norm(imgRGBA, m32, Core.NORM_INF); 88 Log.d("RGBA->bmp32->RGBA", "maxDiff = " + maxDiff); 89 assertTrue(maxDiff == 0); 90 91 92 // RGB 93 Mat imgRGB = new Mat(); 94 Imgproc.cvtColor(imgBGR, imgRGB, Imgproc.COLOR_BGR2RGB); 95 assertTrue(!imgRGB.empty() && imgRGB.channels() == 3); 96 97 bmp16.eraseColor(Color.BLACK); m16.setTo(s0); 98 Utils.matToBitmap(imgRGB, bmp16); Utils.bitmapToMat(bmp16, m16); 99 maxDiff = Core.norm(imgRGBA, m16, Core.NORM_INF); 100 Log.d("RGB->bmp16->RGBA", "maxDiff = " + maxDiff); 101 assertTrue(maxDiff <= 8 /* 8 == 2^8 / 2^5 */); 102 103 bmp32.eraseColor(Color.WHITE); m32.setTo(s255); 104 Utils.matToBitmap(imgRGB, bmp32); Utils.bitmapToMat(bmp32, m32); 105 maxDiff = Core.norm(imgRGBA, m32, Core.NORM_INF); 106 Log.d("RGB->bmp32->RGBA", "maxDiff = " + maxDiff); 107 assertTrue(maxDiff == 0); 108 109 110 // Gray 111 Mat imgGray = new Mat(); 112 Imgproc.cvtColor(imgBGR, imgGray, Imgproc.COLOR_BGR2GRAY); 113 assertTrue(!imgGray.empty() && imgGray.channels() == 1); 114 Mat tmp = new Mat(); 115 116 bmp16.eraseColor(Color.BLACK); m16.setTo(s0); 117 Utils.matToBitmap(imgGray, bmp16); Utils.bitmapToMat(bmp16, m16); 118 Core.extractChannel(m16, tmp, 0); 119 maxDiff = Core.norm(imgGray, tmp, Core.NORM_INF); 120 Log.d("Gray->bmp16->RGBA", "maxDiff = " + maxDiff); 121 assertTrue(maxDiff <= 8 /* 8 == 2^8 / 2^5 */); 122 123 bmp32.eraseColor(Color.WHITE); m32.setTo(s255); 124 Utils.matToBitmap(imgGray, bmp32); Utils.bitmapToMat(bmp32, m32); 125 tmp.setTo(s0); 126 Core.extractChannel(m32, tmp, 0); 127 maxDiff = Core.norm(imgGray, tmp, Core.NORM_INF); 128 Log.d("Gray->bmp32->RGBA", "maxDiff = " + maxDiff); 129 assertTrue(maxDiff == 0); 130 131 } 132 133 public void testAlphaPremultiplication() { 134 final int size = 256; 135 Bitmap bmp = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888); 136 Mat mOrig = new Mat(size, size, CvType.CV_8UC4); 137 Mat mUnPre = new Mat(size, size, CvType.CV_8UC4); 138 for(int y=0; y<size; y++) { 139 int a = y; 140 for(int x=0; x<size; x++) { 141 int color = Color.argb(a, 0, x, y); 142 bmp.setPixel(x, y, color); 143 mOrig.put(y, x, Color.red(color), Color.green(color), Color.blue(color), Color.alpha(color)); 144 int colorUnPre = bmp.getPixel(x, y); 145 mUnPre.put(y, x, Color.red(colorUnPre), Color.green(colorUnPre), Color.blue(colorUnPre), Color.alpha(colorUnPre)); 146 } 147 } 148 149 // Bitmap -> Mat 150 Mat m1 = new Mat(); 151 Mat m2 = new Mat(); 152 153 Utils.bitmapToMat(bmp, m1, false); 154 Imgproc.cvtColor(mOrig, m2, Imgproc.COLOR_RGBA2mRGBA); 155 assertMatEqual(m1, m2, 1.1); 156 157 Utils.bitmapToMat(bmp, m1, true); 158 assertMatEqual(m1, mUnPre, 1.1); 159 160 // Mat -> Bitmap 161 Bitmap bmp1 = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888); 162 163 Utils.matToBitmap(mOrig, bmp1, true); 164 Utils.bitmapToMat(bmp1, m1, true); 165 //assertMatEqual(m1, mUnPre, 1.1); 166 Mat diff = new Mat(); 167 Core.absdiff(m1, mUnPre, diff); 168 int numDiff = Core.countNonZero(diff.reshape(1)); 169 assertTrue(numDiff < size * 4); 170 } 171 172 } 173