test_cvtyuv.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727
  1. #include "test_precomp.hpp"
  2. namespace opencv_test { namespace {
  3. #undef RGB
  4. #undef YUV
  5. typedef Vec3b YUV;
  6. typedef Vec3b RGB;
  7. int countOfDifferencies(const Mat& gold, const Mat& result, int maxAllowedDifference = 1)
  8. {
  9. Mat diff;
  10. absdiff(gold, result, diff);
  11. return countNonZero(diff.reshape(1) > maxAllowedDifference);
  12. }
  13. class YUVreader
  14. {
  15. public:
  16. virtual ~YUVreader() {}
  17. virtual YUV read(const Mat& yuv, int row, int col) = 0;
  18. virtual int channels() = 0;
  19. virtual Size size(Size imgSize) = 0;
  20. virtual bool requiresEvenHeight() { return true; }
  21. virtual bool requiresEvenWidth() { return true; }
  22. static YUVreader* getReader(int code);
  23. };
  24. class RGBreader
  25. {
  26. public:
  27. virtual ~RGBreader() {}
  28. virtual RGB read(const Mat& rgb, int row, int col) = 0;
  29. virtual int channels() = 0;
  30. static RGBreader* getReader(int code);
  31. };
  32. class RGBwriter
  33. {
  34. public:
  35. virtual ~RGBwriter() {}
  36. virtual void write(Mat& rgb, int row, int col, const RGB& val) = 0;
  37. virtual int channels() = 0;
  38. static RGBwriter* getWriter(int code);
  39. };
  40. class GRAYwriter
  41. {
  42. public:
  43. virtual ~GRAYwriter() {}
  44. virtual void write(Mat& gray, int row, int col, const uchar& val)
  45. {
  46. gray.at<uchar>(row, col) = val;
  47. }
  48. virtual int channels() { return 1; }
  49. static GRAYwriter* getWriter(int code);
  50. };
  51. class YUVwriter
  52. {
  53. public:
  54. virtual ~YUVwriter() {}
  55. virtual void write(Mat& yuv, int row, int col, const YUV& val) = 0;
  56. virtual int channels() = 0;
  57. virtual Size size(Size imgSize) = 0;
  58. virtual bool requiresEvenHeight() { return true; }
  59. virtual bool requiresEvenWidth() { return true; }
  60. static YUVwriter* getWriter(int code);
  61. };
  62. class RGB888Writer : public RGBwriter
  63. {
  64. void write(Mat& rgb, int row, int col, const RGB& val)
  65. {
  66. rgb.at<Vec3b>(row, col) = val;
  67. }
  68. int channels() { return 3; }
  69. };
  70. class BGR888Writer : public RGBwriter
  71. {
  72. void write(Mat& rgb, int row, int col, const RGB& val)
  73. {
  74. Vec3b tmp(val[2], val[1], val[0]);
  75. rgb.at<Vec3b>(row, col) = tmp;
  76. }
  77. int channels() { return 3; }
  78. };
  79. class RGBA8888Writer : public RGBwriter
  80. {
  81. void write(Mat& rgb, int row, int col, const RGB& val)
  82. {
  83. Vec4b tmp(val[0], val[1], val[2], 255);
  84. rgb.at<Vec4b>(row, col) = tmp;
  85. }
  86. int channels() { return 4; }
  87. };
  88. class BGRA8888Writer : public RGBwriter
  89. {
  90. void write(Mat& rgb, int row, int col, const RGB& val)
  91. {
  92. Vec4b tmp(val[2], val[1], val[0], 255);
  93. rgb.at<Vec4b>(row, col) = tmp;
  94. }
  95. int channels() { return 4; }
  96. };
  97. class YUV420pWriter: public YUVwriter
  98. {
  99. int channels() { return 1; }
  100. Size size(Size imgSize) { return Size(imgSize.width, imgSize.height + imgSize.height/2); }
  101. };
  102. class YV12Writer: public YUV420pWriter
  103. {
  104. void write(Mat& yuv, int row, int col, const YUV& val)
  105. {
  106. int h = yuv.rows * 2 / 3;
  107. yuv.ptr<uchar>(row)[col] = val[0];
  108. if( row % 2 == 0 && col % 2 == 0 )
  109. {
  110. yuv.ptr<uchar>(h + row/4)[col/2 + ((row/2) % 2) * (yuv.cols/2)] = val[2];
  111. yuv.ptr<uchar>(h + (row/2 + h/2)/2)[col/2 + ((row/2 + h/2) % 2) * (yuv.cols/2)] = val[1];
  112. }
  113. }
  114. };
  115. class I420Writer: public YUV420pWriter
  116. {
  117. void write(Mat& yuv, int row, int col, const YUV& val)
  118. {
  119. int h = yuv.rows * 2 / 3;
  120. yuv.ptr<uchar>(row)[col] = val[0];
  121. if( row % 2 == 0 && col % 2 == 0 )
  122. {
  123. yuv.ptr<uchar>(h + row/4)[col/2 + ((row/2) % 2) * (yuv.cols/2)] = val[1];
  124. yuv.ptr<uchar>(h + (row/2 + h/2)/2)[col/2 + ((row/2 + h/2) % 2) * (yuv.cols/2)] = val[2];
  125. }
  126. }
  127. };
  128. class YUV420Reader: public YUVreader
  129. {
  130. int channels() { return 1; }
  131. Size size(Size imgSize) { return Size(imgSize.width, imgSize.height * 3 / 2); }
  132. };
  133. class YUV422Reader: public YUVreader
  134. {
  135. int channels() { return 2; }
  136. Size size(Size imgSize) { return imgSize; }
  137. bool requiresEvenHeight() { return false; }
  138. };
  139. class NV21Reader: public YUV420Reader
  140. {
  141. YUV read(const Mat& yuv, int row, int col)
  142. {
  143. uchar y = yuv.ptr<uchar>(row)[col];
  144. uchar u = yuv.ptr<uchar>(yuv.rows * 2 / 3 + row/2)[(col/2)*2 + 1];
  145. uchar v = yuv.ptr<uchar>(yuv.rows * 2 / 3 + row/2)[(col/2)*2];
  146. return YUV(y, u, v);
  147. }
  148. };
  149. struct NV12Reader: public YUV420Reader
  150. {
  151. YUV read(const Mat& yuv, int row, int col)
  152. {
  153. uchar y = yuv.ptr<uchar>(row)[col];
  154. uchar u = yuv.ptr<uchar>(yuv.rows * 2 / 3 + row/2)[(col/2)*2];
  155. uchar v = yuv.ptr<uchar>(yuv.rows * 2 / 3 + row/2)[(col/2)*2 + 1];
  156. return YUV(y, u, v);
  157. }
  158. };
  159. class YV12Reader: public YUV420Reader
  160. {
  161. YUV read(const Mat& yuv, int row, int col)
  162. {
  163. int h = yuv.rows * 2 / 3;
  164. uchar y = yuv.ptr<uchar>(row)[col];
  165. uchar u = yuv.ptr<uchar>(h + (row/2 + h/2)/2)[col/2 + ((row/2 + h/2) % 2) * (yuv.cols/2)];
  166. uchar v = yuv.ptr<uchar>(h + row/4)[col/2 + ((row/2) % 2) * (yuv.cols/2)];
  167. return YUV(y, u, v);
  168. }
  169. };
  170. class IYUVReader: public YUV420Reader
  171. {
  172. YUV read(const Mat& yuv, int row, int col)
  173. {
  174. int h = yuv.rows * 2 / 3;
  175. uchar y = yuv.ptr<uchar>(row)[col];
  176. uchar u = yuv.ptr<uchar>(h + row/4)[col/2 + ((row/2) % 2) * (yuv.cols/2)];
  177. uchar v = yuv.ptr<uchar>(h + (row/2 + h/2)/2)[col/2 + ((row/2 + h/2) % 2) * (yuv.cols/2)];
  178. return YUV(y, u, v);
  179. }
  180. };
  181. class UYVYReader: public YUV422Reader
  182. {
  183. YUV read(const Mat& yuv, int row, int col)
  184. {
  185. uchar y = yuv.ptr<Vec2b>(row)[col][1];
  186. uchar u = yuv.ptr<Vec2b>(row)[(col/2)*2][0];
  187. uchar v = yuv.ptr<Vec2b>(row)[(col/2)*2 + 1][0];
  188. return YUV(y, u, v);
  189. }
  190. };
  191. class YUY2Reader: public YUV422Reader
  192. {
  193. YUV read(const Mat& yuv, int row, int col)
  194. {
  195. uchar y = yuv.ptr<Vec2b>(row)[col][0];
  196. uchar u = yuv.ptr<Vec2b>(row)[(col/2)*2][1];
  197. uchar v = yuv.ptr<Vec2b>(row)[(col/2)*2 + 1][1];
  198. return YUV(y, u, v);
  199. }
  200. };
  201. class YVYUReader: public YUV422Reader
  202. {
  203. YUV read(const Mat& yuv, int row, int col)
  204. {
  205. uchar y = yuv.ptr<Vec2b>(row)[col][0];
  206. uchar u = yuv.ptr<Vec2b>(row)[(col/2)*2 + 1][1];
  207. uchar v = yuv.ptr<Vec2b>(row)[(col/2)*2][1];
  208. return YUV(y, u, v);
  209. }
  210. };
  211. class YUV888Reader : public YUVreader
  212. {
  213. YUV read(const Mat& yuv, int row, int col)
  214. {
  215. return yuv.at<YUV>(row, col);
  216. }
  217. int channels() { return 3; }
  218. Size size(Size imgSize) { return imgSize; }
  219. bool requiresEvenHeight() { return false; }
  220. bool requiresEvenWidth() { return false; }
  221. };
  222. class RGB888Reader : public RGBreader
  223. {
  224. RGB read(const Mat& rgb, int row, int col)
  225. {
  226. return rgb.at<RGB>(row, col);
  227. }
  228. int channels() { return 3; }
  229. };
  230. class BGR888Reader : public RGBreader
  231. {
  232. RGB read(const Mat& rgb, int row, int col)
  233. {
  234. RGB tmp = rgb.at<RGB>(row, col);
  235. return RGB(tmp[2], tmp[1], tmp[0]);
  236. }
  237. int channels() { return 3; }
  238. };
  239. class RGBA8888Reader : public RGBreader
  240. {
  241. RGB read(const Mat& rgb, int row, int col)
  242. {
  243. Vec4b rgba = rgb.at<Vec4b>(row, col);
  244. return RGB(rgba[0], rgba[1], rgba[2]);
  245. }
  246. int channels() { return 4; }
  247. };
  248. class BGRA8888Reader : public RGBreader
  249. {
  250. RGB read(const Mat& rgb, int row, int col)
  251. {
  252. Vec4b rgba = rgb.at<Vec4b>(row, col);
  253. return RGB(rgba[2], rgba[1], rgba[0]);
  254. }
  255. int channels() { return 4; }
  256. };
  257. class YUV2RGB_Converter
  258. {
  259. public:
  260. RGB convert(YUV yuv)
  261. {
  262. int y = std::max(0, yuv[0] - 16);
  263. int u = yuv[1] - 128;
  264. int v = yuv[2] - 128;
  265. uchar r = saturate_cast<uchar>(1.164f * y + 1.596f * v);
  266. uchar g = saturate_cast<uchar>(1.164f * y - 0.813f * v - 0.391f * u);
  267. uchar b = saturate_cast<uchar>(1.164f * y + 2.018f * u);
  268. return RGB(r, g, b);
  269. }
  270. };
  271. class YUV2GRAY_Converter
  272. {
  273. public:
  274. uchar convert(YUV yuv)
  275. {
  276. return yuv[0];
  277. }
  278. };
  279. class RGB2YUV_Converter
  280. {
  281. public:
  282. YUV convert(RGB rgb)
  283. {
  284. int r = rgb[0];
  285. int g = rgb[1];
  286. int b = rgb[2];
  287. uchar y = saturate_cast<uchar>((int)( 0.257f*r + 0.504f*g + 0.098f*b + 0.5f) + 16);
  288. uchar u = saturate_cast<uchar>((int)(-0.148f*r - 0.291f*g + 0.439f*b + 0.5f) + 128);
  289. uchar v = saturate_cast<uchar>((int)( 0.439f*r - 0.368f*g - 0.071f*b + 0.5f) + 128);
  290. return YUV(y, u, v);
  291. }
  292. };
  293. YUVreader* YUVreader::getReader(int code)
  294. {
  295. switch(code)
  296. {
  297. case COLOR_YUV2RGB_NV12:
  298. case COLOR_YUV2BGR_NV12:
  299. case COLOR_YUV2RGBA_NV12:
  300. case COLOR_YUV2BGRA_NV12:
  301. return new NV12Reader();
  302. case COLOR_YUV2RGB_NV21:
  303. case COLOR_YUV2BGR_NV21:
  304. case COLOR_YUV2RGBA_NV21:
  305. case COLOR_YUV2BGRA_NV21:
  306. return new NV21Reader();
  307. case COLOR_YUV2RGB_YV12:
  308. case COLOR_YUV2BGR_YV12:
  309. case COLOR_YUV2RGBA_YV12:
  310. case COLOR_YUV2BGRA_YV12:
  311. return new YV12Reader();
  312. case COLOR_YUV2RGB_IYUV:
  313. case COLOR_YUV2BGR_IYUV:
  314. case COLOR_YUV2RGBA_IYUV:
  315. case COLOR_YUV2BGRA_IYUV:
  316. return new IYUVReader();
  317. case COLOR_YUV2RGB_UYVY:
  318. case COLOR_YUV2BGR_UYVY:
  319. case COLOR_YUV2RGBA_UYVY:
  320. case COLOR_YUV2BGRA_UYVY:
  321. return new UYVYReader();
  322. //case COLOR_YUV2RGB_VYUY = 109,
  323. //case COLOR_YUV2BGR_VYUY = 110,
  324. //case COLOR_YUV2RGBA_VYUY = 113,
  325. //case COLOR_YUV2BGRA_VYUY = 114,
  326. // return ??
  327. case COLOR_YUV2RGB_YUY2:
  328. case COLOR_YUV2BGR_YUY2:
  329. case COLOR_YUV2RGBA_YUY2:
  330. case COLOR_YUV2BGRA_YUY2:
  331. return new YUY2Reader();
  332. case COLOR_YUV2RGB_YVYU:
  333. case COLOR_YUV2BGR_YVYU:
  334. case COLOR_YUV2RGBA_YVYU:
  335. case COLOR_YUV2BGRA_YVYU:
  336. return new YVYUReader();
  337. case COLOR_YUV2GRAY_420:
  338. return new NV21Reader();
  339. case COLOR_YUV2GRAY_UYVY:
  340. return new UYVYReader();
  341. case COLOR_YUV2GRAY_YUY2:
  342. return new YUY2Reader();
  343. case COLOR_YUV2BGR:
  344. case COLOR_YUV2RGB:
  345. return new YUV888Reader();
  346. default:
  347. return 0;
  348. }
  349. }
  350. RGBreader* RGBreader::getReader(int code)
  351. {
  352. switch(code)
  353. {
  354. case COLOR_RGB2YUV_YV12:
  355. case COLOR_RGB2YUV_I420:
  356. return new RGB888Reader();
  357. case COLOR_BGR2YUV_YV12:
  358. case COLOR_BGR2YUV_I420:
  359. return new BGR888Reader();
  360. case COLOR_RGBA2YUV_I420:
  361. case COLOR_RGBA2YUV_YV12:
  362. return new RGBA8888Reader();
  363. case COLOR_BGRA2YUV_YV12:
  364. case COLOR_BGRA2YUV_I420:
  365. return new BGRA8888Reader();
  366. default:
  367. return 0;
  368. };
  369. }
  370. RGBwriter* RGBwriter::getWriter(int code)
  371. {
  372. switch(code)
  373. {
  374. case COLOR_YUV2RGB_NV12:
  375. case COLOR_YUV2RGB_NV21:
  376. case COLOR_YUV2RGB_YV12:
  377. case COLOR_YUV2RGB_IYUV:
  378. case COLOR_YUV2RGB_UYVY:
  379. //case COLOR_YUV2RGB_VYUY:
  380. case COLOR_YUV2RGB_YUY2:
  381. case COLOR_YUV2RGB_YVYU:
  382. case COLOR_YUV2RGB:
  383. return new RGB888Writer();
  384. case COLOR_YUV2BGR_NV12:
  385. case COLOR_YUV2BGR_NV21:
  386. case COLOR_YUV2BGR_YV12:
  387. case COLOR_YUV2BGR_IYUV:
  388. case COLOR_YUV2BGR_UYVY:
  389. //case COLOR_YUV2BGR_VYUY:
  390. case COLOR_YUV2BGR_YUY2:
  391. case COLOR_YUV2BGR_YVYU:
  392. case COLOR_YUV2BGR:
  393. return new BGR888Writer();
  394. case COLOR_YUV2RGBA_NV12:
  395. case COLOR_YUV2RGBA_NV21:
  396. case COLOR_YUV2RGBA_YV12:
  397. case COLOR_YUV2RGBA_IYUV:
  398. case COLOR_YUV2RGBA_UYVY:
  399. //case COLOR_YUV2RGBA_VYUY:
  400. case COLOR_YUV2RGBA_YUY2:
  401. case COLOR_YUV2RGBA_YVYU:
  402. return new RGBA8888Writer();
  403. case COLOR_YUV2BGRA_NV12:
  404. case COLOR_YUV2BGRA_NV21:
  405. case COLOR_YUV2BGRA_YV12:
  406. case COLOR_YUV2BGRA_IYUV:
  407. case COLOR_YUV2BGRA_UYVY:
  408. //case COLOR_YUV2BGRA_VYUY:
  409. case COLOR_YUV2BGRA_YUY2:
  410. case COLOR_YUV2BGRA_YVYU:
  411. return new BGRA8888Writer();
  412. default:
  413. return 0;
  414. };
  415. }
  416. GRAYwriter* GRAYwriter::getWriter(int code)
  417. {
  418. switch(code)
  419. {
  420. case COLOR_YUV2GRAY_420:
  421. case COLOR_YUV2GRAY_UYVY:
  422. case COLOR_YUV2GRAY_YUY2:
  423. return new GRAYwriter();
  424. default:
  425. return 0;
  426. }
  427. }
  428. YUVwriter* YUVwriter::getWriter(int code)
  429. {
  430. switch(code)
  431. {
  432. case COLOR_RGB2YUV_YV12:
  433. case COLOR_BGR2YUV_YV12:
  434. case COLOR_RGBA2YUV_YV12:
  435. case COLOR_BGRA2YUV_YV12:
  436. return new YV12Writer();
  437. case COLOR_RGB2YUV_I420:
  438. case COLOR_BGR2YUV_I420:
  439. case COLOR_RGBA2YUV_I420:
  440. case COLOR_BGRA2YUV_I420:
  441. return new I420Writer();
  442. default:
  443. return 0;
  444. };
  445. }
  446. template<class convertor>
  447. void referenceYUV2RGB(const Mat& yuv, Mat& rgb, YUVreader* yuvReader, RGBwriter* rgbWriter)
  448. {
  449. convertor cvt;
  450. for(int row = 0; row < rgb.rows; ++row)
  451. for(int col = 0; col < rgb.cols; ++col)
  452. rgbWriter->write(rgb, row, col, cvt.convert(yuvReader->read(yuv, row, col)));
  453. }
  454. template<class convertor>
  455. void referenceYUV2GRAY(const Mat& yuv, Mat& rgb, YUVreader* yuvReader, GRAYwriter* grayWriter)
  456. {
  457. convertor cvt;
  458. for(int row = 0; row < rgb.rows; ++row)
  459. for(int col = 0; col < rgb.cols; ++col)
  460. grayWriter->write(rgb, row, col, cvt.convert(yuvReader->read(yuv, row, col)));
  461. }
  462. template<class convertor>
  463. void referenceRGB2YUV(const Mat& rgb, Mat& yuv, RGBreader* rgbReader, YUVwriter* yuvWriter)
  464. {
  465. convertor cvt;
  466. for(int row = 0; row < rgb.rows; ++row)
  467. for(int col = 0; col < rgb.cols; ++col)
  468. yuvWriter->write(yuv, row, col, cvt.convert(rgbReader->read(rgb, row, col)));
  469. }
  470. struct ConversionYUV
  471. {
  472. explicit ConversionYUV( const int code )
  473. {
  474. yuvReader_ = YUVreader :: getReader(code);
  475. yuvWriter_ = YUVwriter :: getWriter(code);
  476. rgbReader_ = RGBreader :: getReader(code);
  477. rgbWriter_ = RGBwriter :: getWriter(code);
  478. grayWriter_ = GRAYwriter:: getWriter(code);
  479. }
  480. ~ConversionYUV()
  481. {
  482. if (yuvReader_)
  483. delete yuvReader_;
  484. if (yuvWriter_)
  485. delete yuvWriter_;
  486. if (rgbReader_)
  487. delete rgbReader_;
  488. if (rgbWriter_)
  489. delete rgbWriter_;
  490. if (grayWriter_)
  491. delete grayWriter_;
  492. }
  493. int getDcn()
  494. {
  495. return (rgbWriter_ != 0) ? rgbWriter_->channels() : ((grayWriter_ != 0) ? grayWriter_->channels() : yuvWriter_->channels());
  496. }
  497. int getScn()
  498. {
  499. return (yuvReader_ != 0) ? yuvReader_->channels() : rgbReader_->channels();
  500. }
  501. Size getSrcSize( const Size& imgSize )
  502. {
  503. return (yuvReader_ != 0) ? yuvReader_->size(imgSize) : imgSize;
  504. }
  505. Size getDstSize( const Size& imgSize )
  506. {
  507. return (yuvWriter_ != 0) ? yuvWriter_->size(imgSize) : imgSize;
  508. }
  509. bool requiresEvenHeight()
  510. {
  511. return (yuvReader_ != 0) ? yuvReader_->requiresEvenHeight() : ((yuvWriter_ != 0) ? yuvWriter_->requiresEvenHeight() : false);
  512. }
  513. bool requiresEvenWidth()
  514. {
  515. return (yuvReader_ != 0) ? yuvReader_->requiresEvenWidth() : ((yuvWriter_ != 0) ? yuvWriter_->requiresEvenWidth() : false);
  516. }
  517. YUVreader* yuvReader_;
  518. YUVwriter* yuvWriter_;
  519. RGBreader* rgbReader_;
  520. RGBwriter* rgbWriter_;
  521. GRAYwriter* grayWriter_;
  522. };
  523. CV_ENUM(YUVCVTS, COLOR_YUV2RGB_NV12, COLOR_YUV2BGR_NV12, COLOR_YUV2RGB_NV21, COLOR_YUV2BGR_NV21,
  524. COLOR_YUV2RGBA_NV12, COLOR_YUV2BGRA_NV12, COLOR_YUV2RGBA_NV21, COLOR_YUV2BGRA_NV21,
  525. COLOR_YUV2RGB_YV12, COLOR_YUV2BGR_YV12, COLOR_YUV2RGB_IYUV, COLOR_YUV2BGR_IYUV,
  526. COLOR_YUV2RGBA_YV12, COLOR_YUV2BGRA_YV12, COLOR_YUV2RGBA_IYUV, COLOR_YUV2BGRA_IYUV,
  527. COLOR_YUV2RGB_UYVY, COLOR_YUV2BGR_UYVY, COLOR_YUV2RGBA_UYVY, COLOR_YUV2BGRA_UYVY,
  528. COLOR_YUV2RGB_YUY2, COLOR_YUV2BGR_YUY2, COLOR_YUV2RGB_YVYU, COLOR_YUV2BGR_YVYU,
  529. COLOR_YUV2RGBA_YUY2, COLOR_YUV2BGRA_YUY2, COLOR_YUV2RGBA_YVYU, COLOR_YUV2BGRA_YVYU,
  530. COLOR_YUV2GRAY_420, COLOR_YUV2GRAY_UYVY, COLOR_YUV2GRAY_YUY2,
  531. COLOR_YUV2BGR, COLOR_YUV2RGB, COLOR_RGB2YUV_YV12, COLOR_BGR2YUV_YV12, COLOR_RGBA2YUV_YV12,
  532. COLOR_BGRA2YUV_YV12, COLOR_RGB2YUV_I420, COLOR_BGR2YUV_I420, COLOR_RGBA2YUV_I420, COLOR_BGRA2YUV_I420)
  533. typedef ::testing::TestWithParam<YUVCVTS> Imgproc_ColorYUV;
  534. TEST_P(Imgproc_ColorYUV, accuracy)
  535. {
  536. int code = GetParam();
  537. RNG& random = theRNG();
  538. ConversionYUV cvt(code);
  539. const int scn = cvt.getScn();
  540. const int dcn = cvt.getDcn();
  541. for(int iter = 0; iter < 30; ++iter)
  542. {
  543. Size sz(random.uniform(1, 641), random.uniform(1, 481));
  544. if(cvt.requiresEvenWidth()) sz.width += sz.width % 2;
  545. if(cvt.requiresEvenHeight()) sz.height += sz.height % 2;
  546. Size srcSize = cvt.getSrcSize(sz);
  547. Mat src = Mat(srcSize.height, srcSize.width * scn, CV_8UC1).reshape(scn);
  548. Size dstSize = cvt.getDstSize(sz);
  549. Mat dst = Mat(dstSize.height, dstSize.width * dcn, CV_8UC1).reshape(dcn);
  550. Mat gold(dstSize, CV_8UC(dcn));
  551. random.fill(src, RNG::UNIFORM, 0, 256);
  552. if(cvt.rgbWriter_)
  553. referenceYUV2RGB<YUV2RGB_Converter> (src, gold, cvt.yuvReader_, cvt.rgbWriter_);
  554. else if(cvt.grayWriter_)
  555. referenceYUV2GRAY<YUV2GRAY_Converter>(src, gold, cvt.yuvReader_, cvt.grayWriter_);
  556. else if(cvt.yuvWriter_)
  557. referenceRGB2YUV<RGB2YUV_Converter> (src, gold, cvt.rgbReader_, cvt.yuvWriter_);
  558. cv::cvtColor(src, dst, code, -1);
  559. EXPECT_EQ(0, countOfDifferencies(gold, dst));
  560. }
  561. }
  562. TEST_P(Imgproc_ColorYUV, roi_accuracy)
  563. {
  564. int code = GetParam();
  565. RNG& random = theRNG();
  566. ConversionYUV cvt(code);
  567. const int scn = cvt.getScn();
  568. const int dcn = cvt.getDcn();
  569. for(int iter = 0; iter < 30; ++iter)
  570. {
  571. Size sz(random.uniform(1, 641), random.uniform(1, 481));
  572. if(cvt.requiresEvenWidth()) sz.width += sz.width % 2;
  573. if(cvt.requiresEvenHeight()) sz.height += sz.height % 2;
  574. int roi_offset_top = random.uniform(0, 6);
  575. int roi_offset_bottom = random.uniform(0, 6);
  576. int roi_offset_left = random.uniform(0, 6);
  577. int roi_offset_right = random.uniform(0, 6);
  578. Size srcSize = cvt.getSrcSize(sz);
  579. Mat src_full(srcSize.height + roi_offset_top + roi_offset_bottom, srcSize.width + roi_offset_left + roi_offset_right, CV_8UC(scn));
  580. Size dstSize = cvt.getDstSize(sz);
  581. Mat dst_full(dstSize.height + roi_offset_left + roi_offset_right, dstSize.width + roi_offset_top + roi_offset_bottom, CV_8UC(dcn), Scalar::all(0));
  582. Mat gold_full(dst_full.size(), CV_8UC(dcn), Scalar::all(0));
  583. random.fill(src_full, RNG::UNIFORM, 0, 256);
  584. Mat src = src_full(Range(roi_offset_top, roi_offset_top + srcSize.height), Range(roi_offset_left, roi_offset_left + srcSize.width));
  585. Mat dst = dst_full(Range(roi_offset_left, roi_offset_left + dstSize.height), Range(roi_offset_top, roi_offset_top + dstSize.width));
  586. Mat gold = gold_full(Range(roi_offset_left, roi_offset_left + dstSize.height), Range(roi_offset_top, roi_offset_top + dstSize.width));
  587. if(cvt.rgbWriter_)
  588. referenceYUV2RGB<YUV2RGB_Converter> (src, gold, cvt.yuvReader_, cvt.rgbWriter_);
  589. else if(cvt.grayWriter_)
  590. referenceYUV2GRAY<YUV2GRAY_Converter>(src, gold, cvt.yuvReader_, cvt.grayWriter_);
  591. else if(cvt.yuvWriter_)
  592. referenceRGB2YUV<RGB2YUV_Converter> (src, gold, cvt.rgbReader_, cvt.yuvWriter_);
  593. cv::cvtColor(src, dst, code, -1);
  594. EXPECT_EQ(0, countOfDifferencies(gold_full, dst_full));
  595. }
  596. }
  597. INSTANTIATE_TEST_CASE_P(cvt420, Imgproc_ColorYUV,
  598. ::testing::Values((int)COLOR_YUV2RGB_NV12, (int)COLOR_YUV2BGR_NV12, (int)COLOR_YUV2RGB_NV21, (int)COLOR_YUV2BGR_NV21,
  599. (int)COLOR_YUV2RGBA_NV12, (int)COLOR_YUV2BGRA_NV12, (int)COLOR_YUV2RGBA_NV21, (int)COLOR_YUV2BGRA_NV21,
  600. (int)COLOR_YUV2RGB_YV12, (int)COLOR_YUV2BGR_YV12, (int)COLOR_YUV2RGB_IYUV, (int)COLOR_YUV2BGR_IYUV,
  601. (int)COLOR_YUV2RGBA_YV12, (int)COLOR_YUV2BGRA_YV12, (int)COLOR_YUV2RGBA_IYUV, (int)COLOR_YUV2BGRA_IYUV,
  602. (int)COLOR_YUV2GRAY_420, (int)COLOR_RGB2YUV_YV12, (int)COLOR_BGR2YUV_YV12, (int)COLOR_RGBA2YUV_YV12,
  603. (int)COLOR_BGRA2YUV_YV12, (int)COLOR_RGB2YUV_I420, (int)COLOR_BGR2YUV_I420, (int)COLOR_RGBA2YUV_I420,
  604. (int)COLOR_BGRA2YUV_I420));
  605. INSTANTIATE_TEST_CASE_P(cvt422, Imgproc_ColorYUV,
  606. ::testing::Values((int)COLOR_YUV2RGB_UYVY, (int)COLOR_YUV2BGR_UYVY, (int)COLOR_YUV2RGBA_UYVY, (int)COLOR_YUV2BGRA_UYVY,
  607. (int)COLOR_YUV2RGB_YUY2, (int)COLOR_YUV2BGR_YUY2, (int)COLOR_YUV2RGB_YVYU, (int)COLOR_YUV2BGR_YVYU,
  608. (int)COLOR_YUV2RGBA_YUY2, (int)COLOR_YUV2BGRA_YUY2, (int)COLOR_YUV2RGBA_YVYU, (int)COLOR_YUV2BGRA_YVYU,
  609. (int)COLOR_YUV2GRAY_UYVY, (int)COLOR_YUV2GRAY_YUY2));
  610. }} // namespace