Reputation: 25
So, I have been asked to convert a BGR image to GRAYSCALE using weighted mean of each pixel.
img = cv2.imread('..\\Images\\horse.jpg',-1)
height = img.shape[0]
width = img.shape[1]
gray = img.copy()
for x in range(img.shape[1]):
for y in range(img.shape[0]):
gray[y][x]= (0.11 * img[y][x][0] + 0.6 * img[y][x][1] + 0.3 * img[y][x][2])
print(gray)
print(gray.shape)
cv2.imshow('gray',gray)
cv2.waitkey(0)
Shape of the resultant image:
(404, 640, 3)
it should be a single channeled image right? In result the image shown is GRAYSCALE but still it is a 3 channeled image, can anyone help me with that?
Upvotes: 1
Views: 401
Reputation: 12992
The reason is so simple, this happens because you copied the whole img
at the beginning which has three channels. You need to copy just one channel like so:
gray = img[:, :, 0].copy()
Upvotes: 1