1

I'm supposed to write a method that converts an RGB image to Grayscale by using the "average method" where I take the average of the 3 colors (NOT the weighted method or luminosity method). I then must display the original RGB image and grayscale image next to each other (concatenated). The language I'm writing in is Python. This is what my code looks like currently.

import numpy as np
import cv2

def average_method(img):
    grayValue = (img[:,:,2] + img[:,:,1] + img[:,:,0])/3
    gray_img = grayValue.astype(np.uint8)
    return gray_img

def main():
    img1 = cv2.imread('html/images/sunflowers.jpg')
    img1 = cv2.resize(img1, (0, 0), None, .25, .25)
    img2 = average_method(img1)
    numpy_concat = np.concatenate((img1, img2), 1)
    cv2.imshow('Numpy Concat', numpy_concat)
    cv2.waitKey(0)
    cv2.destroyAllWindows

if __name__ =="__main__":
    main()

When I try to run this, it shows me this error:

    Traceback (most recent call last):
  File "test.py", line 23, in <module>
    main()
  File "test.py", line 16, in main
    numpy_concat = np.concatenate((img1, img2), 1)
  File "<__array_function__ internals>", line 5, in concatenate
ValueError: all the input arrays must have same number of dimensions, but the array at index 0 has 3 dimension(s) and the array at index 1 has 2 dimension(s)

Can someone please help me figure this out so that I can successfully have RGB and grayscale side by side by using the "average method"? Not sure if I'm setting up the average method correctly and it causes this issue, or if it's from how I try to concatenate the pictures. Thanks.

5
  • Pro tip: your question is about index error, and not about gray calculation. So you should put that in title and as main problem, or people may overlook your questions Commented Oct 26, 2020 at 9:23
  • Are you sure it's a colour image? What is img1.shape after each step? What does the second argument to cv2.imread mean? Commented Oct 26, 2020 at 10:08
  • @GiacomoCatenazzi Thanks for pointing that out! I went ahead and edited it. Commented Oct 26, 2020 at 17:37
  • @TurePålsson Yes, img1 is supposed to be the color image (the original), then img2 would be the grayscale image of it. Thanks for mentioning the second argument to cv2.imread, I took that out because that itself converts it to grayscale but I just want to display the original rgb one. The img1.shape after the cv2.imread is (183, 275, 3). The img1.shape after the cv2.resize is (46, 69, 3). Commented Oct 26, 2020 at 17:51
  • @TurePålsson The print statement doesn't not print after the np.concatenate line, meaning that line is not going through since "ValueError: all the input arrays must have same number of dimensions, but the array at index 0 has 3 dimension(s) and the array at index 1 has 2 dimension(s)". I edited my question to be more clear now. Commented Oct 26, 2020 at 17:51

1 Answer 1

1

The error message pretty much tells you what is going on. Your img2 is now a greyscale (single-channel) image, which img1 is still, obviously, colour (three-channel).

I am no numpy wizard, so there might be a better solution, but I think you can expand img2 to three channels using

img2 = np.stack(3 * [img2], axis=2)

Edit: possibly more efficient (and harder to understand):

img2 = np.repeat(A[..., np.newaxis], 3, axis=2)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.