| 108 | } |
| 109 | |
| 110 | VARP ImageDataset::convertImage(const std::string& imageName, const ImageConfig& mConfig, const MNN::CV::ImageProcess::Config& mProcessConfig) { |
| 111 | int originalWidth, originalHeight, comp; |
| 112 | auto bitmap32bits = stbi_load(imageName.c_str(), &originalWidth, &originalHeight, &comp, 4); |
| 113 | if (bitmap32bits == nullptr) { |
| 114 | MNN_PRINT("can not open image: %s\n", imageName.c_str()); |
| 115 | MNN_ASSERT(false); |
| 116 | return nullptr; |
| 117 | } |
| 118 | |
| 119 | // choose resize or crop |
| 120 | // resize method |
| 121 | int oh, ow, bpp; |
| 122 | if (mConfig.resizeHeight > 0 && mConfig.resizeWidth > 0) { |
| 123 | oh = mConfig.resizeHeight; |
| 124 | ow = mConfig.resizeWidth; |
| 125 | } else { |
| 126 | oh = originalHeight; |
| 127 | ow = originalWidth; |
| 128 | } |
| 129 | bpp = 0; |
| 130 | switch (mConfig.destFormat) { |
| 131 | case GRAY: |
| 132 | bpp = 1; |
| 133 | break; |
| 134 | case RGB: |
| 135 | case BGR: |
| 136 | bpp = 3; |
| 137 | break; |
| 138 | case RGBA: |
| 139 | case BGRA: |
| 140 | bpp = 4; |
| 141 | break; |
| 142 | default: |
| 143 | break; |
| 144 | } |
| 145 | MNN_ASSERT(bpp > 0); |
| 146 | |
| 147 | std::shared_ptr<MNN::CV::ImageProcess> process; |
| 148 | process.reset(ImageProcess::create(mProcessConfig)); |
| 149 | |
| 150 | if (abs(mConfig.cropFraction[0] - 1.) > 1e-6 || abs(mConfig.cropFraction[1] - 1.) > 1e-6) { |
| 151 | const float cropFractionH = mConfig.cropFraction[0]; |
| 152 | const float cropFractionW = mConfig.cropFraction[1]; |
| 153 | |
| 154 | const int hCropSize = int(originalHeight * cropFractionH); |
| 155 | const int wCropSize = int(originalWidth * cropFractionW); |
| 156 | MNN_ASSERT(hCropSize > 0 && wCropSize > 0); |
| 157 | // default center crop |
| 158 | int startH = (originalHeight - hCropSize) / 2; |
| 159 | int startW = (originalWidth - wCropSize) / 2; |
| 160 | |
| 161 | if (mConfig.centerOrRandomCrop == true) { |
| 162 | const int maxStartPointH = originalHeight - hCropSize; |
| 163 | const int maxStartPointW = originalWidth - wCropSize; |
| 164 | // generate a random number between (0, maxPoint) |
| 165 | auto gen = RandomGenerator::generator(); |
| 166 | std::uniform_int_distribution<> disH(0, maxStartPointH); |
| 167 | startH = disH(gen); |
nothing calls this directly
no test coverage detected