Ferdous Hussain
Ferdous Hussain

Reputation: 73

While saving images the color on the saved jpg file is incorrect(cv2.COLOR_BGR2RGB does not fix it)

My program is supposed to take a picture and then store it in a specified folder. It is doing all that, however the color on the stored jpg file is far from the image it displays. I would like to correct this.

i have tried cv2.COLOR_BGR2HSV but I think RGB is the way to go. Please help.

import cv2
import matplotlib.pyplot as  plt
import sys
def main():
    cap = cv2.VideoCapture(0)

    if cap.isOpened():
        ret, frame=cap.read()
        print(ret)
        print(frame)
    else:
        ret = False

    #I AM TRYING TO CONVERT THE COLOR USING THIS LINE OF CODE
    img1 = cv2.cvtColor(frame,cv2.COLOR_BGR2RGB)

    plt.imshow(img1)
    plt.title("Car Image")
    plt.xticks([])
    plt.yticks([])
    plt.show()

    cv2.imwrite("C:/Users/Fahim/PycharmProjects/CarPark/Car_Image.jpg", img1);





    cap.release()



if __name__ == "__main__":
    main()

I would like to store the image as it is displayed

Upvotes: 1

Views: 305

Answers (1)

J_H
J_H

Reputation: 20570

The OpenCV and Matplotlib conventions are different, as they use BGR and RGB respectively.

You may find it helpful to rename variables, e.g. to frame_bgr. An explicit notation will help you remember when the 1st & 3rd channels should be swapped as you go back and forth between using routines from one package or the other.

The conversion you posted is perfectly nice. You might choose to assign the result e.g. to img_rgb.

If you're mostly making cv2 calls, then when writing your own functions you might choose to adopt a BGR convention for all inputs and outputs, to minimize conversions.

Upvotes: 1

Related Questions