0

I sum each pixel's Red, Green and Blue values and divide the sum by 3:

gray_image = (image[:,:,0] + image[:,:,1] + image[:,:,2]) / 3

This is what I got:

My code is:

import matplotlib.image as pltim
import matplotlib.pyplot as plt
import numpy as np

def rgb2gray(image):
    imageHeight = len(image)
    imageWidth = len(image[0])
    grayImage = np.empty([imageHeight, imageWidth], dtype=np.uint8)

    for i in range(imageHeight):
        for j in range(imageWidth):
            grayImage[i][j] = int((image[i][j][0] + image[i][j][1] + image[i][j][2]) / 3)
    return grayImage


class RetargetedImage:
    imageDirectory = ""
    image = None
    grayImage = None

    def __init__(self, imageDirectory):
        self.imageDirectory = imageDirectory
        self.image = pltim.imread(self.imageDirectory)
        self.grayImage = rgb2gray(self.image)

    def showOriginalImage(self):
        plt.imshow(self.image)
        plt.show()

    def showGrayImage(self):
        plt.imshow(self.grayImage)
        plt.show()


example1 = RetargetedImage("treeMedium.jpg")
example1.showGrayImage()

And this is the original image:

Where am I doing wrong?

2
  • plt.imshow(..., cmap='gray') Commented May 10, 2020 at 21:02
  • Try to avoid for loops (they are very slow and error-prone), use np.dot() instead.... stackoverflow.com/a/58531115/2836621 Commented May 10, 2020 at 21:05

2 Answers 2

2

Here is the documentation of the imshow method

The input may either be actual RGB(A) data, or 2D scalar data, which will be rendered as a pseudocolor image. Note: For actually displaying a grayscale image set up the color mapping using the parameters cmap='gray', vmin=0, vmax=255

To visualize the image in grayscale:

def showGrayImage(self):
    plt.imshow(self.grayImage, cmap='gray', vmin=0, vmax=255)
    plt.show()

Concerning line:

grayImage[i][j] = int((image[i][j][0] + image[i][j][1] + image[i][j][2]) / 3)

You are missing the three weighting coefficients for the R, G and B channels, as explained here on Wikipedia.

Y ← 0.299⋅R+0.587⋅G+0.114⋅B
Sign up to request clarification or add additional context in comments.

Comments

0

to convert from rgb to grayscale use gray = 0.2126 * red + 0.7152 * green + 0.0722 * blue

can you post the output

for i in range(imageHeight):
        for j in range(imageWidth):
            grayImage[i][j] = int(image[i][j][0]*0.2126 + image[i][j][1]*0.7152 + image[i][j][2] * 0.0722)
    return grayImage

1 Comment

Where did we find 0.2126, 0.7152 and 0.0722? I applied and now I got this image: i.ibb.co/6Y2zYBj/treebuggy2.png

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.