Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9189183
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T20:11:12+00:00 2026-06-17T20:11:12+00:00

I wanted to code this RGB to grayscale convertor without any inbuilt Open-CV function.

  • 0

I wanted to code this RGB to grayscale convertor without any inbuilt Open-CV function. This is how my code looks like

import cv2 , numpy
def GrayConvertor(img):


    rows , cols , layers = img.shape

    matrix = numpy.zeros((rows , cols))

    for i in range(rows):
        for j in range(cols):
            val = 0.114 * (img[i][j][0]) + 0.587 * (img[i][j][1]) + 0.299 * (img[i][j][2])
            fraction = val - int(val)
            if fraction >= 0.5:
                matrix[i][j] = (int(val) + 1) 

            else: 
                matrix[i][j] = int(val) 
    cv2.imshow("gray" , matrix)
    cv2.waitKey(0)

However it shows a blank image , Any ideas?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T20:11:13+00:00Added an answer on June 17, 2026 at 8:11 pm

    When you create your matrix array with np.zeros it is, by default, assigned dtype=float. So even though you round and convert your values to int, when writing them into matrixthey get stored as floats. If you read the docs for cv.imshow you will find the following:

    • If the image is 32-bit floating-point, the pixel values are
      multiplied by 255. That is, the value range [0,1] is mapped to
      [0,255].

    So everything in your image is getting multiplied by 255, thus messing up your final result.

    You can do two things:

    1. Have a floating point matrix, skip all the rounding, and divide your values by 255.
    2. Explicitly create your matrix with dtype='uint8' and leave everything unchanged.

    There is also the fact that you are doing a very poor use of numpy’s capabilities. The two options I gave you above, you can code with no loops or matrix assignments, as

    rgb2k = np.array([0.114, 0.587, 0.299])
    matrix_int = np.round(np.sum(img * rgb2k, axis=-1)).astype('uint8')
    matrix_float = np.sum(img * rgb2k, axis=-1) / 255
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wanted to use code like this in my Zend Framework project: public function
I wanted to use this code, and it break on flush() without any exception
I wanted to write some code like this: class SomeModel(models.Model): field = models.ForeignKey(SomeOtherModel) def
I don't like this code but I always get confused with threads so wanted
I wanted to parallelize this code with the help of OpenMP with something like
I wanted to know whether it is possible to implement code like this? public
Hi i wanted to remove a link and i got this code $(document).ready(function(){ $(.features-list
I wanted to do something like this code, but the date does not let
I wanted to get ribbot an open source code from this git repository .
I wanted to list all my Recently Used Items. I use this code: public

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.