parent
47ca1e3128
commit
b356211413
@ -0,0 +1,5 @@
|
||||
file(GLOB_RECURSE _CURRENT_SRC_FILES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} "*.cc")
|
||||
set_property(SOURCE ${_CURRENT_SRC_FILES} PROPERTY COMPILE_DEFINITIONS SUBMODULE_ID=mindspore::SubModuleId::SM_MD)
|
||||
add_library(lite-cv OBJECT
|
||||
image_process.cc
|
||||
lite_mat.cc)
|
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,83 @@
|
||||
/**
|
||||
* Copyright 2020 Huawei Technologies Co., Ltd
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#ifndef IMAGE_PROCESS_H_
|
||||
#define IMAGE_PROCESS_H_
|
||||
|
||||
#include <math.h>
|
||||
#include <vector>
|
||||
#include <algorithm>
|
||||
|
||||
#include "lite_cv/lite_mat.h"
|
||||
|
||||
namespace mindspore {
|
||||
namespace dataset {
|
||||
|
||||
#define INT16_CAST(X) \
|
||||
static_cast<int16_t>(::std::min(::std::max(static_cast<int>(X + (X >= 0.f ? 0.5f : -0.5f)), -32768), 32767));
|
||||
|
||||
#define R2GRAY 77
|
||||
#define G2GRAY 150
|
||||
#define B2GRAY 29
|
||||
#define GRAYSHIFT 8
|
||||
|
||||
enum PaddBorderType { PADD_BORDER_CONSTANT = 0, PADD_BORDER_REPLICATE = 1 };
|
||||
|
||||
struct BoxesConfig {
|
||||
public:
|
||||
std::vector<size_t> img_shape;
|
||||
std::vector<int> num_default;
|
||||
std::vector<int> feature_size;
|
||||
float min_scale;
|
||||
float max_scale;
|
||||
std::vector<std::vector<float>> aspect_rations;
|
||||
std::vector<int> steps;
|
||||
std::vector<float> prior_scaling;
|
||||
};
|
||||
|
||||
/// \brief resizing image by bilinear algorithm, the data type of currently only supports is uint8,
|
||||
/// the channel of currently supports is 3 and 1
|
||||
bool ResizeBilinear(const LiteMat &src, LiteMat &dst, int dst_w, int dst_h);
|
||||
|
||||
/// \brief Init Lite Mat from pixel, the conversion of currently supports is rbgaTorgb and rgbaTobgr
|
||||
bool InitFromPixel(const unsigned char *data, LPixelType pixel_type, LDataType data_type, int w, int h, LiteMat &m);
|
||||
|
||||
/// \brief convert the data type, the conversion of currently supports is uint8 to float
|
||||
bool ConvertTo(const LiteMat &src, LiteMat &dst, double scale = 1.0);
|
||||
|
||||
/// \brief crop image, the channel supports is 3 and 1
|
||||
bool Crop(const LiteMat &src, LiteMat &dst, int x, int y, int w, int h);
|
||||
|
||||
/// \brief normalize image, currently the supports data type is float
|
||||
bool SubStractMeanNormalize(const LiteMat &src, LiteMat &dst, float *mean, float *norm);
|
||||
|
||||
/// \brief padd image, the channel supports is 3 and 1
|
||||
bool Padd(const LiteMat &src, LiteMat &dst, int top, int bottom, int left, int right, PaddBorderType pad_type,
|
||||
uint8_t fill_b_or_gray, uint8_t fill_g, uint8_t fill_r);
|
||||
|
||||
void WarpAffine(const LiteMat &src, LiteMat &out_img, double M[6], std::vector<size_t> dsize, uint8_t borderValue[3]);
|
||||
|
||||
std::vector<std::vector<float>> GetDefaultBoxes(const BoxesConfig config);
|
||||
|
||||
void ConvertBoxes(std::vector<std::vector<float>> &boxes, const std::vector<std::vector<float>> &default_boxes,
|
||||
const BoxesConfig config);
|
||||
|
||||
std::vector<int> ApplyNms(const std::vector<std::vector<float>> &all_boxes, std::vector<float> &all_scores, float thres,
|
||||
int max_boxes);
|
||||
|
||||
} // namespace dataset
|
||||
} // namespace mindspore
|
||||
#endif // IMAGE_PROCESS_H_
|
@ -0,0 +1,207 @@
|
||||
/**
|
||||
* Copyright 2020 Huawei Technologies Co., Ltd
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
#include "lite_cv/lite_mat.h"
|
||||
|
||||
namespace mindspore {
|
||||
namespace dataset {
|
||||
|
||||
LiteMat::LiteMat() {
|
||||
data_ptr_ = 0;
|
||||
elem_size_ = 0;
|
||||
width_ = 0;
|
||||
height_ = 0;
|
||||
channel_ = 0;
|
||||
c_step_ = 0;
|
||||
dims_ = 0;
|
||||
data_type_ = LDataType::UINT8;
|
||||
ref_count_ = 0;
|
||||
}
|
||||
|
||||
LiteMat::LiteMat(int width, LDataType data_type) {
|
||||
data_ptr_ = 0;
|
||||
elem_size_ = 0;
|
||||
width_ = 0;
|
||||
height_ = 0;
|
||||
channel_ = 0;
|
||||
c_step_ = 0;
|
||||
dims_ = 0;
|
||||
data_type_ = LDataType::UINT8;
|
||||
ref_count_ = 0;
|
||||
Init(width, data_type);
|
||||
}
|
||||
|
||||
LiteMat::LiteMat(int width, int height, LDataType data_type) {
|
||||
data_ptr_ = 0;
|
||||
elem_size_ = 0;
|
||||
width_ = 0;
|
||||
height_ = 0;
|
||||
channel_ = 0;
|
||||
c_step_ = 0;
|
||||
dims_ = 0;
|
||||
data_type_ = LDataType::UINT8;
|
||||
ref_count_ = 0;
|
||||
Init(width, height, data_type);
|
||||
}
|
||||
|
||||
LiteMat::LiteMat(int width, int height, int channel, LDataType data_type) {
|
||||
data_ptr_ = 0;
|
||||
elem_size_ = 0;
|
||||
width_ = 0;
|
||||
height_ = 0;
|
||||
channel_ = 0;
|
||||
c_step_ = 0;
|
||||
dims_ = 0;
|
||||
data_type_ = LDataType::UINT8;
|
||||
ref_count_ = 0;
|
||||
Init(width, height, channel, data_type);
|
||||
}
|
||||
|
||||
LiteMat::~LiteMat() { Release(); }
|
||||
|
||||
int LiteMat::addRef(int *p, int value) {
|
||||
int v = *p;
|
||||
*p += value;
|
||||
return v;
|
||||
}
|
||||
|
||||
LiteMat::LiteMat(const LiteMat &m) {
|
||||
data_ptr_ = m.data_ptr_;
|
||||
elem_size_ = m.elem_size_;
|
||||
width_ = m.width_;
|
||||
height_ = m.height_;
|
||||
channel_ = m.channel_;
|
||||
c_step_ = m.c_step_;
|
||||
dims_ = m.dims_;
|
||||
data_type_ = m.data_type_;
|
||||
ref_count_ = m.ref_count_;
|
||||
if (ref_count_) {
|
||||
addRef(ref_count_, 1);
|
||||
}
|
||||
}
|
||||
|
||||
LiteMat &LiteMat::operator=(const LiteMat &m) {
|
||||
if (this == &m) {
|
||||
return *this;
|
||||
}
|
||||
|
||||
if (m.ref_count_) {
|
||||
addRef(m.ref_count_, 1);
|
||||
}
|
||||
|
||||
Release();
|
||||
data_ptr_ = m.data_ptr_;
|
||||
elem_size_ = m.elem_size_;
|
||||
width_ = m.width_;
|
||||
height_ = m.height_;
|
||||
channel_ = m.channel_;
|
||||
c_step_ = m.c_step_;
|
||||
dims_ = m.dims_;
|
||||
data_type_ = m.data_type_;
|
||||
ref_count_ = m.ref_count_;
|
||||
return *this;
|
||||
}
|
||||
|
||||
void LiteMat::Init(int width, LDataType data_type) {
|
||||
Release();
|
||||
data_type_ = data_type;
|
||||
InitElemSize(data_type);
|
||||
width_ = width;
|
||||
dims_ = 1;
|
||||
height_ = 1;
|
||||
channel_ = 1;
|
||||
c_step_ = width;
|
||||
size_ = c_step_ * elem_size_;
|
||||
data_ptr_ = AlignMalloc(size_);
|
||||
ref_count_ = new int[1];
|
||||
*ref_count_ = 1;
|
||||
}
|
||||
|
||||
void LiteMat::Init(int width, int height, LDataType data_type) {
|
||||
Release();
|
||||
data_type_ = data_type;
|
||||
InitElemSize(data_type);
|
||||
|
||||
width_ = width;
|
||||
height_ = height;
|
||||
dims_ = 2;
|
||||
channel_ = 1;
|
||||
c_step_ = width_ * height_;
|
||||
size_ = c_step_ * elem_size_;
|
||||
data_ptr_ = AlignMalloc(size_);
|
||||
ref_count_ = new int[1];
|
||||
*ref_count_ = 1;
|
||||
}
|
||||
|
||||
void LiteMat::Init(int width, int height, int channel, LDataType data_type) {
|
||||
Release();
|
||||
data_type_ = data_type;
|
||||
InitElemSize(data_type);
|
||||
width_ = width;
|
||||
height_ = height;
|
||||
dims_ = 3;
|
||||
channel_ = channel;
|
||||
c_step_ = ((height_ * width_ * elem_size_ + ALIGN - 1) & (-ALIGN)) / elem_size_;
|
||||
size_ = c_step_ * channel_ * elem_size_;
|
||||
|
||||
data_ptr_ = AlignMalloc(size_);
|
||||
|
||||
ref_count_ = new int[1];
|
||||
*ref_count_ = 1;
|
||||
}
|
||||
|
||||
bool LiteMat::IsEmpty() const { return data_ptr_ == 0 || data_ptr_ == nullptr || c_step_ * channel_ == 0; }
|
||||
|
||||
void LiteMat::Release() {
|
||||
if (ref_count_ && (addRef(ref_count_, -1) == 1)) {
|
||||
if (data_ptr_) {
|
||||
AlignFree(data_ptr_);
|
||||
}
|
||||
if (ref_count_) {
|
||||
delete[] ref_count_;
|
||||
}
|
||||
}
|
||||
data_ptr_ = 0;
|
||||
elem_size_ = 0;
|
||||
width_ = 0;
|
||||
height_ = 0;
|
||||
channel_ = 0;
|
||||
c_step_ = 0;
|
||||
ref_count_ = 0;
|
||||
}
|
||||
|
||||
void *LiteMat::AlignMalloc(unsigned int size) {
|
||||
unsigned int length = sizeof(void *) + ALIGN - 1;
|
||||
void *p_raw = reinterpret_cast<void *>(malloc(size + length));
|
||||
if (p_raw) {
|
||||
void **p_algin = reinterpret_cast<void **>(((size_t)(p_raw) + length) & ~(ALIGN - 1));
|
||||
p_algin[-1] = p_raw;
|
||||
return p_algin;
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
void LiteMat::AlignFree(void *ptr) { (void)free(reinterpret_cast<void **>(ptr)[-1]); }
|
||||
inline void LiteMat::InitElemSize(LDataType data_type) {
|
||||
if (data_type == LDataType::UINT8) {
|
||||
elem_size_ = 1;
|
||||
} else if (data_type == LDataType::UINT16) {
|
||||
elem_size_ = 2;
|
||||
} else if (data_type == LDataType::FLOAT32) {
|
||||
elem_size_ = 4;
|
||||
} else {
|
||||
}
|
||||
}
|
||||
} // namespace dataset
|
||||
} // namespace mindspore
|
@ -0,0 +1,219 @@
|
||||
/**
|
||||
* Copyright 2020 Huawei Technologies Co., Ltd
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#ifndef MINI_MAT_H_
|
||||
#define MINI_MAT_H_
|
||||
|
||||
#include <string>
|
||||
#include <memory>
|
||||
|
||||
namespace mindspore {
|
||||
namespace dataset {
|
||||
|
||||
#define ALIGN 16
|
||||
|
||||
template <typename T>
|
||||
struct Chn1 {
|
||||
T c1;
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
struct Chn2 {
|
||||
T c1;
|
||||
T c2;
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
struct Chn3 {
|
||||
T c1;
|
||||
T c2;
|
||||
T c3;
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
struct Chn4 {
|
||||
T c1;
|
||||
T c2;
|
||||
T c3;
|
||||
T c4;
|
||||
};
|
||||
|
||||
using UINT8_C1 = Chn1<uint8_t>;
|
||||
using UINT8_C2 = Chn2<uint8_t>;
|
||||
using UINT8_C3 = Chn3<uint8_t>;
|
||||
using UINT8_C4 = Chn4<uint8_t>;
|
||||
|
||||
using INT8_C1 = Chn1<int8_t>;
|
||||
using INT8_C2 = Chn2<int8_t>;
|
||||
using INT8_C3 = Chn3<int8_t>;
|
||||
using INT8_C4 = Chn4<int8_t>;
|
||||
|
||||
using UINT32_C1 = Chn1<uint32_t>;
|
||||
using UINT32_C2 = Chn2<uint32_t>;
|
||||
using UINT32_C3 = Chn3<uint32_t>;
|
||||
using UINT32_C4 = Chn4<uint32_t>;
|
||||
|
||||
using INT32_C1 = Chn1<int32_t>;
|
||||
using INT32_C2 = Chn2<int32_t>;
|
||||
using INT32_C3 = Chn3<int32_t>;
|
||||
using INT32_C4 = Chn4<int32_t>;
|
||||
|
||||
using FLOAT32_C1 = Chn1<float>;
|
||||
using FLOAT32_C2 = Chn2<float>;
|
||||
using FLOAT32_C3 = Chn3<float>;
|
||||
using FLOAT32_C4 = Chn4<float>;
|
||||
|
||||
using FLOAT64_C1 = Chn1<double>;
|
||||
using FLOAT64_C2 = Chn2<double>;
|
||||
using FLOAT64_C3 = Chn3<double>;
|
||||
using FLOAT64_C4 = Chn4<double>;
|
||||
|
||||
enum LPixelType {
|
||||
BGR = 0,
|
||||
RGB = 1,
|
||||
RGBA = 2,
|
||||
RGBA2GRAY = 3,
|
||||
RGBA2BGR = 4,
|
||||
};
|
||||
|
||||
class LDataType {
|
||||
public:
|
||||
enum Type : uint8_t {
|
||||
UNKNOWN = 0,
|
||||
BOOL,
|
||||
INT8,
|
||||
UINT8,
|
||||
INT16,
|
||||
UINT16,
|
||||
INT32,
|
||||
UINT32,
|
||||
INT64,
|
||||
UINT64,
|
||||
FLOAT16,
|
||||
FLOAT32,
|
||||
FLOAT64,
|
||||
NUM_OF_TYPES
|
||||
};
|
||||
|
||||
LDataType() : type_(UNKNOWN) {}
|
||||
|
||||
LDataType(Type d) : type_(d) {}
|
||||
|
||||
~LDataType() = default;
|
||||
|
||||
inline Type Value() const { return type_; }
|
||||
inline bool operator==(const LDataType &ps) const {
|
||||
if (this->type_ == ps.type_) return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
inline bool operator!=(const LDataType &ps) const {
|
||||
if (this->type_ != ps.type_) return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
uint8_t SizeInBytes() const {
|
||||
if (type_ < LDataType::NUM_OF_TYPES)
|
||||
return SIZE_IN_BYTES[type_];
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
|
||||
public:
|
||||
static inline const uint8_t SIZE_IN_BYTES[] = {
|
||||
0, // UNKNOWN
|
||||
1, // BOOL
|
||||
1, // INT8
|
||||
1, // UINT8
|
||||
2, // INT16
|
||||
2, // UINT16
|
||||
4, // INT32
|
||||
4, // UINT32
|
||||
8, // INT64
|
||||
8, // UINT64
|
||||
2, // FLOAT16
|
||||
4, // FLOAT32
|
||||
8, // FLOAT64
|
||||
};
|
||||
|
||||
Type type_;
|
||||
};
|
||||
|
||||
class LiteMat {
|
||||
// Class that represents a lite Mat of a Image.
|
||||
// -# The pixel type of Lite Mat is RGBRGB...RGB.
|
||||
public:
|
||||
LiteMat();
|
||||
|
||||
explicit LiteMat(int width, LDataType data_type = LDataType::UINT8);
|
||||
|
||||
LiteMat(int width, int height, LDataType data_type = LDataType::UINT8);
|
||||
|
||||
LiteMat(int width, int height, int channel, LDataType data_type = LDataType::UINT8);
|
||||
|
||||
~LiteMat();
|
||||
|
||||
LiteMat(const LiteMat &m);
|
||||
|
||||
void Init(int width, LDataType data_type = LDataType::UINT8);
|
||||
|
||||
void Init(int width, int height, LDataType data_type = LDataType::UINT8);
|
||||
|
||||
void Init(int width, int height, int channel, LDataType data_type = LDataType::UINT8);
|
||||
|
||||
bool IsEmpty() const;
|
||||
|
||||
void Release();
|
||||
|
||||
LiteMat &operator=(const LiteMat &m);
|
||||
|
||||
template <typename T>
|
||||
operator T *() {
|
||||
return reinterpret_cast<T *>(data_ptr_);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
operator const T *() const {
|
||||
return reinterpret_cast<const T *>(data_ptr_);
|
||||
}
|
||||
|
||||
private:
|
||||
/// \brief apply for memory alignment
|
||||
void *AlignMalloc(unsigned int size);
|
||||
|
||||
/// \brief free memory
|
||||
void AlignFree(void *ptr);
|
||||
|
||||
void InitElemSize(LDataType data_type);
|
||||
|
||||
/// \brief add reference
|
||||
int addRef(int *p, int value);
|
||||
|
||||
public:
|
||||
void *data_ptr_ = nullptr;
|
||||
int elem_size_;
|
||||
int width_;
|
||||
int height_;
|
||||
int channel_;
|
||||
int c_step_;
|
||||
int dims_;
|
||||
size_t size_;
|
||||
LDataType data_type_;
|
||||
int *ref_count_;
|
||||
};
|
||||
} // namespace dataset
|
||||
} // namespace mindspore
|
||||
#endif // MINI_MAT_H_
|
File diff suppressed because it is too large
Load Diff
Loading…
Reference in new issue