Ask Your Question
0

Why is the order of color channels in funtion copyMakeBorder in Python version not (b, g, r) but (r, g, b)?

asked 2019-04-10 01:41:12 -0600

shxuy gravatar image

As we all know, OpenCV uses a differnt order of color channels (b, r, g,) not (r, b, g) for a historical reason. But I find that in python version, the second parameter of cv.copyMakeBorder, namely 'value', uses (r, b, g) as its order of color channels, which is inconsistent with other APIs and confused me.

So maybe there is a typo or a tiny mistake in the tutorial for Python tutorial_py_basic_ops. The tutorial provides a code snippet as shown below to teach the usage of funtion cv.copyMakeBorder.

import cv2 as cv
import numpy as np
from matplotlib import pyplot as plt
BLUE = [255,0,0]
img1 = cv.imread('opencv-logo.png')
replicate = cv.copyMakeBorder(img1,10,10,10,10,cv.BORDER_REPLICATE)
reflect = cv.copyMakeBorder(img1,10,10,10,10,cv.BORDER_REFLECT)
reflect101 = cv.copyMakeBorder(img1,10,10,10,10,cv.BORDER_REFLECT_101)
wrap = cv.copyMakeBorder(img1,10,10,10,10,cv.BORDER_WRAP)
constant= cv.copyMakeBorder(img1,10,10,10,10,cv.BORDER_CONSTANT,value=BLUE)
plt.subplot(231),plt.imshow(img1,'gray'),plt.title('ORIGINAL')
plt.subplot(232),plt.imshow(replicate,'gray'),plt.title('REPLICATE')
plt.subplot(233),plt.imshow(reflect,'gray'),plt.title('REFLECT')
plt.subplot(234),plt.imshow(reflect101,'gray'),plt.title('REFLECT_101')
plt.subplot(235),plt.imshow(wrap,'gray'),plt.title('WRAP')
plt.subplot(236),plt.imshow(constant,'gray'),plt.title('CONSTANT')
plt.show()

The author names a variable called 'BLUE' and assigns [255, 0, 0] to it which is sensible in the first glance. Then the variable 'BLUE' is used in

constant= cv.copyMakeBorder(img1,10,10,10,10,cv.BORDER_CONSTANT,value=BLUE)

in order to add a pure blue border to img1.

However, we get a pure red border at last which titled 'CONSTANT' in the bottom-right corner of the whole final result picture.

the result

So may I fork the github repository and replace the variable name 'BLUE' with 'RED' to keep semantic consistency of the tutorial?

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

answered 2019-04-10 01:46:34 -0600

berak gravatar image

updated 2019-04-10 01:48:57 -0600

the opencv code for copyMakeBorder() is all correct.

blame matplotlib for swapping bgr to rgb (and look at the logo above, to see the correct order)

(but yes, it's already wrong in the tutorial)

edit flag offensive delete link more

Comments

Thanks. But it seems that we have to modify many lines of the code which is too complicated for me and I am not able to fix it.

shxuy gravatar imageshxuy ( 2019-04-11 00:19:49 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2019-04-10 01:41:12 -0600

Seen: 786 times

Last updated: Apr 10 '19