You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

opencv.cpp 2.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. // Tencent is pleased to support the open source community by making ncnn available.
  2. //
  3. // Copyright (C) 2017 THL A29 Limited, a Tencent company. All rights reserved.
  4. //
  5. // Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
  6. // in compliance with the License. You may obtain a copy of the License at
  7. //
  8. // https://opensource.org/licenses/BSD-3-Clause
  9. //
  10. // Unless required by applicable law or agreed to in writing, software distributed
  11. // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  12. // CONDITIONS OF ANY KIND, either express or implied. See the License for the
  13. // specific language governing permissions and limitations under the License.
  14. #include "opencv.h"
  15. #if NCNN_OPENCV
  16. #include <stdio.h>
  17. namespace cv {
  18. Mat imread(const std::string& path, int flags)
  19. {
  20. (void)flags;
  21. // read pgm/ppm
  22. FILE* fp = fopen(path.c_str(), "rb");
  23. if (!fp)
  24. return Mat();
  25. Mat m;
  26. char magic[3];
  27. int w, h;
  28. int nscan = fscanf(fp, "%2s\n%d %d\n255\n", magic, &w, &h);
  29. if (nscan == 3 && magic[0] == 'P' && (magic[1] == '5' || magic[1] == '6'))
  30. {
  31. if (magic[1] == '5')
  32. {
  33. m.create(h, w, CV_8UC1);
  34. }
  35. else if (magic[1] == '6')
  36. {
  37. m.create(h, w, CV_8UC3);
  38. }
  39. if (m.empty())
  40. {
  41. fclose(fp);
  42. return Mat();
  43. }
  44. fread(m.data, 1, m.total(), fp);
  45. }
  46. fclose(fp);
  47. return m;
  48. }
  49. void imwrite(const std::string& path, const Mat& m)
  50. {
  51. // write pgm/ppm
  52. FILE* fp = fopen(path.c_str(), "wb");
  53. if (!fp)
  54. return;
  55. if (m.channels() == 1)
  56. {
  57. fprintf(fp, "P5\n%d %d\n255\n", m.cols, m.rows);
  58. }
  59. else if (m.channels() == 3)
  60. {
  61. fprintf(fp, "P6\n%d %d\n255\n", m.cols, m.rows);
  62. }
  63. fwrite(m.data, 1, m.total(), fp);
  64. fclose(fp);
  65. }
  66. #if NCNN_PIXEL
  67. void resize(const Mat& src, Mat& dst, const Size& size, float sw, float sh, int flags)
  68. {
  69. int srcw = src.cols;
  70. int srch = src.rows;
  71. int w = size.width;
  72. int h = size.height;
  73. if (w == 0 || h == 0)
  74. {
  75. w = srcw * sw;
  76. h = srch * sh;
  77. }
  78. if (w == 0 || h == 0)
  79. return;
  80. if (w == srcw && h == srch)
  81. {
  82. dst = src.clone();
  83. return;
  84. }
  85. cv::Mat tmp(h, w, src.c);
  86. if (tmp.empty())
  87. return;
  88. if (src.c == 1)
  89. ncnn::resize_bilinear_c1(src.data, srcw, srch, tmp.data, w, h);
  90. else if (src.c == 3)
  91. ncnn::resize_bilinear_c3(src.data, srcw, srch, tmp.data, w, h);
  92. else if (src.c == 4)
  93. ncnn::resize_bilinear_c4(src.data, srcw, srch, tmp.data, w, h);
  94. dst = tmp;
  95. }
  96. #endif // NCNN_PIXEL
  97. } // namespace cv
  98. #endif // NCNN_OPENCV