Issue
I am trying to learn OpenCV and I am new to these things(Python, OpenCV).
Here is the link that i am following And here is the part i don't understand
In lesson 1. There is a subject about why we should use RGB and BGR conversion. I understand that matplotlib is using RGB and OpenCV is using BGR (Correct me if i'm wrong). But couldn't get how to convert BGR to RGB with this code:
coke_img_channels_reversed = coke_img[:, :, ::-1]
plt.imshow(coke_img_channels_reversed)
And I couldn't tell if the transformation is done exactly in matrix or array. How this code change order of BGR to RGB. Can anyone explain it mathematically or logically?
Solution
In the case of color channels in the last dimension, think of it like this: you have three matrices B (mxn), G (mxn), and R (mxn) that are "stacked" on top of each other with B at the bottom, G in the middle, and R on top. If they were truly separate matrices you would stack them in numpy like this
bgr_image = np.concatenate((B[:, :, None], G[:, :, None], R[:, :, None]), axis=2)
When you are doing the operation
rgb_image = bgr_image[:, :, ::-1]
you are flipping that BGR stack so that now R is on bottom, G in the middle, and B on top. From the perspective of array operation (the slice notation): bgr_image[:, :, ::-1] is all the "rows" (dim0) of the bgr_image, all the "columns" (dim1) of the bgr_image, and in the "channels" (dim2) take the last channel first and build back one-by-one all the way to the first channel (effectively reversing the channel order from BGR to RGB). The operation could be rewritten more explicitly as
rgb_image = bgr_image[0::1, 0::1, -1::-1]
or if you wanted to go way too far in explicitness you could do
m, n, c = bgr_image.shape
rgb_image = bgr_image[0:m:1, 0:n:1, -1::-1]
OpenCV just makes it a convenience function even though the operation is simple. They also try to mirror the C++ library pretty close, where in C++ the matrix shifting is not quite a simple and the BGRtoRGB function is pretty convenient.
Answered By - Andrew Holmgren
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.