question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Resize mask should not use nearest method of opencv

See original GitHub issue

I’m using this project to train my segmentation model. I find that the mask has a right-down offset to the image. Because the opencv resize_nearest is wrong. Please refer the opencv project issue:
https://github.com/opencv/opencv/issues/9096 https://github.com/opencv/opencv/issues/10146

The code of opencv is: for( x = 0; x < dsize.width; x++ ) { int sx = cvFloor(x*ifx); x_ofs[x] = std::min(sx, ssize.width-1)*pix_size; } It’s not the nearest, so it will be a offset between image and mask.

Mask uses the resize nearest in Pillow has no offset to the image. Using the resize nearest of opencv to train segmentation model will have a bad result.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:5
  • Comments:11 (2 by maintainers)

github_iconTop GitHub Comments

8reactions
creafzcommented, Mar 5, 2021

I do agree that we need a way to fix the current behavior of OpenCV’s nearest interpolation that we use for masks.

However, we need a way to inform the current users of different behaviors of cv2.INTER_NEAREST and cv2.INTER_NEAREST_EXACT and let them choose the one they prefer. By following this path, we will not introduce a sudden significant change that will break the current behavior of augmentation pipelines (even if the current behavior is not optimal and may be seen as ‘buggy’, it is still how Albumentations resizes masks since the first release).

So my proposal is the following:

  1. Add a way for users to specify the interpolation algorithm for masks. For example, we could use a parameter for A.Compose, e.g.
transform = A.Compose([...], mask_interpolation=cv2.INTER_NEAREST_EXACT)
  1. In the nearest (no pun intended) release of Albumentations, add a warning which says that the next release will use cv2.INTER_NEAREST_EXACT instead of cv2.INTER_NEAREST for mask interpolation, and to preserve the old behavior, the user must explicitly specify the mask interpolation method for an augmentation pipeline. Let’s make a separate article in the documentation that tells the difference between those interpolation methods.
  2. After that, in a new release, we will change the default interpolation method to cv2.INTER_NEAREST_EXACT, but still, show a warning if the user didn’t specify the interpolation method and Albumentations used the default value.
0reactions
karthik1145commented, Aug 17, 2021

Can I try to fix it?

On Tue, Aug 17, 2021, 10:20 PM Mikhail Druzhinin @.***> wrote:

Unfortunately, this issue has not been fixed yet.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/albumentations-team/albumentations/issues/850#issuecomment-900461886, or unsubscribe https://github.com/notifications/unsubscribe-auth/ALPCLOOA4RNVQB7XAKMYJULT5KHNJANCNFSM4YTPK54A . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&utm_campaign=notification-email .

Read more comments on GitHub >

github_iconTop Results From Across the Web

python - How to resize a labeled mask with nearest neighbor ...
It is very important to use 'nearest neighbor' interpolation when resizing segmentation masks. Does anyone know how to do this with scikit-image ...
Read more >
Image Resizing with OpenCV - LearnOpenCV
Come, let's learn about image resizing with OpenCV. To resize an image, scale it along each axis (height and width), considering the specified...
Read more >
OpenCV Resize Image ( cv2.resize ) - PyImageSearch
In this tutorial, you will learn how to resize an image using OpenCV and the cv2.resize function.
Read more >
Geometric Image Transformations - OpenCV
Resizes an image. ... They do not change the image content but deform the pixel grid and map ... In this case, an...
Read more >
Different Interpolation methods in OpenCV - OpenGenus IQ
In OpenCV Python, Interpolation method is used while resizing an image as ... Unlike INTER_NEAREST, this does the interpolation in two dimensions and ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found