MatTest.swift 50 KB

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