Sj03rd
Sj03rd

Reputation: 71

Changing images from BGR to RGB

I used the code below to plot the pictures that were predicted wrong. When the pictures are displayed I see that the pictures seem to be in the format BGR.

I found methods to do this for a single picture but not for a list of pictures Is there a way how I can change the colors to RGB within this block of code?

this is the code I used to plot the images:

plt.figure(figsize=(15,10))
for i in range(10):
    err_index = errors[errors['error']==1].index[i]
    plt.subplot(2,5,i+1)
    plt.xticks([])
    plt.yticks([])
    plt.grid(False)
    plt.imshow(X_test[err_index].reshape(224,224,3))
    plt.xlabel("ture is {}, predicted as {}".format(np.argmax(y_test[err_index]), y_pred[err_index]))

Edit-- Sorry pasted the wrong code. I tried to add a line with cv2.COLOR_BGR2RGB but that didn't work. Really new to all this.

Upvotes: 2

Views: 5358

Answers (1)

Alex
Alex

Reputation: 344

I am pretty sure this has been asked, and answered, several times, but it's actually faster for me to put this here than to search: since you're using numpy, this:

img_rgb = img_bgr[:, :, ::-1]

Also, given you're getting a BGR format, you're probably using OpenCV, so also this:

img_rgb = cv2.cvtColor(img_bgr, cv2.COLOR_BGR2RGB)

See here.

Upvotes: 13

Related Questions