Browse Source

!14790 Fix codedex warnings of Canny and GaussianBlur ops

From: @tiancixiao
Reviewed-by: @heleiwang,@liucunwei
Signed-off-by: @liucunwei
pull/14790/MERGE
mindspore-ci-bot Gitee 4 years ago
parent
commit
654c867cc0
3 changed files with 30 additions and 44 deletions
  1. +16
    -26
      mindspore/ccsrc/minddata/dataset/kernels/image/lite_cv/canny.cc
  2. +6
    -9
      mindspore/ccsrc/minddata/dataset/kernels/image/lite_cv/gaussian_blur.cc
  3. +8
    -9
      mindspore/ccsrc/minddata/dataset/kernels/image/lite_cv/image_process.cc

+ 16
- 26
mindspore/ccsrc/minddata/dataset/kernels/image/lite_cv/canny.cc View File

@@ -31,10 +31,8 @@

namespace mindspore {
namespace dataset {

static void GetSobelKernel(float *kernel, int flag, int ksize, double scale) {
std::vector<float> buffer(ksize + 1);
float *ptr = kernel;

if (ksize == 1) {
buffer[0] = 1;
@@ -72,7 +70,7 @@ static void GetSobelKernel(float *kernel, int flag, int ksize, double scale) {

scale = flag == 0 ? scale : 1.0;
for (int i = 0; i < ksize; i++) {
ptr[i] = buffer[i] * scale;
kernel[i] = buffer[i] * scale;
}
}

@@ -155,33 +153,27 @@ static void NonMaximumSuppression(const LiteMat &gx, const LiteMat &gy, LiteMat
float angle_value = atan2(gy_value_abs, gx_value_abs);
float edge_value = temp[y * gx.width_ + x];
float edge_pre, edge_nex;
if (angle_value < ANGLE_22_5) {
edge_pre = GetEdge(temp, gx.width_, gx.height_, x - 1, y);
edge_nex = GetEdge(temp, gx.width_, gx.height_, x + 1, y);
if (edge_value > edge_pre && edge_value >= edge_nex) {
edges_ptr[y * gx.width_ + x] = temp[y * gx.width_ + x];
if (angle_value < ANGLE_22_5 || angle_value > ANGLE_67_5) {
if (angle_value < ANGLE_22_5) {
edge_pre = GetEdge(temp, gx.width_, gx.height_, x - 1, y);
edge_nex = GetEdge(temp, gx.width_, gx.height_, x + 1, y);
} else {
edges_ptr[y * gx.width_ + x] = 0.f;
edge_pre = GetEdge(temp, gx.width_, gx.height_, x, y - 1);
edge_nex = GetEdge(temp, gx.width_, gx.height_, x, y + 1);
}
} else if (angle_value > ANGLE_67_5) {
edge_pre = GetEdge(temp, gx.width_, gx.height_, x, y - 1);
edge_nex = GetEdge(temp, gx.width_, gx.height_, x, y + 1);
if (edge_value > edge_pre && edge_value >= edge_nex) {
edges_ptr[y * gx.width_ + x] = temp[y * gx.width_ + x];
} else {
edges_ptr[y * gx.width_ + x] = 0.f;
}
} else if (gx_value * gy_value < 0) {
edge_pre = GetEdge(temp, gx.width_, gx.height_, x + 1, y - 1);
edge_nex = GetEdge(temp, gx.width_, gx.height_, x - 1, y + 1);
if (edge_value > edge_pre && edge_value > edge_nex) {
edges_ptr[y * gx.width_ + x] = temp[y * gx.width_ + x];
} else {
if (gx_value * gy_value < 0) {
edge_pre = GetEdge(temp, gx.width_, gx.height_, x + 1, y - 1);
edge_nex = GetEdge(temp, gx.width_, gx.height_, x - 1, y + 1);
} else {
edges_ptr[y * gx.width_ + x] = 0.f;
edge_pre = GetEdge(temp, gx.width_, gx.height_, x - 1, y - 1);
edge_nex = GetEdge(temp, gx.width_, gx.height_, x + 1, y + 1);
}
} else {
edge_pre = GetEdge(temp, gx.width_, gx.height_, x - 1, y - 1);
edge_nex = GetEdge(temp, gx.width_, gx.height_, x + 1, y + 1);
if (edge_value > edge_pre && edge_value > edge_nex) {
edges_ptr[y * gx.width_ + x] = temp[y * gx.width_ + x];
} else {
@@ -194,7 +186,6 @@ static void NonMaximumSuppression(const LiteMat &gx, const LiteMat &gy, LiteMat

static void Hysteresis(const LiteMat &edges, uint8_t *dst, double low_thresh, double high_thresh) {
const float *edges_ptr = edges;
uint8_t *dst_ptr = dst;

int size = edges.height_ * edges.width_;
std::vector<int> stack;
@@ -239,9 +230,9 @@ static void Hysteresis(const LiteMat &edges, uint8_t *dst, double low_thresh, do

for (int i = 0; i < size; i++) {
if (buffer[i] == 2) {
dst_ptr[i] = 255;
dst[i] = 255;
} else {
dst_ptr[i] = 0;
dst[i] = 0;
}
}
}
@@ -254,7 +245,7 @@ bool Canny(const LiteMat &src, LiteMat &dst, double low_thresh, double high_thre
if (low_thresh < 0 || high_thresh < 0 || low_thresh > high_thresh) {
return false;
}
if ((ksize & 1) == 0 || ksize < 3 || ksize > 7) {
if (ksize % 2 == 0 || ksize < 3 || ksize > 7) {
return false;
}
if (dst.IsEmpty() || dst.width_ != src.width_ || dst.height_ != src.height_ || dst.channel_ != src.channel_ ||
@@ -276,6 +267,5 @@ bool Canny(const LiteMat &src, LiteMat &dst, double low_thresh, double high_thre
Hysteresis(edges, dst, low_thresh, high_thresh);
return true;
}

} // namespace dataset
} // namespace mindspore

+ 6
- 9
mindspore/ccsrc/minddata/dataset/kernels/image/lite_cv/gaussian_blur.cc View File

@@ -28,7 +28,6 @@

namespace mindspore {
namespace dataset {

static void GetGaussianKernel(float *kernel, int size, double sigma) {
int n = (size - 1) / 2;
std::vector<float> buffer(n);
@@ -44,16 +43,15 @@ static void GetGaussianKernel(float *kernel, int size, double sigma) {
sum += 1;
}

float scale = 1. / sum;
float *ptr = kernel;
const float scale = 1. / sum;
for (int i = 0; i < n; i++) {
float g = buffer[i] * scale;
ptr[i] = g;
ptr[size - 1 - i] = g;
kernel[i] = g;
kernel[size - 1 - i] = g;
}
ptr[n] = scale;
if ((size & 1) == 0) {
ptr[n + 1] = scale;
kernel[n] = scale;
if (size % 2 == 0) {
kernel[n + 1] = scale;
}
}

@@ -85,6 +83,5 @@ bool GaussianBlur(const LiteMat &src, LiteMat &dst, const std::vector<int> &ksiz

return ConvRowCol(src, kx, ky, dst, src.data_type_, pad_type);
}

} // namespace dataset
} // namespace mindspore

+ 8
- 9
mindspore/ccsrc/minddata/dataset/kernels/image/lite_cv/image_process.cc View File

@@ -261,7 +261,6 @@ static bool Conv2DImplement(const LiteMat &src, const LiteMat &kernel, T2 *dst,

const T1 *pad_ptr = pad_mat;
const float *kernel_ptr = kernel;
T2 *dst_ptr = dst;

int pad_step = pad_mat.width_ * pad_mat.channel_;
int dst_step = src.width_ * src.channel_;
@@ -277,9 +276,9 @@ static bool Conv2DImplement(const LiteMat &src, const LiteMat &kernel, T2 *dst,
}
}
if (dst_type == LDataType::UINT8) {
dst_ptr[(y - border_y) * dst_step + (x - border_x) * src.channel_] = clip(conv_sum);
dst[(y - border_y) * dst_step + (x - border_x) * src.channel_] = clip(conv_sum);
} else {
dst_ptr[(y - border_y) * dst_step + (x - border_x) * src.channel_] = conv_sum;
dst[(y - border_y) * dst_step + (x - border_x) * src.channel_] = conv_sum;
}
}
}
@@ -300,13 +299,13 @@ static bool Conv2DImplement(const LiteMat &src, const LiteMat &kernel, T2 *dst,
}
}
if (dst_type == LDataType::UINT8) {
dst_ptr[(y - border_y) * dst_step + (x - border_x) * src.channel_] = clip(conv_sum_b);
dst_ptr[(y - border_y) * dst_step + (x - border_x) * src.channel_ + 1] = clip(conv_sum_g);
dst_ptr[(y - border_y) * dst_step + (x - border_x) * src.channel_ + 2] = clip(conv_sum_r);
dst[(y - border_y) * dst_step + (x - border_x) * src.channel_] = clip(conv_sum_b);
dst[(y - border_y) * dst_step + (x - border_x) * src.channel_ + 1] = clip(conv_sum_g);
dst[(y - border_y) * dst_step + (x - border_x) * src.channel_ + 2] = clip(conv_sum_r);
} else {
dst_ptr[(y - border_y) * dst_step + (x - border_x) * src.channel_] = conv_sum_b;
dst_ptr[(y - border_y) * dst_step + (x - border_x) * src.channel_ + 1] = conv_sum_g;
dst_ptr[(y - border_y) * dst_step + (x - border_x) * src.channel_ + 2] = conv_sum_r;
dst[(y - border_y) * dst_step + (x - border_x) * src.channel_] = conv_sum_b;
dst[(y - border_y) * dst_step + (x - border_x) * src.channel_ + 1] = conv_sum_g;
dst[(y - border_y) * dst_step + (x - border_x) * src.channel_ + 2] = conv_sum_r;
}
}
}


Loading…
Cancel
Save