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

  • SEARCH
  • Home
  • 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 8223145
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T14:34:34+00:00 2026-06-07T14:34:34+00:00

I am using OpenCV with Python. I have an image, and what I want

  • 0

I am using OpenCV with Python. I have an image, and what I want to do is set all pixels of BGR value [0, 0, 255] to [0, 255, 255].

I asked a previous question on how to posterize an image, and from the answer I learned about indexing with an Array of indices, for ex:
image[image > 128] = 255

I understand how this works, since image > 128 will return an array of multi-dimensional array of indices that satisfy the condition, and then I apply this array to the image and set those to 255. However, I’m getting confused with how to extend this to doing a value for an array.

I tried doing the following:

      red = np.array([0, 0, 255])
      redIndex = np.where(np.equal(image, red))
      image[redIndex] = np.array([0, 255, 255])

but it doesn’t work, with the error:

ValueError: array is not broadcastable to correct shape

Is there an efficient way to handle this?

  • 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-07T14:34:39+00:00Added an answer on June 7, 2026 at 2:34 pm

    Consider an image like array as below :

    >>> red
    array([[[  0,   0, 255],
            [  0,   0, 255],
            [  0,   0, 255],
            [  0,   0, 255],
            [  0,   0, 255]],
    
           [[  0,   0, 255],
            [  0,   0, 255],
            [  0,   0, 255],
            [  0,   0, 255],
            [  0,   0, 255]]])
    

    Its all elements are [0,0,255]. Its shape is 2x5x3. Just think there are other values also in it. (I can’t create all those).

    Now you find where [0,0,255] are present and change them to [0,255,255]. You can do it as follows :

    >>> red[np.where((red == [0,0,255]).all(axis = 2))] = [0,255,255]
    

    Now check the red.

    >>> red
    array([[[  0, 255, 255],
            [  0, 255, 255],
            [  0, 255, 255],
            [  0, 255, 255],
            [  0, 255, 255]],
    
           [[  0, 255, 255],
            [  0, 255, 255],
            [  0, 255, 255],
            [  0, 255, 255],
            [  0, 255, 255]]])
    

    Hope this is what you want.

    Test Results:

    Check out the code from this link : https://stackoverflow.com/a/11072667/1134940

    I want to change all red pixels to yellow as asked in question.

    So i added this below piece of code at the end :

    im2[np.where((im2 == [0,0,255]).all(axis = 2))] = [0,255,255]
    

    Below is the result I got :

    enter image description here

    What if i want to change green ground to yellow ground :

    im2[np.where((im2 == [0,255,0]).all(axis = 2))] = [0,255,255]
    

    Result :

    enter image description here

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to get the video stream from my webcam using python and OpenCV,
A few days back, I started using new OpenCV-Python interface, cv2 . My question
I have written a program using Python and OpenCV where I perform operations on
I am using openCV python bindings. I am trying to show the output image
I'm trying to create a basic video file using OpenCV (in Python). I have
I'm using the Python OpenCV bindings to find the contours in an Image. I'm
How to do image stitching using opencv (python), simplecv or pil? or any other
I'm trying to read a 16 bit grayscale image using OpenCV 2.4 in Python,
Can anyone describe how can i implement SWT in python using opencv or simplecv
I'm using the Python bindings to OpenCV. If I pass a nonexistent (or bad)

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.