plt.imshow() expects integers.
However, I noticed that even though two arrays of integers can look identical, they might have different types of integers.
In my case, when looking at integer type in the "right" array (i.e. the one where the colors display properly) I got: class 'numpy.uint8'
Whereas when looking at integer type in the array where the colors are inverted in plt.imshow() I got: class 'numpy.int64'
All I needed to do was to convert the array values to uint8:
image = image.astype(np.uint8)
Answer from Alexis Saint-Jean on Stack Overflowplt.imshow() expects integers.
However, I noticed that even though two arrays of integers can look identical, they might have different types of integers.
In my case, when looking at integer type in the "right" array (i.e. the one where the colors display properly) I got: class 'numpy.uint8'
Whereas when looking at integer type in the array where the colors are inverted in plt.imshow() I got: class 'numpy.int64'
All I needed to do was to convert the array values to uint8:
image = image.astype(np.uint8)
Not at all. You are dealing with numpy arrays and as so you can just:
freq = 255 - freq
So your code would be:
import numpy as np
from scipy import fftpack
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
image = mpimg.imread("EGZ68.jpg")
freq = fftpack.fft2(image)
freq = 255 - freq # HERE IS THE CHANGE
IFFT = fftpack.ifft2(freq)
IFFT = IFFT.astype('float32')
plt.figure(1)
plt.imshow(image)
plt.figure(2)
plt.imshow(IFFT)
plt.show()
Although I'm not sure why you are getting a an inversed colormap in the first place.
python - Reverse colormap in matplotlib - Stack Overflow
How to invert an plot
python - Invert colors when plotting a PNG file using matplotlib - Stack Overflow
matplotlib - imshow inverts colors after using astype(float) - Stack Overflow
It's not default, but you can invert the colors by subtract the array from 1:
plt.imshow(1 - array)
As suggested by @tacaswell in the comments of OP's question, you may use the color map gray_r in matplotlib pyplot's imshow method for having a reverse gray plot. Alternatively, you may use color maps binary and gist_yarg (as shown in matplotlib's documentation). Just a curiosity: notice the pun in the term "gist_yarg": "yarg" is "gray" written in the reverse order.
As an illustrative example, to plot OP's image im with reverse gray colormap, one could do:
from PIL import Image
import matplotlib.pyplot as plt
im = array(Image.open('empire.jpg').convert('L'))
plt.imshow(X=im, cmap="gray_r")
The standard colormaps also all have reversed versions. They have the same names with _r tacked on to the end. (Documentation here.)
The solution is pretty straightforward. Suppose you want to use the "autumn" colormap scheme. The standard version:
cmap = matplotlib.cm.autumn
To reverse the colormap color spectrum, use get_cmap() function and append '_r' to the colormap title like this:
cmap_reversed = matplotlib.cm.get_cmap('autumn_r')
It appears that you may somehow have RGB switched with BGR. Notice that your greens are retained but all the blues turned to red. If cube has shape (M,N,3), try swapping cube[:,:,0] with cube[:,:,2]. You can do that with numpy like so:
rgb = numpy.fliplr(cube.reshape(-1,3)).reshape(cube.shape)
From the OpenCV documentation:
Note: In the case of color images, the decoded images will have the channels stored in B G R order.
Try:
plt.imshow(cv2.cvtColor(cube, cv2.COLOR_BGR2RGB))
I am starting to get into computer vision, I installed opencv and matplotlib to test, but when I try to see the images, the images appear as if the colors are inverted, but the only thing I do is to read and display the image.
Try several things like: img = cv.imread(image_four, cv.IMREAD_COLOR) or img = cv.cvtColor(img, cv.COLOR_BGR2RGB) but the colors are still displayed wrong
I am using windows 11 and python 3.11
This worked for me:
plt.imshow(lena[:,:,::-1]) # RGB-> BGR
Same idea but nicer and more robust approach is to use "ellipsis" proposed by @rayryeng:
plt.imshow(lena[...,::-1])
OpenCV represents the images in BGR as opposed to the RGB we expect. Since it is in the reverse order, you tend to see the blue color in images. Try using the following line (below comment in code) for converting from BGR to RGB:
import cv2
import matplotlib.pyplot as plt
lena=cv2.imread("lena.jpg")
#plt.imshow(lena)
#plt.axis("off")
#Converts from BGR to RGB
plt.imshow(cv2.cvtColor(lena, cv2.COLOR_BGR2RGB))
plt.show()