Last active
December 30, 2018 00:27
-
-
Save crazysal/3d437340f8906d6ba62b4653d1598e43 to your computer and use it in GitHub Desktop.
C code to crop and resize a rectangle box from an image to given dimension
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void CropAndResizePerBox( | |
const float * image_data, | |
const int batch_size, | |
const int depth, | |
const int image_height, | |
const int image_width, | |
const float * boxes_data, | |
const int * box_index_data, | |
const int start_box, | |
const int limit_box, | |
float * corps_data, | |
const int crop_height, | |
const int crop_width, | |
const float extrapolation_value | |
) { | |
const int image_channel_elements = image_height * image_width; | |
const int image_elements = depth * image_channel_elements; | |
const int channel_elements = crop_height * crop_width; | |
const int crop_elements = depth * channel_elements; | |
int b; | |
#pragma omp parallel for | |
for (b = start_box; b < limit_box; ++b) { | |
const float * box = boxes_data + b * 4; | |
const float y1 = box[0]; | |
const float x1 = box[1]; | |
const float y2 = box[2]; | |
const float x2 = box[3]; | |
const int b_in = box_index_data[b]; | |
if (b_in < 0 || b_in >= batch_size) { | |
printf("Error: batch_index %d out of range [0, %d)\n", b_in, batch_size); | |
exit(-1); | |
} | |
// height of box multiplied with scale of resize | |
const float height_scale = | |
(crop_height > 1) | |
? (y2 - y1) * (image_height - 1) / (crop_height - 1) | |
: 0; | |
// width of box multiplied with scale of resize | |
const float width_scale = | |
(crop_width > 1) | |
? (x2 - x1) * (image_width - 1) / (crop_width - 1) | |
: 0; | |
for (int y = 0; y < crop_height; ++y) | |
{ | |
const float in_y = (crop_height > 1) | |
? y1 * (image_height - 1) + y * height_scale | |
: 0.5 * (y1 + y2) * (image_height - 1); | |
if (in_y < 0 || in_y > image_height - 1) | |
{ | |
for (int x = 0; x < crop_width; ++x) | |
{ | |
for (int d = 0; d < depth; ++d) | |
{ | |
// crops(b, y, x, d) = extrapolation_value; | |
corps_data[crop_elements * b + channel_elements * d + y * crop_width + x] = extrapolation_value; | |
} | |
} | |
continue; | |
} | |
const int top_y_index = floorf(in_y); | |
const int bottom_y_index = ceilf(in_y); | |
const float y_lerp = in_y - top_y_index; | |
for (int x = 0; x < crop_width; ++x) | |
{ | |
const float in_x = (crop_width > 1) | |
? x1 * (image_width - 1) + x * width_scale | |
: 0.5 * (x1 + x2) * (image_width - 1); | |
if (in_x < 0 || in_x > image_width - 1) | |
{ | |
for (int d = 0; d < depth; ++d) | |
{ | |
corps_data[crop_elements * b + channel_elements * d + y * crop_width + x] = extrapolation_value; | |
} | |
continue; | |
} | |
const int left_x_index = floorf(in_x); | |
const int right_x_index = ceilf(in_x); | |
const float x_lerp = in_x - left_x_index; | |
for (int d = 0; d < depth; ++d) | |
{ | |
const float *pimage = image_data + b_in * image_elements + d * image_channel_elements; | |
const float top_left = pimage[top_y_index * image_width + left_x_index]; | |
const float top_right = pimage[top_y_index * image_width + right_x_index]; | |
const float bottom_left = pimage[bottom_y_index * image_width + left_x_index]; | |
const float bottom_right = pimage[bottom_y_index * image_width + right_x_index]; | |
const float top = top_left + (top_right - top_left) * x_lerp; | |
const float bottom = | |
bottom_left + (bottom_right - bottom_left) * x_lerp; | |
corps_data[crop_elements * b + channel_elements * d + y * crop_width + x] = top + (bottom - top) * y_lerp; | |
} | |
} // end for x | |
} // end for y | |
} // end for b | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment