MatTest.java 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325
  1. package org.opencv.test.core;
  2. import java.util.Arrays;
  3. import java.nio.ByteBuffer;
  4. import org.opencv.core.Core;
  5. import org.opencv.core.CvException;
  6. import org.opencv.core.CvType;
  7. import org.opencv.core.Mat;
  8. import org.opencv.core.Point;
  9. import org.opencv.core.Range;
  10. import org.opencv.core.Rect;
  11. import org.opencv.core.Scalar;
  12. import org.opencv.core.Size;
  13. import org.opencv.test.OpenCVTestCase;
  14. public class MatTest extends OpenCVTestCase {
  15. public void testAdjustROI() {
  16. Mat roi = gray0.submat(3, 5, 7, 10);
  17. Mat originalroi = roi.clone();
  18. Mat adjusted = roi.adjustROI(2, 2, 2, 2);
  19. assertMatEqual(adjusted, roi);
  20. assertSizeEquals(new Size(5, 6), adjusted.size(), EPS);
  21. assertEquals(originalroi.type(), adjusted.type());
  22. assertTrue(adjusted.isSubmatrix());
  23. assertFalse(adjusted.isContinuous());
  24. Point offset = new Point();
  25. Size size = new Size();
  26. adjusted.locateROI(size, offset);
  27. assertPointEquals(new Point(5, 1), offset, EPS);
  28. assertSizeEquals(gray0.size(), size, EPS);
  29. }
  30. public void testAssignToMat() {
  31. gray0.assignTo(dst);
  32. assertMatEqual(gray0, dst);
  33. gray255.assignTo(dst);
  34. assertMatEqual(gray255, dst);
  35. }
  36. public void testAssignToMatInt() {
  37. gray255.assignTo(dst, CvType.CV_32F);
  38. assertMatEqual(gray255_32f, dst, EPS);
  39. }
  40. public void testChannels() {
  41. assertEquals(1, gray0.channels());
  42. assertEquals(3, rgbLena.channels());
  43. assertEquals(4, rgba0.channels());
  44. }
  45. public void testCheckVectorInt() {
  46. // ! returns N if the matrix is 1-channel (N x ptdim) or ptdim-channel
  47. // (1 x N) or (N x 1); negative number otherwise
  48. assertEquals(2, new Mat(2, 10, CvType.CV_8U).checkVector(10));
  49. assertEquals(2, new Mat(1, 2, CvType.CV_8UC(10)).checkVector(10));
  50. assertEquals(2, new Mat(2, 1, CvType.CV_8UC(10)).checkVector(10));
  51. assertEquals(10, new Mat(1, 10, CvType.CV_8UC2).checkVector(2));
  52. assertTrue(0 > new Mat().checkVector(0));
  53. assertTrue(0 > new Mat(10, 1, CvType.CV_8U).checkVector(10));
  54. assertTrue(0 > new Mat(10, 20, CvType.CV_8U).checkVector(10));
  55. }
  56. public void testCheckVectorIntInt() {
  57. assertEquals(2, new Mat(2, 10, CvType.CV_8U).checkVector(10, CvType.CV_8U));
  58. assertEquals(2, new Mat(1, 2, CvType.CV_8UC(10)).checkVector(10, CvType.CV_8U));
  59. assertEquals(2, new Mat(2, 1, CvType.CV_8UC(10)).checkVector(10, CvType.CV_8U));
  60. assertEquals(10, new Mat(1, 10, CvType.CV_8UC2).checkVector(2, CvType.CV_8U));
  61. assertTrue(0 > new Mat(2, 10, CvType.CV_8U).checkVector(10, CvType.CV_8S));
  62. assertTrue(0 > new Mat(1, 2, CvType.CV_8UC(10)).checkVector(10, CvType.CV_8S));
  63. assertTrue(0 > new Mat(2, 1, CvType.CV_8UC(10)).checkVector(10, CvType.CV_8S));
  64. assertTrue(0 > new Mat(1, 10, CvType.CV_8UC2).checkVector(10, CvType.CV_8S));
  65. }
  66. public void testCheckVectorIntIntBoolean() {
  67. Mat mm = new Mat(5, 1, CvType.CV_8UC(10));
  68. Mat roi = new Mat(5, 3, CvType.CV_8UC(10)).submat(1, 3, 2, 3);
  69. assertEquals(5, mm.checkVector(10, CvType.CV_8U, true));
  70. assertEquals(5, mm.checkVector(10, CvType.CV_8U, false));
  71. assertEquals(2, roi.checkVector(10, CvType.CV_8U, false));
  72. assertTrue(0 > roi.checkVector(10, CvType.CV_8U, true));
  73. }
  74. public void testClone() {
  75. dst = gray0.clone();
  76. assertMatEqual(gray0, dst);
  77. assertFalse(gray0.getNativeObjAddr() == dst.getNativeObjAddr());
  78. assertFalse(gray0.dataAddr() == dst.dataAddr());
  79. }
  80. public void testCol() {
  81. Mat col = gray0.col(0);
  82. assertEquals(1, col.cols());
  83. assertEquals(gray0.rows(), col.rows());
  84. }
  85. public void testColRangeIntInt() {
  86. Mat cols = gray0.colRange(0, gray0.cols() / 2);
  87. assertEquals(gray0.cols() / 2, cols.cols());
  88. assertEquals(gray0.rows(), cols.rows());
  89. }
  90. public void testColRangeRange() {
  91. Range range = new Range(0, 5);
  92. dst = gray0.colRange(range);
  93. truth = new Mat(10, 5, CvType.CV_8UC1, new Scalar(0.0));
  94. assertMatEqual(truth, dst);
  95. }
  96. public void testCols() {
  97. assertEquals(matSize, gray0.cols());
  98. }
  99. public void testConvertToMatInt() {
  100. gray255.convertTo(dst, CvType.CV_32F);
  101. truth = new Mat(matSize, matSize, CvType.CV_32F, new Scalar(255));
  102. assertMatEqual(truth, dst, EPS);
  103. }
  104. public void testConvertToMatIntDouble() {
  105. gray2.convertTo(dst, CvType.CV_16U, 2.0);
  106. truth = new Mat(matSize, matSize, CvType.CV_16U, new Scalar(4));
  107. assertMatEqual(truth, dst);
  108. }
  109. public void testConvertToMatIntDoubleDouble() {
  110. gray0_32f.convertTo(dst, CvType.CV_8U, 2.0, 4.0);
  111. truth = new Mat(matSize, matSize, CvType.CV_8U, new Scalar(4));
  112. assertMatEqual(truth, dst);
  113. }
  114. public void testCopyToMat() {
  115. rgbLena.copyTo(dst);
  116. assertMatEqual(rgbLena, dst);
  117. }
  118. public void testCopyToMatMat() {
  119. Mat src = new Mat(4, 4, CvType.CV_8U, new Scalar(5));
  120. Mat mask = makeMask(src.clone());
  121. src.copyTo(dst, mask);
  122. truth = new Mat(4, 4, CvType.CV_8U) {
  123. {
  124. put(0, 0, 0, 0, 5, 5);
  125. put(1, 0, 0, 0, 5, 5);
  126. put(2, 0, 0, 0, 5, 5);
  127. put(3, 0, 0, 0, 5, 5);
  128. }
  129. };
  130. assertMatEqual(truth, dst);
  131. }
  132. public void testCreateIntIntInt() {
  133. gray255.create(4, 5, CvType.CV_32F);
  134. assertEquals(4, gray255.rows());
  135. assertEquals(5, gray255.cols());
  136. assertEquals(CvType.CV_32F, gray255.type());
  137. }
  138. public void testCreateSizeInt() {
  139. Size size = new Size(5, 5);
  140. dst.create(size, CvType.CV_16U);
  141. assertEquals(5, dst.rows());
  142. assertEquals(5, dst.cols());
  143. assertEquals(CvType.CV_16U, dst.type());
  144. }
  145. public void testCreateIntArrayInt() {
  146. int[] dims = new int[] {5, 6, 7};
  147. dst.create(dims, CvType.CV_16U);
  148. assertEquals(5, dst.size(0));
  149. assertEquals(6, dst.size(1));
  150. assertEquals(7, dst.size(2));
  151. assertEquals(CvType.CV_16U, dst.type());
  152. }
  153. public void testCross() {
  154. Mat answer = new Mat(1, 3, CvType.CV_32F);
  155. answer.put(0, 0, 7.0, 1.0, -5.0);
  156. Mat cross = v1.cross(v2);
  157. assertMatEqual(answer, cross, EPS);
  158. }
  159. public void testDataAddr() {
  160. assertTrue(0 != gray0.dataAddr());
  161. assertEquals(0, new Mat().dataAddr());
  162. }
  163. public void testDepth() {
  164. assertEquals(CvType.CV_8U, gray0.depth());
  165. assertEquals(CvType.CV_32F, gray0_32f.depth());
  166. }
  167. public void testDiag() {
  168. dst = gray0.diag();
  169. truth = new Mat(10, 1, CvType.CV_8UC1, new Scalar(0));
  170. assertMatEqual(truth, dst);
  171. }
  172. public void testDiagInt() {
  173. dst = gray255.diag(2);
  174. truth = new Mat(8, 1, CvType.CV_8UC1, new Scalar(255));
  175. assertMatEqual(truth, dst);
  176. }
  177. public void testDiagMat() {
  178. Mat diagVector = new Mat(matSize, 1, CvType.CV_32F, new Scalar(1));
  179. dst = Mat.diag(diagVector);
  180. assertMatEqual(grayE_32f, dst, EPS);
  181. }
  182. public void testDiagMat_sqrMatrix() {
  183. try {
  184. dst = Mat.diag(gray255);
  185. } catch (CvException e) {
  186. // expected
  187. }
  188. }
  189. public void testDot() {
  190. double s = v1.dot(v2);
  191. assertEquals(11.0, s);
  192. }
  193. public void testDump() {
  194. assertEquals("[1, 3, 2]", v1.dump());
  195. }
  196. public void testElemSize() {
  197. assertEquals(Byte.SIZE / 8 * gray0.channels(), gray0.elemSize());
  198. assertEquals(Float.SIZE / 8 * gray0_32f.channels(), gray0_32f.elemSize());
  199. assertEquals(Byte.SIZE / 8 * rgbLena.channels(), rgbLena.elemSize());
  200. }
  201. public void testElemSize1() {
  202. assertEquals(Byte.SIZE / 8, gray255.elemSize1());
  203. assertEquals(Double.SIZE / 8, gray0_64f.elemSize1());
  204. assertEquals(Byte.SIZE / 8, rgbLena.elemSize1());
  205. }
  206. public void testEmpty() {
  207. assertTrue(dst.empty());
  208. assertFalse(gray0.empty());
  209. }
  210. public void testEyeIntIntInt() {
  211. Mat eye = Mat.eye(3, 3, CvType.CV_32FC1);
  212. assertMatEqual(eye, eye.inv(), EPS);
  213. }
  214. public void testEyeSizeInt() {
  215. Size size = new Size(5, 5);
  216. Mat eye = Mat.eye(size, CvType.CV_32S);
  217. assertEquals(5, Core.countNonZero(eye));
  218. }
  219. public Mat getTestMat(int size, int type) {
  220. Mat m = new Mat(size, size, type);
  221. final int ch = CvType.channels(type);
  222. double buff[] = new double[size*size * ch];
  223. for(int i=0; i<size; i++)
  224. for(int j=0; j<size; j++)
  225. for(int k=0; k<ch; k++) {
  226. buff[i*size*ch + j*ch + k] = 100*i + 10*j + k;
  227. }
  228. m.put(0, 0, buff);
  229. return m;
  230. }
  231. public void testGetIntInt_8U() {
  232. Mat m = getTestMat(5, CvType.CV_8UC2);
  233. // whole Mat
  234. assertTrue(Arrays.equals(new double[] {0, 1}, m.get(0, 0)));
  235. assertTrue(Arrays.equals(new double[] {240, 241}, m.get(2, 4)));
  236. assertTrue(Arrays.equals(new double[] {255, 255}, m.get(4, 4)));
  237. // sub-Mat
  238. Mat sm = m.submat(2, 4, 3, 5);
  239. assertTrue(Arrays.equals(new double[] {230, 231}, sm.get(0, 0)));
  240. assertTrue(Arrays.equals(new double[] {255, 255}, sm.get(1, 1)));
  241. }
  242. public void testGetIntInt_32S() {
  243. Mat m = getTestMat(5, CvType.CV_32SC3);
  244. // whole Mat
  245. assertTrue(Arrays.equals(new double[] {0, 1, 2}, m.get(0, 0)));
  246. assertTrue(Arrays.equals(new double[] {240, 241, 242}, m.get(2, 4)));
  247. assertTrue(Arrays.equals(new double[] {440, 441, 442}, m.get(4, 4)));
  248. // sub-Mat
  249. Mat sm = m.submat(2, 4, 3, 5);
  250. assertTrue(Arrays.equals(new double[] {230, 231, 232}, sm.get(0, 0)));
  251. assertTrue(Arrays.equals(new double[] {340, 341, 342}, sm.get(1, 1)));
  252. }
  253. public void testGetIntInt_64F() {
  254. Mat m = getTestMat(5, CvType.CV_64FC1);
  255. // whole Mat
  256. assertTrue(Arrays.equals(new double[] {0}, m.get(0, 0)));
  257. assertTrue(Arrays.equals(new double[] {240}, m.get(2, 4)));
  258. assertTrue(Arrays.equals(new double[] {440}, m.get(4, 4)));
  259. // sub-Mat
  260. Mat sm = m.submat(2, 4, 3, 5);
  261. assertTrue(Arrays.equals(new double[] {230}, sm.get(0, 0)));
  262. assertTrue(Arrays.equals(new double[] {340}, sm.get(1, 1)));
  263. }
  264. public void testGetIntIntByteArray() {
  265. Mat m = getTestMat(5, CvType.CV_8UC3);
  266. byte[] goodData = new byte[9];
  267. byte[] badData = new byte[7];
  268. // whole Mat
  269. int bytesNum = m.get(1, 1, goodData);
  270. assertEquals(9, bytesNum);
  271. assertTrue(Arrays.equals(new byte[] { 110, 111, 112, 120, 121, 122, (byte) 130, (byte) 131, (byte) 132 }, goodData));
  272. try {
  273. m.get(2, 2, badData);
  274. fail("Expected UnsupportedOperationException (data.length % CvType.channels(t) != 0)");
  275. } catch (UnsupportedOperationException e) {
  276. // expected
  277. }
  278. // sub-Mat
  279. Mat sm = m.submat(2, 4, 3, 5);
  280. byte buff00[] = new byte[3];
  281. bytesNum = sm.get(0, 0, buff00);
  282. assertEquals(3, bytesNum);
  283. assertTrue(Arrays.equals(new byte[] {(byte) 230, (byte) 231, (byte) 232}, buff00));
  284. byte buff11[] = new byte[3];
  285. bytesNum = sm.get(1, 1, buff11);
  286. assertEquals(3, bytesNum);
  287. assertTrue(Arrays.equals(new byte[] {(byte) 255, (byte) 255, (byte) 255}, buff11));
  288. }
  289. public void testGetIntIntDoubleArray() {
  290. Mat m = getTestMat(5, CvType.CV_64F);
  291. double buff[] = new double[4];
  292. // whole Mat
  293. int bytesNum = m.get(1, 1, buff);
  294. assertEquals(32, bytesNum);
  295. assertTrue(Arrays.equals(new double[] { 110, 120, 130, 140 }, buff));
  296. // sub-Mat
  297. Mat sm = m.submat(2, 4, 3, 5);
  298. double buff00[] = new double[2];
  299. bytesNum = sm.get(0, 0, buff00);
  300. assertEquals(16, bytesNum);
  301. assertTrue(Arrays.equals(new double[] {230, 240}, buff00));
  302. double buff11[] = new double[] {0, 0};
  303. bytesNum = sm.get(1, 1, buff11);
  304. assertEquals(8, bytesNum);
  305. assertTrue(Arrays.equals(new double[] {340, 0}, buff11));
  306. }
  307. public void testGetIntIntFloatArray() {
  308. Mat m = getTestMat(5, CvType.CV_32F);
  309. float buff[] = new float[4];
  310. // whole Mat
  311. int bytesNum = m.get(1, 1, buff);
  312. assertEquals(16, bytesNum);
  313. assertTrue(Arrays.equals(new float[] { 110, 120, 130, 140 }, buff));
  314. // sub-Mat
  315. Mat sm = m.submat(2, 4, 3, 5);
  316. float buff00[] = new float[2];
  317. bytesNum = sm.get(0, 0, buff00);
  318. assertEquals(8, bytesNum);
  319. assertTrue(Arrays.equals(new float[] {230, 240}, buff00));
  320. float buff11[] = new float[] {0, 0};
  321. bytesNum = sm.get(1, 1, buff11);
  322. assertEquals(4, bytesNum);
  323. assertTrue(Arrays.equals(new float[] {340, 0}, buff11));
  324. }
  325. public void testGetIntIntIntArray() {
  326. Mat m = getTestMat(5, CvType.CV_32SC2);
  327. int[] buff = new int[6];
  328. // whole Mat
  329. int bytesNum = m.get(1, 1, buff);
  330. assertEquals(24, bytesNum);
  331. assertTrue(Arrays.equals(new int[] { 110, 111, 120, 121, 130, 131 }, buff));
  332. // sub-Mat
  333. Mat sm = m.submat(2, 4, 3, 5);
  334. int buff00[] = new int[4];
  335. bytesNum = sm.get(0, 0, buff00);
  336. assertEquals(16, bytesNum);
  337. assertTrue(Arrays.equals(new int[] {230, 231, 240, 241}, buff00));
  338. int buff11[] = new int[]{0, 0, 0, 0};
  339. bytesNum = sm.get(1, 1, buff11);
  340. assertEquals(8, bytesNum);
  341. assertTrue(Arrays.equals(new int[] {340, 341, 0, 0}, buff11));
  342. }
  343. public void testGetIntIntShortArray() {
  344. Mat m = getTestMat(5, CvType.CV_16SC2);
  345. short[] buff = new short[6];
  346. // whole Mat
  347. int bytesNum = m.get(1, 1, buff);
  348. assertEquals(12, bytesNum);
  349. assertTrue(Arrays.equals(new short[] { 110, 111, 120, 121, 130, 131 }, buff));
  350. // sub-Mat
  351. Mat sm = m.submat(2, 4, 3, 5);
  352. short buff00[] = new short[4];
  353. bytesNum = sm.get(0, 0, buff00);
  354. assertEquals(8, bytesNum);
  355. assertTrue(Arrays.equals(new short[] {230, 231, 240, 241}, buff00));
  356. short buff11[] = new short[]{0, 0, 0, 0};
  357. bytesNum = sm.get(1, 1, buff11);
  358. assertEquals(4, bytesNum);
  359. assertTrue(Arrays.equals(new short[] {340, 341, 0, 0}, buff11));
  360. Mat m2 = new Mat(new int[]{ 5, 6, 8 }, CvType.CV_16S);
  361. short[] data = new short[(int)m2.total()];
  362. for (int i = 0; i < data.length; i++ ) {
  363. data[i] = (short)i;
  364. }
  365. m2.put(new int[] {0, 0, 0}, data);
  366. Mat matNonContinuous = m2.submat(new Range[]{new Range(1,4), new Range(2,5), new Range(3,6)});
  367. Mat matContinuous = matNonContinuous.clone();
  368. short[] outNonContinuous = new short[(int)matNonContinuous.total()];
  369. matNonContinuous.get(new int[] { 0, 0, 0 }, outNonContinuous);
  370. short[] outContinuous = new short[(int)matNonContinuous.total()];
  371. matContinuous.get(new int[] { 0, 0, 0 }, outContinuous);
  372. assertArrayEquals(outNonContinuous, outContinuous);
  373. Mat subMat2 = m2.submat(new Range[]{new Range(1,4), new Range(1,5), new Range(0,8)});
  374. Mat subMatClone2 = subMat2.clone();
  375. short[] outNonContinuous2 = new short[(int)subMat2.total()];
  376. subMat2.get(new int[] { 0, 1, 1 }, outNonContinuous2);
  377. short[] outContinuous2 = new short[(int)subMat2.total()];
  378. subMatClone2.get(new int[] { 0, 1, 1 }, outContinuous2);
  379. assertArrayEquals(outNonContinuous2, outContinuous2);
  380. }
  381. public void testGetNativeObjAddr() {
  382. assertTrue(0 != gray0.getNativeObjAddr());
  383. }
  384. public void testHeight() {
  385. assertEquals(gray0.rows(), gray0.height());
  386. assertEquals(rgbLena.rows(), rgbLena.height());
  387. assertEquals(rgba128.rows(), rgba128.height());
  388. }
  389. public void testInv() {
  390. dst = grayE_32f.inv();
  391. assertMatEqual(grayE_32f, dst, EPS);
  392. }
  393. public void testInvInt() {
  394. Mat src = new Mat(2, 2, CvType.CV_32F) {
  395. {
  396. put(0, 0, 1.0);
  397. put(0, 1, 2.0);
  398. put(1, 0, 1.5);
  399. put(1, 1, 4.0);
  400. }
  401. };
  402. dst = src.inv(Core.DECOMP_CHOLESKY);
  403. truth = new Mat(2, 2, CvType.CV_32F) {
  404. {
  405. put(0, 0, 4.0);
  406. put(0, 1, -2.0);
  407. put(1, 0, -1.5);
  408. put(1, 1, 1.0);
  409. }
  410. };
  411. assertMatEqual(truth, dst, EPS);
  412. }
  413. public void testIsContinuous() {
  414. assertTrue(gray0.isContinuous());
  415. Mat subMat = gray0.submat(0, gray0.rows() / 2, 0, gray0.cols() / 2);
  416. assertFalse(subMat.isContinuous());
  417. }
  418. public void testIsSubmatrix() {
  419. assertFalse(gray0.isSubmatrix());
  420. Mat subMat = gray0.submat(0, gray0.rows() / 2, 0, gray0.cols() / 2);
  421. assertTrue(subMat.isSubmatrix());
  422. }
  423. public void testLocateROI() {
  424. Mat roi = gray0.submat(3, 5, 7, 10);
  425. Point offset = new Point();
  426. Size size = new Size();
  427. roi.locateROI(size, offset);
  428. assertPointEquals(new Point(7, 3), offset, EPS);
  429. assertSizeEquals(new Size(10, 10), size, EPS);
  430. }
  431. public void testMat() {
  432. Mat m = new Mat();
  433. assertNotNull(m);
  434. assertTrue(m.empty());
  435. }
  436. public void testMatIntIntCvType() {
  437. Mat gray = new Mat(1, 1, CvType.CV_8UC1);
  438. assertFalse(gray.empty());
  439. Mat rgb = new Mat(1, 1, CvType.CV_8UC3);
  440. assertFalse(rgb.empty());
  441. }
  442. public void testMatIntIntCvTypeScalar() {
  443. dst = new Mat(gray127.rows(), gray127.cols(), CvType.CV_8U, new Scalar(127));
  444. assertFalse(dst.empty());
  445. assertMatEqual(dst, gray127);
  446. dst = new Mat(rgba128.rows(), rgba128.cols(), CvType.CV_8UC4, Scalar.all(128));
  447. assertFalse(dst.empty());
  448. assertMatEqual(dst, rgba128);
  449. }
  450. public void testMatIntIntInt() {
  451. Mat gray = new Mat(1, 1, CvType.CV_8U);
  452. assertFalse(gray.empty());
  453. Mat rgb = new Mat(1, 1, CvType.CV_8U);
  454. assertFalse(rgb.empty());
  455. }
  456. public void testMatIntIntIntScalar() {
  457. Mat m1 = new Mat(gray127.rows(), gray127.cols(), CvType.CV_8U, new Scalar(127));
  458. assertFalse(m1.empty());
  459. assertMatEqual(m1, gray127);
  460. Mat m2 = new Mat(gray0_32f.rows(), gray0_32f.cols(), CvType.CV_32F, new Scalar(0));
  461. assertFalse(m2.empty());
  462. assertMatEqual(m2, gray0_32f, EPS);
  463. }
  464. public void testMatMatRange() {
  465. dst = new Mat(gray0, new Range(0, 5));
  466. truth = new Mat(5, 10, CvType.CV_8UC1, new Scalar(0));
  467. assertFalse(dst.empty());
  468. assertMatEqual(truth, dst);
  469. }
  470. public void testMatMatRangeRange() {
  471. dst = new Mat(gray255_32f, new Range(0, 5), new Range(0, 5));
  472. truth = new Mat(5, 5, CvType.CV_32FC1, new Scalar(255));
  473. assertFalse(dst.empty());
  474. assertMatEqual(truth, dst, EPS);
  475. }
  476. public void testMatMatRangeArray() {
  477. dst = new Mat(gray255_32f_3d, new Range[]{new Range(0, 5), new Range(0, 5), new Range(0, 5)});
  478. truth = new Mat(new int[] {5, 5, 5}, CvType.CV_32FC1, new Scalar(255));
  479. assertFalse(dst.empty());
  480. assertMatEqual(truth, dst, EPS);
  481. }
  482. public void testMatMatRect() {
  483. Mat m = new Mat(7, 6, CvType.CV_32SC1);
  484. m.put(0, 0,
  485. 0, 1, 2, 3, 4, 5,
  486. 10, 11, 12, 13, 14, 15,
  487. 20, 21, 22, 23, 24, 25,
  488. 30, 31, 32, 33, 34, 35,
  489. 40, 41, 42, 43, 44, 45,
  490. 50, 51, 52, 53, 54, 55,
  491. 60, 61, 62, 63, 64, 65 );
  492. dst = new Mat(m, new Rect(1, 2, 3, 4));
  493. truth = new Mat(4, 3, CvType.CV_32SC1);
  494. truth.put(0, 0,
  495. 21, 22, 23,
  496. 31, 32, 33,
  497. 41, 42, 43,
  498. 51, 52, 53 );
  499. assertFalse(dst.empty());
  500. assertMatEqual(truth, dst);
  501. }
  502. public void testMatSizeInt() {
  503. dst = new Mat(new Size(10, 10), CvType.CV_8U);
  504. assertFalse(dst.empty());
  505. }
  506. public void testMatSizeIntScalar() {
  507. dst = new Mat(new Size(10, 10), CvType.CV_32F, new Scalar(255));
  508. assertFalse(dst.empty());
  509. assertMatEqual(gray255_32f, dst, EPS);
  510. }
  511. public void testMatIntArrayIntScalar() {
  512. dst = new Mat(new int[]{10, 10, 10}, CvType.CV_32F, new Scalar(255));
  513. assertFalse(dst.empty());
  514. assertMatEqual(gray255_32f_3d, dst, EPS);
  515. }
  516. public void testMulMat() {
  517. assertMatEqual(gray0, gray0.mul(gray255));
  518. Mat m1 = new Mat(2, 2, CvType.CV_32F, new Scalar(2));
  519. Mat m2 = new Mat(2, 2, CvType.CV_32F, new Scalar(3));
  520. dst = m1.mul(m2);
  521. truth = new Mat(2, 2, CvType.CV_32F, new Scalar(6));
  522. assertMatEqual(truth, dst, EPS);
  523. }
  524. public void testMulMat3d() {
  525. Mat m1 = new Mat(new int[] {2, 2, 2}, CvType.CV_32F, new Scalar(2));
  526. Mat m2 = new Mat(new int[] {2, 2, 2}, CvType.CV_32F, new Scalar(3));
  527. dst = m1.mul(m2);
  528. truth = new Mat(new int[] {2, 2, 2}, CvType.CV_32F, new Scalar(6));
  529. assertMatEqual(truth, dst, EPS);
  530. }
  531. public void testMulMatDouble() {
  532. Mat m1 = new Mat(2, 2, CvType.CV_32F, new Scalar(2));
  533. Mat m2 = new Mat(2, 2, CvType.CV_32F, new Scalar(3));
  534. dst = m1.mul(m2, 3.0);
  535. truth = new Mat(2, 2, CvType.CV_32F, new Scalar(18));
  536. assertMatEqual(truth, dst, EPS);
  537. }
  538. public void testMatMulMat() {
  539. Mat m1 = new Mat(2, 2, CvType.CV_32F, new Scalar(2));
  540. Mat m2 = new Mat(2, 2, CvType.CV_32F, new Scalar(3));
  541. dst = m1.matMul(m2);
  542. truth = new Mat(2, 2, CvType.CV_32F, new Scalar(12));
  543. assertMatEqual(truth, dst, EPS);
  544. }
  545. public void testOnesIntIntInt() {
  546. dst = Mat.ones(matSize, matSize, CvType.CV_32F);
  547. truth = new Mat(matSize, matSize, CvType.CV_32F, new Scalar(1));
  548. assertMatEqual(truth, dst, EPS);
  549. }
  550. public void testOnesSizeInt() {
  551. dst = Mat.ones(new Size(2, 2), CvType.CV_16S);
  552. truth = new Mat(2, 2, CvType.CV_16S, new Scalar(1));
  553. assertMatEqual(truth, dst);
  554. }
  555. public void testOnesIntArrayInt() {
  556. dst = Mat.ones(new int[]{2, 2, 2}, CvType.CV_16S);
  557. truth = new Mat(new int[]{2, 2, 2}, CvType.CV_16S, new Scalar(1));
  558. assertMatEqual(truth, dst);
  559. }
  560. public void testPush_back() {
  561. Mat m1 = new Mat(2, 4, CvType.CV_32F, new Scalar(2));
  562. Mat m2 = new Mat(3, 4, CvType.CV_32F, new Scalar(3));
  563. m1.push_back(m2);
  564. truth = new Mat(5, 4, CvType.CV_32FC1) {
  565. {
  566. put(0, 0, 2, 2, 2, 2);
  567. put(1, 0, 2, 2, 2, 2);
  568. put(2, 0, 3, 3, 3, 3);
  569. put(3, 0, 3, 3, 3, 3);
  570. put(4, 0, 3, 3, 3, 3);
  571. }
  572. };
  573. assertMatEqual(truth, m1, EPS);
  574. }
  575. public void testPutIntIntByteArray() {
  576. Mat m = new Mat(5, 5, CvType.CV_8UC3, new Scalar(1, 2, 3));
  577. Mat sm = m.submat(2, 4, 3, 5);
  578. byte[] buff = new byte[] { 0, 0, 0, 0, 0, 0 };
  579. byte[] buff0 = new byte[] { 10, 20, 30, 40, 50, 60 };
  580. byte[] buff1 = new byte[] { -1, -2, -3, -4, -5, -6 };
  581. int bytesNum = m.put(1, 2, buff0);
  582. assertEquals(6, bytesNum);
  583. bytesNum = m.get(1, 2, buff);
  584. assertEquals(6, bytesNum);
  585. assertTrue(Arrays.equals(buff, buff0));
  586. bytesNum = sm.put(0, 0, buff1);
  587. assertEquals(6, bytesNum);
  588. bytesNum = sm.get(0, 0, buff);
  589. assertEquals(6, bytesNum);
  590. assertTrue(Arrays.equals(buff, buff1));
  591. bytesNum = m.get(2, 3, buff);
  592. assertEquals(6, bytesNum);
  593. assertTrue(Arrays.equals(buff, buff1));
  594. Mat m1 = m.row(1);
  595. bytesNum = m1.get(0, 2, buff);
  596. assertEquals(6, bytesNum);
  597. assertTrue(Arrays.equals(buff, buff0));
  598. try {
  599. byte[] bytes2 = new byte[] { 10, 20, 30, 40, 50 };
  600. m.put(2, 2, bytes2);
  601. fail("Expected UnsupportedOperationException (data.length % CvType.channels(t) != 0)");
  602. } catch (UnsupportedOperationException e) {
  603. // expected
  604. }
  605. }
  606. public void testPutIntArrayByteArray() {
  607. Mat m = new Mat(new int[]{5, 5, 5}, CvType.CV_8UC3, new Scalar(1, 2, 3));
  608. Mat sm = m.submat(new Range[]{ new Range(0, 2), new Range(1, 3), new Range(2, 4)});
  609. byte[] buff = new byte[] { 0, 0, 0, 0, 0, 0 };
  610. byte[] buff0 = new byte[] { 10, 20, 30, 40, 50, 60 };
  611. byte[] buff1 = new byte[] { -1, -2, -3, -4, -5, -6 };
  612. int bytesNum = m.put(new int[]{1, 2, 0}, buff0);
  613. assertEquals(6, bytesNum);
  614. bytesNum = m.get(new int[]{1, 2, 0}, buff);
  615. assertEquals(6, bytesNum);
  616. assertTrue(Arrays.equals(buff, buff0));
  617. bytesNum = sm.put(new int[]{0, 0, 0}, buff1);
  618. assertEquals(6, bytesNum);
  619. bytesNum = sm.get(new int[]{0, 0, 0}, buff);
  620. assertEquals(6, bytesNum);
  621. assertTrue(Arrays.equals(buff, buff1));
  622. bytesNum = m.get(new int[]{0, 1, 2}, buff);
  623. assertEquals(6, bytesNum);
  624. assertTrue(Arrays.equals(buff, buff1));
  625. Mat m1 = m.submat(new Range[]{ new Range(1,2), Range.all(), Range.all() });
  626. bytesNum = m1.get(new int[]{ 0, 2, 0}, buff);
  627. assertEquals(6, bytesNum);
  628. assertTrue(Arrays.equals(buff, buff0));
  629. try {
  630. byte[] bytes2 = new byte[] { 10, 20, 30, 40, 50 };
  631. m.put(new int[]{ 2, 2, 0 }, bytes2);
  632. fail("Expected UnsupportedOperationException (data.length % CvType.channels(t) != 0)");
  633. } catch (UnsupportedOperationException e) {
  634. // expected
  635. }
  636. }
  637. public void testPutIntIntDoubleArray() {
  638. Mat m = new Mat(5, 5, CvType.CV_8UC3, new Scalar(1, 2, 3));
  639. Mat sm = m.submat(2, 4, 3, 5);
  640. byte[] buff = new byte[] { 0, 0, 0, 0, 0, 0 };
  641. int bytesNum = m.put(1, 2, 10, 20, 30, 40, 50, 60);
  642. assertEquals(6, bytesNum);
  643. bytesNum = m.get(1, 2, buff);
  644. assertEquals(6, bytesNum);
  645. assertTrue(Arrays.equals(buff, new byte[]{10, 20, 30, 40, 50, 60}));
  646. bytesNum = sm.put(0, 0, 255, 254, 253, 252, 251, 250);
  647. assertEquals(6, bytesNum);
  648. bytesNum = sm.get(0, 0, buff);
  649. assertEquals(6, bytesNum);
  650. assertTrue(Arrays.equals(buff, new byte[]{-1, -2, -3, -4, -5, -6}));
  651. bytesNum = m.get(2, 3, buff);
  652. assertEquals(6, bytesNum);
  653. assertTrue(Arrays.equals(buff, new byte[]{-1, -2, -3, -4, -5, -6}));
  654. }
  655. public void testPutIntArrayDoubleArray() {
  656. Mat m = new Mat(new int[]{5, 5, 5}, CvType.CV_8UC3, new Scalar(1, 2, 3));
  657. Mat sm = m.submat(new Range[]{ new Range(0, 2), new Range(1, 3), new Range(2, 4)});
  658. byte[] buff = new byte[] { 0, 0, 0, 0, 0, 0 };
  659. int bytesNum = m.put(new int[]{1, 2, 0}, 10, 20, 30, 40, 50, 60);
  660. assertEquals(6, bytesNum);
  661. bytesNum = m.get(new int[]{1, 2, 0}, buff);
  662. assertEquals(6, bytesNum);
  663. assertTrue(Arrays.equals(buff, new byte[]{10, 20, 30, 40, 50, 60}));
  664. bytesNum = sm.put(new int[]{0, 0, 0}, 255, 254, 253, 252, 251, 250);
  665. assertEquals(6, bytesNum);
  666. bytesNum = sm.get(new int[]{0, 0, 0}, buff);
  667. assertEquals(6, bytesNum);
  668. assertTrue(Arrays.equals(buff, new byte[]{-1, -2, -3, -4, -5, -6}));
  669. bytesNum = m.get(new int[]{0, 1, 2}, buff);
  670. assertEquals(6, bytesNum);
  671. assertTrue(Arrays.equals(buff, new byte[]{-1, -2, -3, -4, -5, -6}));
  672. }
  673. public void testPutIntIntFloatArray() {
  674. Mat m = new Mat(5, 5, CvType.CV_32FC3, new Scalar(1, 2, 3));
  675. float[] elements = new float[] { 10, 20, 30, 40, 50, 60 };
  676. int bytesNum = m.put(4, 3, elements);
  677. assertEquals(elements.length * 4, bytesNum);
  678. Mat m1 = m.row(4);
  679. float buff[] = new float[3];
  680. bytesNum = m1.get(0, 4, buff);
  681. assertEquals(buff.length * 4, bytesNum);
  682. assertTrue(Arrays.equals(new float[]{40, 50, 60}, buff));
  683. assertArrayEquals(new double[]{10, 20, 30}, m.get(4, 3), EPS);
  684. try {
  685. float[] elements2 = new float[] { 10, 20, 30, 40, 50 };
  686. m.put(2, 2, elements2);
  687. fail("Expected UnsupportedOperationException (data.length % CvType.channels(t) != 0)");
  688. } catch (UnsupportedOperationException e) {
  689. // expected
  690. }
  691. }
  692. public void testPutIntArrayFloatArray() {
  693. Mat m = new Mat(new int[]{5, 5, 5}, CvType.CV_32FC3, new Scalar(1, 2, 3));
  694. float[] elements = new float[] { 10, 20, 30, 40, 50, 60 };
  695. int bytesNum = m.put(new int[]{0, 4, 3}, elements);
  696. assertEquals(elements.length * 4, bytesNum);
  697. Mat m1 = m.submat(new Range[]{ Range.all(), new Range(4, 5), Range.all() });
  698. float buff[] = new float[3];
  699. bytesNum = m1.get(new int[]{ 0, 0, 4 }, buff);
  700. assertEquals(buff.length * 4, bytesNum);
  701. assertTrue(Arrays.equals(new float[]{40, 50, 60}, buff));
  702. assertArrayEquals(new double[]{10, 20, 30}, m.get(new int[]{ 0, 4, 3 }), EPS);
  703. try {
  704. float[] elements2 = new float[] { 10, 20, 30, 40, 50 };
  705. m.put(new int[]{4, 2, 2}, elements2);
  706. fail("Expected UnsupportedOperationException (data.length % CvType.channels(t) != 0)");
  707. } catch (UnsupportedOperationException e) {
  708. // expected
  709. }
  710. }
  711. public void testPutIntIntIntArray() {
  712. Mat m = new Mat(5, 5, CvType.CV_32SC3, new Scalar(-1, -2, -3));
  713. int[] elements = new int[] { 10, 20, 30, 40, 50, 60 };
  714. int bytesNum = m.put(0, 4, elements);
  715. assertEquals(elements.length * 4, bytesNum);
  716. Mat m1 = m.col(4);
  717. int buff[] = new int[3];
  718. bytesNum = m1.get(0, 0, buff);
  719. assertEquals(buff.length * 4, bytesNum);
  720. assertTrue(Arrays.equals(new int[]{10, 20, 30}, buff));
  721. assertArrayEquals(new double[]{40, 50, 60}, m.get(1, 0), EPS);
  722. try {
  723. int[] elements2 = new int[] { 10, 20, 30, 40, 50 };
  724. m.put(2, 2, elements2);
  725. fail("Expected UnsupportedOperationException (data.length % CvType.channels(t) != 0)");
  726. } catch (UnsupportedOperationException e) {
  727. // expected
  728. }
  729. }
  730. public void testPutIntArrayIntArray() {
  731. Mat m = new Mat(new int[]{5, 5, 5}, CvType.CV_32SC3, new Scalar(-1, -2, -3));
  732. int[] elements = new int[] { 10, 20, 30, 40, 50, 60 };
  733. int bytesNum = m.put(new int[]{ 0, 0, 4 }, elements);
  734. assertEquals(elements.length * 4, bytesNum);
  735. Mat m1 = m.submat(new Range[]{ Range.all(), Range.all(), new Range(4, 5)});
  736. int buff[] = new int[3];
  737. bytesNum = m1.get(new int[]{ 0, 0, 0 }, buff);
  738. assertEquals(buff.length * 4, bytesNum);
  739. assertTrue(Arrays.equals(new int[]{ 10, 20, 30 }, buff));
  740. assertArrayEquals(new double[]{ 40, 50, 60 }, m.get(new int[]{ 0, 1, 0 }), EPS);
  741. try {
  742. int[] elements2 = new int[] { 10, 20, 30, 40, 50 };
  743. m.put(new int[] { 2, 2, 0 }, elements2);
  744. fail("Expected UnsupportedOperationException (data.length % CvType.channels(t) != 0)");
  745. } catch (UnsupportedOperationException e) {
  746. // expected
  747. }
  748. }
  749. public void testPutIntIntShortArray() {
  750. Mat m = new Mat(5, 5, CvType.CV_16SC3, new Scalar(-1, -2, -3));
  751. short[] elements = new short[] { 10, 20, 30, 40, 50, 60 };
  752. int bytesNum = m.put(2, 3, elements);
  753. assertEquals(elements.length * 2, bytesNum);
  754. Mat m1 = m.col(3);
  755. short buff[] = new short[3];
  756. bytesNum = m1.get(2, 0, buff);
  757. assertTrue(Arrays.equals(new short[]{10, 20, 30}, buff));
  758. assertArrayEquals(new double[]{40, 50, 60}, m.get(2, 4), EPS);
  759. try {
  760. short[] elements2 = new short[] { 10, 20, 30, 40, 50 };
  761. m.put(2, 2, elements2);
  762. fail("Expected UnsupportedOperationException (data.length % CvType.channels(t) != 0)");
  763. } catch (UnsupportedOperationException e) {
  764. // expected
  765. }
  766. }
  767. public void testPutIntArrayShortArray() {
  768. Mat m = new Mat(new int[]{ 5, 5, 5}, CvType.CV_16SC3, new Scalar(-1, -2, -3));
  769. short[] elements = new short[] { 10, 20, 30, 40, 50, 60 };
  770. int bytesNum = m.put(new int[]{ 0, 2, 3 }, elements);
  771. assertEquals(elements.length * 2, bytesNum);
  772. Mat m1 = m.submat(new Range[]{ Range.all(), Range.all(), new Range(3, 4)});
  773. short buff[] = new short[3];
  774. bytesNum = m1.get(new int[]{ 0, 2, 0 }, buff);
  775. assertTrue(Arrays.equals(new short[]{10, 20, 30}, buff));
  776. assertArrayEquals(new double[]{40, 50, 60}, m.get(new int[]{ 0, 2, 4 }), EPS);
  777. try {
  778. short[] elements2 = new short[] { 10, 20, 30, 40, 50 };
  779. m.put(new int[] { 2, 2, 0 }, elements2);
  780. fail("Expected UnsupportedOperationException (data.length % CvType.channels(t) != 0)");
  781. } catch (UnsupportedOperationException e) {
  782. // expected
  783. }
  784. }
  785. public void testRelease() {
  786. assertFalse(gray0.empty());
  787. assertTrue(gray0.rows() > 0);
  788. gray0.release();
  789. assertTrue(gray0.empty());
  790. assertEquals(0, gray0.rows());
  791. assertEquals(0, gray0.dataAddr());
  792. }
  793. public void testReshapeInt() {
  794. Mat src = new Mat(4, 4, CvType.CV_8U, new Scalar(0));
  795. dst = src.reshape(4);
  796. truth = new Mat(4, 1, CvType.CV_8UC4, new Scalar(0));
  797. assertMatEqual(truth, dst);
  798. }
  799. public void testReshapeIntInt() {
  800. Mat src = new Mat(5, 7, CvType.CV_8U, new Scalar(0));
  801. dst = src.reshape(7, 5);
  802. truth = new Mat(5, 1, CvType.CV_8UC(7), new Scalar(0));
  803. assertMatEqual(truth, dst);
  804. }
  805. public void testReshapeIntIntArray() {
  806. // 2D -> 4D
  807. Mat src = new Mat(6, 5, CvType.CV_8UC3, new Scalar(0));
  808. assertEquals(2, src.dims());
  809. assertEquals(src.rows(), src.size(0));
  810. assertEquals(src.cols(), src.size(1));
  811. int[] newShape = {1, src.channels() * src.cols(), 1, src.rows()};
  812. dst = src.reshape(1, newShape);
  813. assertEquals(newShape.length, dst.dims());
  814. for (int i = 0; i < newShape.length; ++i)
  815. assertEquals(newShape[i], dst.size(i));
  816. // 3D -> 2D
  817. src = new Mat(new int[]{4, 6, 7}, CvType.CV_8UC3, new Scalar(0));
  818. assertEquals(3, src.dims());
  819. assertEquals(4, src.size(0));
  820. assertEquals(6, src.size(1));
  821. assertEquals(7, src.size(2));
  822. int[] newShape2 = {src.channels() * src.size(2), src.size(0) * src.size(1)};
  823. dst = src.reshape(1, newShape2);
  824. assertEquals(newShape2.length, dst.dims());
  825. for (int i = 0; i < newShape2.length; ++i)
  826. assertEquals(newShape2[i], dst.size(i));
  827. }
  828. public void testCopySize() {
  829. Mat src = new Mat(new int[]{1, 1, 10, 10}, CvType.CV_8UC1, new Scalar(1));
  830. assertEquals(4, src.dims());
  831. assertEquals(1, src.size(0));
  832. assertEquals(1, src.size(1));
  833. assertEquals(10, src.size(2));
  834. assertEquals(10, src.size(3));
  835. Mat other = new Mat(new int[]{10, 10}, src.type());
  836. src.copySize(other);
  837. assertEquals(other.dims(), src.dims());
  838. for (int i = 0; i < other.dims(); ++i)
  839. assertEquals(other.size(i), src.size(i));
  840. }
  841. public void testRow() {
  842. Mat row = gray0.row(0);
  843. assertEquals(1, row.rows());
  844. assertEquals(gray0.cols(), row.cols());
  845. }
  846. public void testRowRangeIntInt() {
  847. Mat rows = gray0.rowRange(0, gray0.rows() / 2);
  848. assertEquals(gray0.rows() / 2, rows.rows());
  849. assertEquals(gray0.cols(), rows.cols());
  850. }
  851. public void testRowRangeRange() {
  852. Mat rows = gray255.rowRange(new Range(0, 5));
  853. assertEquals(gray255.rows() / 2, rows.rows());
  854. assertEquals(gray255.cols(), rows.cols());
  855. }
  856. public void testRows() {
  857. assertEquals(matSize, gray0.rows());
  858. }
  859. public void testSetToMat() {
  860. Mat vals = new Mat(7, 1, CvType.CV_8U) {
  861. {
  862. put(0, 0, 1, 2, 3, 4, 5, 6, 7);
  863. }
  864. };
  865. Mat dst = new Mat(1, 1, CvType.CV_8UC(7));
  866. dst.setTo(vals);
  867. Mat truth = new Mat(1, 1, CvType.CV_8UC(7)) {
  868. {
  869. put(0, 0, 1, 2, 3, 4, 5, 6, 7);
  870. }
  871. };
  872. assertMatEqual(truth, dst);
  873. }
  874. public void testSetToMatMat() {
  875. Mat vals = new Mat(7, 1, CvType.CV_8U) {
  876. {
  877. put(0, 0, 1, 2, 3, 4, 5, 6, 7);
  878. }
  879. };
  880. Mat dst = Mat.zeros(2, 1, CvType.CV_8UC(7));
  881. Mat mask = new Mat(2, 1, CvType.CV_8U) {
  882. {
  883. put(0, 0, 0, 1);
  884. }
  885. };
  886. dst.setTo(vals, mask);
  887. Mat truth = new Mat(2, 1, CvType.CV_8UC(7)) {
  888. {
  889. put(0, 0, 0, 0, 0, 0, 0, 0, 0);
  890. put(1, 0, 1, 2, 3, 4, 5, 6, 7);
  891. }
  892. };
  893. assertMatEqual(truth, dst);
  894. }
  895. public void testSetToScalar() {
  896. gray0.setTo(new Scalar(127));
  897. assertMatEqual(gray127, gray0);
  898. }
  899. public void testSetToScalarMask() {
  900. Mat mask = gray0.clone();
  901. mask.put(1, 1, 1, 2, 3);
  902. gray0.setTo(new Scalar(1), mask);
  903. assertEquals(3, Core.countNonZero(gray0));
  904. Core.subtract(gray0, mask, gray0);
  905. assertEquals(0, Core.countNonZero(gray0));
  906. }
  907. public void testSize() {
  908. assertEquals(new Size(matSize, matSize), gray0.size());
  909. assertEquals(new Size(3, 1), v1.size());
  910. }
  911. public void testStep1() {
  912. assertEquals(matSize * CvType.channels(CvType.CV_8U), gray0.step1());
  913. assertEquals(3, v2.step1());
  914. }
  915. public void testStep1Int() {
  916. Mat roi = rgba0.submat(3, 5, 7, 10);
  917. Mat m = roi.clone();
  918. assertTrue(rgba0.channels() * rgba0.cols() <= roi.step1(0));
  919. assertEquals(rgba0.channels(), roi.step1(1));
  920. assertTrue(m.channels() * (10 - 7) <= m.step1(0));
  921. assertEquals(m.channels(), m.step1(1));
  922. }
  923. public void testSubmatIntIntIntInt() {
  924. Mat submat = gray0.submat(0, gray0.rows() / 2, 0, gray0.cols() / 2);
  925. assertTrue(submat.isSubmatrix());
  926. assertFalse(submat.isContinuous());
  927. assertEquals(gray0.rows() / 2, submat.rows());
  928. assertEquals(gray0.cols() / 2, submat.cols());
  929. }
  930. public void testSubmatRangeRange() {
  931. Mat submat = gray255.submat(new Range(2, 4), new Range(2, 4));
  932. assertTrue(submat.isSubmatrix());
  933. assertFalse(submat.isContinuous());
  934. assertEquals(2, submat.rows());
  935. assertEquals(2, submat.cols());
  936. }
  937. public void testSubmatRangeArray() {
  938. Mat submat = gray255_32f_3d.submat(new Range[]{ new Range(2, 4), new Range(2, 4), new Range(3, 6) });
  939. assertTrue(submat.isSubmatrix());
  940. assertFalse(submat.isContinuous());
  941. assertEquals(2, submat.size(0));
  942. assertEquals(2, submat.size(1));
  943. assertEquals(3, submat.size(2));
  944. }
  945. public void testSubmatRect() {
  946. Mat submat = gray255.submat(new Rect(5, 5, gray255.cols() / 2, gray255.rows() / 2));
  947. assertTrue(submat.isSubmatrix());
  948. assertFalse(submat.isContinuous());
  949. assertEquals(gray255.rows() / 2, submat.rows());
  950. assertEquals(gray255.cols() / 2, submat.cols());
  951. }
  952. public void testT() {
  953. assertMatEqual(gray255, gray255.t());
  954. Mat src = new Mat(3, 3, CvType.CV_16U) {
  955. {
  956. put(0, 0, 1, 2, 4);
  957. put(1, 0, 7, 5, 0);
  958. put(2, 0, 3, 4, 6);
  959. }
  960. };
  961. dst = src.t();
  962. truth = new Mat(3, 3, CvType.CV_16U) {
  963. {
  964. put(0, 0, 1, 7, 3);
  965. put(1, 0, 2, 5, 4);
  966. put(2, 0, 4, 0, 6);
  967. }
  968. };
  969. assertMatEqual(truth, dst);
  970. }
  971. public void testToString() {
  972. assertNotNull(gray0.toString());
  973. }
  974. public void testTotal() {
  975. int nElements = gray0.rows() * gray0.cols();
  976. assertEquals(nElements, gray0.total());
  977. }
  978. public void testType() {
  979. assertEquals(CvType.CV_8UC1, gray0.type());
  980. assertEquals(CvType.CV_32FC1, gray0_32f.type());
  981. assertEquals(CvType.CV_8UC3, rgbLena.type());
  982. }
  983. public void testWidth() {
  984. assertEquals(gray0.cols(), gray0.width());
  985. assertEquals(rgbLena.cols(), rgbLena.width());
  986. assertEquals(rgba128.cols(), rgba128.width());
  987. }
  988. public void testZerosIntIntInt() {
  989. dst = Mat.zeros(matSize, matSize, CvType.CV_32F);
  990. assertMatEqual(gray0_32f, dst, EPS);
  991. }
  992. public void testZerosSizeInt() {
  993. dst = Mat.zeros(new Size(2, 2), CvType.CV_16S);
  994. truth = new Mat(2, 2, CvType.CV_16S, new Scalar(0));
  995. assertMatEqual(truth, dst);
  996. }
  997. public void testZerosIntArray() {
  998. dst = Mat.zeros(new int[]{2, 3, 4}, CvType.CV_16S);
  999. truth = new Mat(new int[]{2, 3, 4}, CvType.CV_16S, new Scalar(0));
  1000. assertMatEqual(truth, dst);
  1001. }
  1002. public void testMatFromByteBuffer() {
  1003. ByteBuffer bbuf = ByteBuffer.allocateDirect(64*64);
  1004. bbuf.putInt(0x01010101);
  1005. Mat m = new Mat(64,64,CvType.CV_8UC1,bbuf);
  1006. assertEquals(4, Core.countNonZero(m));
  1007. Core.add(m, new Scalar(1), m);
  1008. assertEquals(4096, Core.countNonZero(m));
  1009. m.release();
  1010. assertEquals(2, bbuf.get(0));
  1011. assertEquals(1, bbuf.get(4095));
  1012. }
  1013. public void testMatFromByteBufferWithStep() {
  1014. ByteBuffer bbuf = ByteBuffer.allocateDirect(80*64);
  1015. bbuf.putInt(0x01010101);
  1016. bbuf.putInt(64, 0x02020202);
  1017. bbuf.putInt(80, 0x03030303);
  1018. Mat m = new Mat(64, 64, CvType.CV_8UC1, bbuf, 80);
  1019. assertEquals(8, Core.countNonZero(m));
  1020. Core.add(m, new Scalar(5), m);
  1021. assertEquals(4096, Core.countNonZero(m));
  1022. m.release();
  1023. assertEquals(6, bbuf.get(0));
  1024. assertEquals(5, bbuf.get(63));
  1025. assertEquals(2, bbuf.get(64));
  1026. assertEquals(0, bbuf.get(79));
  1027. assertEquals(8, bbuf.get(80));
  1028. assertEquals(5, bbuf.get(63*80 + 63));
  1029. }
  1030. public void testMatAt() {
  1031. Mat uc1 = new Mat(2, 3, CvType.CV_8S) {
  1032. {
  1033. put(0, 0, 1, 2, 3);
  1034. put(1, 0, 4, 5, 6);
  1035. }
  1036. };
  1037. assertEquals((byte)1, uc1.at(Byte.class, 0, 0).getV().byteValue());
  1038. assertEquals((byte)2, uc1.at(Byte.class, 0, 1).getV().byteValue());
  1039. assertEquals((byte)3, uc1.at(Byte.class, 0, 2).getV().byteValue());
  1040. assertEquals((byte)4, uc1.at(Byte.class, 1, 0).getV().byteValue());
  1041. assertEquals((byte)5, uc1.at(Byte.class, 1, 1).getV().byteValue());
  1042. assertEquals((byte)6, uc1.at(Byte.class, 1, 2).getV().byteValue());
  1043. uc1.at(Byte.class, 0, 0).setV((byte)7);
  1044. uc1.at(Byte.class, 0, 1).setV((byte)8);
  1045. uc1.at(Byte.class, 0, 2).setV((byte)9);
  1046. uc1.at(Byte.class, 1, 0).setV((byte)10);
  1047. uc1.at(Byte.class, 1, 1).setV((byte)11);
  1048. uc1.at(Byte.class, 1, 2).setV((byte)12);
  1049. byte[] data = new byte[6];
  1050. uc1.get(0, 0, data);
  1051. assertArrayEquals(data, new byte[] {7, 8, 9, 10, 11, 12});
  1052. Mat.Tuple3<Byte> bgr = rgbLena.at(Byte.class, 0, 0).getV3c();
  1053. assertEquals(bgr.get_0().byteValue(), (byte)128);
  1054. assertEquals(bgr.get_1().byteValue(), (byte)138);
  1055. assertEquals(bgr.get_2().byteValue(), (byte)225);
  1056. }
  1057. }