未验证 提交 50822e44 编写于 作者: H HappyAngel 提交者: GitHub

[cv] fix resize error (#3178)


* fix resie bug

* fix format, test=develop

* fix memory bug, test=develop

* fix format. test=develop
上级 bb217292
......@@ -489,7 +489,7 @@ void image_resize_basic(const uint8_t* in_data,
int size = srcw * srch;
if (srcw == dstw && srch == dsth) {
if (srcFormat == ImageFormat::NV12 || srcFormat == ImageFormat::NV21) {
size = srcw * (ceil(1.5 * srch));
size = srcw * (static_cast<int>(1.5 * srch));
} else if (srcFormat == ImageFormat::BGR || srcFormat == ImageFormat::RGB) {
size = 3 * srcw * srch;
} else if (srcFormat == ImageFormat::BGRA ||
......@@ -499,23 +499,23 @@ void image_resize_basic(const uint8_t* in_data,
memcpy(out_data, in_data, sizeof(uint8_t) * size);
return;
}
double scale_x = static_cast<double>(srcw / dstw);
double scale_y = static_cast<double>(srch / dsth);
double scale_x = static_cast<double>(srcw) / dstw;
double scale_y = static_cast<double>(srch) / dsth;
int* buf = new int[dstw + dsth];
int* xofs = buf;
int* yofs = buf + dstw;
float* ialpha = new float[dstw * 2];
float* ibeta = new float[dsth * 2];
float* ibeta = new float[dsth * 3];
int w_in = srcw;
int w_out = dstw;
int num = 1;
int orih = dsth;
compute_xy(
srcw, srch, dstw, dsth, scale_x, scale_y, xofs, yofs, ialpha, ibeta);
if (srcFormat == ImageFormat::GRAY) {
num = 1;
} else if (srcFormat == ImageFormat::NV12 || srcFormat == ImageFormat::NV21) {
......@@ -538,9 +538,9 @@ void image_resize_basic(const uint8_t* in_data,
int* yofs1 = nullptr;
if (orih < dsth) {
int tmp = dsth - orih;
float* ialpha1 = new float[dstw];
int* xofs1 = new int[srcw];
int* yofs1 = new int[tmp];
ialpha1 = new float[srcw];
xofs1 = new int[dstw / 2];
yofs1 = new int[tmp];
compute_xy(srcw / 2,
srch / 2,
dstw / 2,
......@@ -550,18 +550,14 @@ void image_resize_basic(const uint8_t* in_data,
xofs1,
yofs1,
ialpha1,
ibeta + dsth);
ibeta + orih * 2);
}
#pragma omp parallel for
for (int dy = 0; dy < dsth; dy++) {
uint8_t* out_ptr = out_data + dy * w_out;
int y_in_start = yofs[dy];
int y_in_end = y_in_start + 1;
int y_flag = 0; // only one line
if (y_in_start < 0) {
y_flag = 1;
y_in_end = 0;
}
int y_flag = 0;
float b0 = ibeta[dy * 2];
float b1 = ibeta[dy * 2 + 1];
if (dy >= orih) {
......@@ -569,6 +565,12 @@ void image_resize_basic(const uint8_t* in_data,
ialpha = ialpha1;
xofs = xofs1;
yofs = yofs1;
y_in_start = yofs[dy - orih];
}
int y_in_end = y_in_start + 1;
if (y_in_start < 0) {
y_flag = 1;
y_in_end = 0;
}
for (int dx = 0; dx < w_out; dx += num) {
int tmp = dx / num;
......@@ -579,7 +581,6 @@ void image_resize_basic(const uint8_t* in_data,
x_flag = 1;
x_in_end = 0;
}
// printf("x_in: %d, y_in: %d \n", x_in_start, y_in_start);
float a0 = ialpha[tmp * 2];
float a1 = ialpha[tmp * 2 + 1];
int tl_index = y_in_start * w_in + x_in_start; // 0
......@@ -605,9 +606,6 @@ void image_resize_basic(const uint8_t* in_data,
bl_index++;
br_index++;
float outval = (tl * a0 + tr * a1) * b0 + (bl * a0 + br * a1) * b1;
// printf("tl: %d, tr: %d, bl: %d, br: %d \n", tl, tr, bl, br);
// printf("br_index: %d, a0: %f, b1: %f, out: %f \n", br_index, a0, b1,
// outval);
out_ptr[ind++] = ceil(outval);
}
}
......
此差异已折叠。
......@@ -110,7 +110,8 @@ rotate:
*/
void flip_hwc1_x(const uint8_t* src, uint8_t* dst, int w_in, int h_in) {
int h = h_in - 1;
uint8_t zerobuff[8] = {0, 0, 0, 0, 0, 0, 0, 0};
uint8_t* zerobuff = new uint8_t[w_in];
memset(zerobuff, 0.0, sizeof(uint8_t) * w_in);
#pragma omp parallel for
for (int i = 0; i < h_in; i += 4) {
const uint8_t* inptr0 = src + i * w_in;
......@@ -233,7 +234,8 @@ flip:
*/
void flip_hwc1_y(const uint8_t* src, uint8_t* dst, int w_in, int h_in) {
int64_t stride_w = 8;
uint8_t zerobuff[8] = {0, 0, 0, 0, 0, 0, 0, 0};
uint8_t* zerobuff = new uint8_t[w_in];
memset(zerobuff, 0.0, sizeof(uint8_t) * w_in);
#pragma omp parallel for
for (int i = 0; i < h_in; i += 4) {
const uint8_t* inptr0 = src + i * w_in;
......@@ -386,7 +388,8 @@ flip:
*/
void flip_hwc1_xy(const uint8_t* src, uint8_t* dst, int w_in, int h_in) {
int64_t stride_w = 8;
uint8_t zerobuff[8] = {0, 0, 0, 0, 0, 0, 0, 0};
uint8_t* zerobuff = new uint8_t[w_in];
memset(zerobuff, 0.0, sizeof(uint8_t) * w_in);
#pragma omp parallel for
for (int i = 0; i < h_in; i += 4) {
const uint8_t* inptr0 = src + i * w_in;
......@@ -398,17 +401,17 @@ void flip_hwc1_xy(const uint8_t* src, uint8_t* dst, int w_in, int h_in) {
uint8_t* outptr1 = outptr0 - w_in;
uint8_t* outptr2 = outptr1 - w_in;
uint8_t* outptr3 = outptr2 - w_in;
if (i + 3 >= h_in) {
switch ((i + 3) - h_in) {
if (i + 4 > h_in) {
switch ((i + 4) - h_in) {
case 3:
inptr0 = zerobuff;
outptr0 = zerobuff;
case 2:
inptr1 = zerobuff;
outptr1 = zerobuff;
case 1:
case 2:
inptr2 = zerobuff;
outptr2 = zerobuff;
case 1:
inptr3 = zerobuff;
outptr3 = zerobuff;
case 0:
inptr3 = zerobuff;
outptr3 = zerobuff;
......@@ -504,16 +507,16 @@ void flip_hwc1_xy(const uint8_t* src, uint8_t* dst, int w_in, int h_in) {
outptr1 += stride_w - 1;
outptr0 += stride_w - 1;
for (; j < w_in; j++) {
if (i + 3 >= h_in) {
switch ((i + 3) - h_in) {
case 0:
if (i + 4 > h_in) {
switch ((i + 4) - h_in) {
case 3:
*outptr2-- = *inptr2++;
case 1:
case 2:
*outptr1-- = *inptr1++;
// inptr1 = zerobuff;
case 2:
case 1:
*outptr0-- = *inptr0++;
case 3:
case 0:
// inptr3 = zerobuff;
default:
break;
......
......@@ -69,7 +69,7 @@ void resize(const uint8_t* src,
int size = srcw * srch;
if (srcw == dstw && srch == dsth) {
if (srcFormat == NV12 || srcFormat == NV21) {
size = srcw * (floor(1.5 * srch));
size = srcw * (static_cast<int>(1.5 * srch));
} else if (srcFormat == BGR || srcFormat == RGB) {
size = 3 * srcw * srch;
} else if (srcFormat == BGRA || srcFormat == RGBA) {
......@@ -81,7 +81,7 @@ void resize(const uint8_t* src,
double scale_x = static_cast<double>(srcw) / dstw;
double scale_y = static_cast<double>(srch) / dsth;
int* buf = new int[dstw * 2 + dsth * 2];
int* buf = new int[dstw * 2 + dsth * 3];
int* xofs = buf;
int* yofs = buf + dstw;
......@@ -110,7 +110,7 @@ void resize(const uint8_t* src,
}
compute_xy(
srcw, srch, dstw, dsth, num, scale_x, scale_y, xofs, yofs, ialpha, ibeta);
srcw, srch, dstw, orih, num, scale_x, scale_y, xofs, yofs, ialpha, ibeta);
int* xofs1 = nullptr;
int* yofs1 = nullptr;
......@@ -131,7 +131,7 @@ void resize(const uint8_t* src,
xofs1,
yofs1,
ialpha1,
ibeta + orih);
ibeta + orih * 2);
}
int cnt = w_out >> 3;
int remain = w_out % 8;
......@@ -160,7 +160,6 @@ void resize(const uint8_t* src,
int sx = xofs[dx / num];
int16_t a0 = ialphap[0];
int16_t a1 = ialphap[1];
const uint8_t* S0pl = S0 + sx;
const uint8_t* S0pr = S0 + sx + num;
const uint8_t* S1pl = S1 + sx;
......@@ -323,7 +322,6 @@ void compute_xy(int srcw,
fy = static_cast<float>((dy + 0.5) * scale_y - 0.5);
sy = floor(fy);
fy -= sy;
if (sy < 0) {
sy = 0;
fy = 0.f;
......@@ -332,12 +330,9 @@ void compute_xy(int srcw,
sy = srch - 2;
fy = 1.f;
}
yofs[dy] = sy;
float b0 = (1.f - fy) * resize_coef_scale;
float b1 = fy * resize_coef_scale;
ibeta[dy * 2] = SATURATE_CAST_SHORT(b0);
ibeta[dy * 2 + 1] = SATURATE_CAST_SHORT(b1);
}
......
......@@ -79,6 +79,7 @@ void rotate_hwc1(
void rotate_hwc3(
const uint8_t* src, uint8_t* dst, int srcw, int srch, float degree) {
if (degree == 90) {
printf("rotate_hwc3_90 \n");
rotate_hwc3_90(src, dst, srcw, srch, srch, srcw);
} else if (degree == 180) {
rotate_hwc3_180(src, dst, srcw, srch, srcw, srch);
......@@ -679,14 +680,14 @@ void rotate_hwc1_90(const uint8_t* src,
const uint8_t* inptr7 = inptr6 + w_in;
for (; j < w_in; j++) {
uint8_t* outptr = dst + j * w_out + ww - i;
*outptr++ = *inptr0++;
*outptr++ = *inptr1++;
*outptr++ = *inptr2++;
*outptr++ = *inptr3++;
*outptr++ = *inptr4++;
*outptr++ = *inptr5++;
*outptr++ = *inptr6++;
*outptr++ = *inptr7++;
*outptr++ = *inptr6++;
*outptr++ = *inptr5++;
*outptr++ = *inptr4++;
*outptr++ = *inptr3++;
*outptr++ = *inptr2++;
*outptr++ = *inptr1++;
*outptr++ = *inptr0++;
}
}
ww = w_out - 1;
......@@ -856,6 +857,7 @@ void rotate_hwc1_180(const uint8_t* src,
}
}
}
delete[] zerobuff;
}
/*
1 2 3
......@@ -1386,6 +1388,7 @@ void rotate_hwc3_180(const uint8_t* src,
}
}
}
delete[] zerobuff;
}
void rotate_hwc3_270(const uint8_t* src,
......@@ -1915,6 +1918,7 @@ void rotate_hwc4_180(const uint8_t* src,
}
}
}
delete[] zerobuff;
}
void rotate_hwc4_270(const uint8_t* src,
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册