-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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
Pillow
vs cv2
resize
#2718
Comments
Case 1: Nearest neighbor is a fast, low quality, best effort interpolation. That said, I believe that our tests show our implementation is reasonably correct. a mean difference of 71 would fall into the range of seriously visually different, and probably the result of either a rotation or channel swapping. Case 2. That appears to be 1 pixel level difference in 10^6 pixels. That's pretty close, and well within what I'd consider reasonable for a lossy format with potentially different implementations and settings on the decoder. Feel free to point out where you feel that the implementation is incorrect. |
For case 1
I use this image: |
Which one do you think is correct? |
I do not know. I am just stating a fact that there is a difference. |
Visually they are also significantly different: Please, look at the sample: This image with the uniform gradient (from 100% white to 100% black) allows us to find out which pixels are used by each library. This is the same image after resizing to (3, 3). Left is CV2, right is Pillow: OpenCV uses the topmost left white pixel from the source image, but the bottommost right pixel on the result is too bright. Here are maps of pixels on the source image: It's clear to me there are some problems with rounding in OpenCV there. |
Hi. Nice visualization. But I find it strange that sometimes neural network's predictions |
I'm not familiar with neural network's, but as I understand this is called "overlearning". This is the case when the network shows good results on the test data, but doesn't accept any deviations. |
I tested that Pillow resize has the same result as Matlab function "resize", The output image size is a little bit difference, maybe Matlab and Pillow use different rounding operation from float to int. But this doesn't matter. I guess Pillow used an anti-aliasing filter together with down-sampling filter, because by default Matlab will apply an anti-aliasing filter. |
Just for completeness, as described at https://stackoverflow.com/questions/21997094/why-opencv-cv2-resize-gives-different-answer-than-matlab-imresize/21998119, Matlab's imresize by default performs antialiasing when downsampling, which is why you see different results. |
In case 1 print((b - a).mean())
# 71.1218444444 This is because the numpy array's dtype is uint8 a = np.array([1], dtype='uint8')
b = np.array([2], dtype='uint8')
a - b
# array([255], dtype=uint8)
# 1 - 2 = -1 = 255 in uint8 So, we can transform it to int16 a_i = np.int16(a)
b_i = np.int16(b)
print(np.abs(b_i - a_i).mean())
# 0.028748148148148148
# which is slight difference |
Results of reading and resizing can be different in
cv2
andPilllow
. This creates problems when you want to reuse a model (neural network) trained usingcv2
withPillow
.Case 1
Different results of resizing an image.
Case 2
Different results of reading an image.
I use:
Python 3.6
,Pillow 4.2.1
,opencv-python 3.3
The text was updated successfully, but these errors were encountered: