Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Image resize fix #1385

Closed
wants to merge 2 commits into from
Closed

Conversation

Beerwalker
Copy link

Fix for incorrect image resize. Image was not "letterboxed"(just resized with aspect ratio distortions) when using API function Detector::detect from yolo_v2_class.cpp.
Since resize is already performed inside Detector::detect, functions detect_resize and mat_to_image_resize were removed.

@AlexeyAB
Copy link
Owner

We shouldn't use letterbox_image() and resize_image() functions from the Darknet.

Fix for incorrect image resize. Image was not "letterboxed"(just resized with aspect ratio distortions) when using API function Detector::detect from yolo_v2_class.cpp.

  1. There are pros and cons for each of 3 resize approaches: Resizing : keeping aspect ratio, or not #232 (comment)
  2. For the most of my cases the resize() is better than letterbox()
  3. Since we use resize() for Training in this repo, then we should use resize() for Detection too.

Since resize is already performed inside Detector::detect, functions detect_resize and mat_to_image_resize were removed.

  • this function cv::resize() is more than 4x times faster than resize_image() from Darknet:

    std::shared_ptr<image_t> mat_to_image_resize(cv::Mat mat) const
    {
    if (mat.data == NULL) return std::shared_ptr<image_t>(NULL);
    cv::Size network_size = cv::Size(get_net_width(), get_net_height());
    cv::Mat det_mat;
    if (mat.size() != network_size)
    cv::resize(mat, det_mat, network_size);
    else
    det_mat = mat; // only reference is copied
    return mat_to_image(det_mat);
    }

  • after the image has been resized to the network size by using cv::resize(), there will not be called function resize_image(), just will be called memcpy() that has overhead less than 0.0...01%:

    memcpy(sized.data, im.data, im.w*im.h*im.c * sizeof(float));

@Beerwalker
Copy link
Author

Thanks for a reply.

We shouldn't use letterbox_image() and resize_image() functions from the Darknet.

Ok, i accept that (however this approach isn't so great in my case). Have you considered using other interpolation methods for OpenCV cv::resize()? Since we usually downsample the common practice is to use CV_INTER_AREA for this task to obtain better image quality then CV_INTER_LINEAR. For upsampling CV_INTER_CUBIC is often used.
If you are interested in different interpolation methods for OpenCV you might want to look at this quick reference:
http://tanbakuchi.com/posts/comparison-of-openv-interpolation-algorithms/

@Beerwalker Beerwalker closed this Aug 16, 2018
@shubham-shahh
Copy link

We shouldn't use letterbox_image() and resize_image() functions from the Darknet.

Fix for incorrect image resize. Image was not "letterboxed"(just resized with aspect ratio distortions) when using API function Detector::detect from yolo_v2_class.cpp.

  1. There are pros and cons for each of 3 resize approaches: #232 (comment)
  2. For the most of my cases the resize() is better than letterbox()
  3. Since we use resize() for Training in this repo, then we should use resize() for Detection too.
  • functions resize_image() and letterbox_image() from Darknet are very slow, they are a bottleneck for performance, that is why I accelerated 4x times FPS for detection on video (FullHD/4K): #529 (comment)
    by implementing resizing by using OpenCV cv::resize() function for the detector demo:
    cvResize(src, *in_img, CV_INTER_LINEAR);

Since resize is already performed inside Detector::detect, functions detect_resize and mat_to_image_resize were removed.

  • this function cv::resize() is more than 4x times faster than resize_image() from Darknet:
    std::shared_ptr<image_t> mat_to_image_resize(cv::Mat mat) const
    {
    if (mat.data == NULL) return std::shared_ptr<image_t>(NULL);
    cv::Size network_size = cv::Size(get_net_width(), get_net_height());
    cv::Mat det_mat;
    if (mat.size() != network_size)
    cv::resize(mat, det_mat, network_size);
    else
    det_mat = mat; // only reference is copied
    return mat_to_image(det_mat);
    }
  • after the image has been resized to the network size by using cv::resize(), there will not be called function resize_image(), just will be called memcpy() that has overhead less than 0.0...01%:
    memcpy(sized.data, im.data, im.w*im.h*im.c * sizeof(float));

Does it resize the image with black paddings or just increase or decrease the height and width?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants