2

The thing is, I have this code:

import numpy as np
import cv2
from matplotlib import pyplot
img = cv2.imread('C:\Users\Niranjan\Desktop\FINAL YEAR PROJECT\Python Codes\obj.jpg')
rows,cols,ch = img.shape
pts1 = np.float32([[170,220],[466,221],[110,540],[528,541]])
pts2 = np.float32([[0,0],[530,0],[0,542],[530,542]])
M = cv2.getPerspectiveTransform(pts1,pts2)
dst = cv2.warpPerspective(img,M,(530,542))
pyplot.subplot(121),pyplot.imshow(img),pyplot.title('Input')
pyplot.subplot(122),pyplot.imshow(dst),pyplot.title('Output')
pyplot.show()

the original image

The result of the python code

So blue is getting converted to yellow... is this a hint that RGB is converted to CMYK?

what should i do to keep my colors same as original?

*note: This is not an ad

Roland Smith
  • 42,427
  • 3
  • 64
  • 94
Niranjan Dixit
  • 115
  • 3
  • 12

2 Answers2

3

For historical reasons, OpenCV uses a BGR color representation. You can convert your images before displaying them using:

image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
Community
  • 1
  • 1
Étienne Millon
  • 3,018
  • 11
  • 27
3

Use img = img[:,:,::-1] to shuffle the color channels form BGR to RGB.

pyplot.subplot(121),pyplot.imshow(img[:,:,::-1] ),pyplot.title('Input')
pyplot.subplot(122),pyplot.imshow(dst[:,:,::-1] ),pyplot.title('Output')
pyplot.show()

Here is the matplotlib input and output images.

enter image description here

thewaywewere
  • 8,128
  • 11
  • 41
  • 46