MCPcopy Create free account
hub / github.com/alibaba/MNN / convertImage

Method convertImage

tools/train/source/datasets/ImageDataset.cpp:110–215  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

108}
109
110VARP 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);

Callers

nothing calls this directly

Calls 10

stbi_loadFunction · 0.85
_InputFunction · 0.85
stbi_image_freeFunction · 0.85
setPolyToPolyMethod · 0.80
createFunction · 0.50
absFunction · 0.50
c_strMethod · 0.45
resetMethod · 0.45
setMatrixMethod · 0.45
convertMethod · 0.45

Tested by

no test coverage detected