0

I'm trying to read and plot an image. However, when I run my 2-line code, the plot is different than the original image. My python version is 3.8.5.

auxImg = (cv2.imread("train/img_0001.jpg",cv2.IMREAD_COLOR))/255
plt.imshow(auxImg)

I also tried without the /255, but it gives the same result.

Original image:

Plotted image:Plotted image

martineau
  • 119,623
  • 25
  • 170
  • 301
Bia
  • 305
  • 3
  • 10
  • 2
    Duplicate: [OpenCV giving wrong color to colored images on loading](https://stackoverflow.com/questions/39316447/opencv-giving-wrong-color-to-colored-images-on-loading) – HansHirse Jun 11 '21 at 08:29

2 Answers2

2

Because cv2 opens image in BGR mode. You need to use cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

kacpo1
  • 525
  • 5
  • 17
0

The cv2 module reads in images in BGR format, while the matplotlib module uses RGB. A simple fix would be to use the cv2.cvtColor() method on the image first:

auxImgRGB = cv2.cvtColor(auxImg, cv2.COLOR_BGR2RGB)

The COLOR_BGR2RGB mode is 4.

Red
  • 26,798
  • 7
  • 36
  • 58