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 9173309
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T16:33:34+00:00 2026-06-17T16:33:34+00:00

My goal is to convert image pixels with value = 255 to 0. i.e.

  • 0

My goal is to convert image pixels with value = 255 to 0. i.e. remove all the pure white pixels. Here’s the code in python using opencv and opencl:

import os
import glob
import cv2 as cv
import numpy as np
import pyopencl as cl

def filter_image( ):

    platforms = cl.get_platforms()
    devices = platforms[0].get_devices( cl.device_type.ALL )
    context = cl.Context( [devices[0]] )
    cQ = cl.CommandQueue( context )
    kernel = """
        __kernel void filter( global uchar* a, global uchar* b ){
                int y = get_global_id(0);
                int x = get_global_id(1);

                int sizex = get_global_size(1);

                if( a[ y*sizex + x ] != 255 )
                        b[ y*sizex + x ] = a[ y*sizex + x ];
            }"""

    program = cl.Program( context, kernel ).build()

    for i in glob.glob("*.png"):

        image = cv.imread( i, 0 )        
        b = np.zeros_like( image, dtype = np.uint8 )
        rdBuf = cl.Buffer( 
                context,
                cl.mem_flags.READ_ONLY | cl.mem_flags.COPY_HOST_PTR,
                hostbuf = image
                          )

        wrtBuf = cl.Buffer( 
                context,
                cl.mem_flags.WRITE_ONLY,
                b.nbytes
                          )

        program.filter( cQ, image.shape, None, rdBuf, wrtBuf ).wait()
        cl.enqueue_copy( cQ, b, wrtBuf ).wait()
        cv.imshow( 'a', b )
        cv.waitKey( 0 )

def Filter( ):
    os.chdir('D:\image')
    filter_image( )
    cv.destroyAllWindows()

The problem I am facing is that, once I use the loop as in the above program, the logic works for only the first image. i.e. the white pixels are removed only for the first image but no effect is seen in the subsequent images, i.e the output image is same as the input images without any kind of effect on pixels with value = 255. This should be simple. I am unable to find any solution.

Kindly help me in resolving this problem.

Thank you.

  • 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-17T16:33:35+00:00Added an answer on June 17, 2026 at 4:33 pm

    In your kernel, you’re not setting the pixel in image b to anything if the pixel in image a is white. You should change it to something like the following:

    b[y * sizex + x] = (a[y * sizex + x] == 255) ? 0 : a[y * sizex + x];
    

    Which will set the pixel in image b to zero if the pixel in image a is white, and copy the pixel unchanged otherwise. Also consider doing this kind of manipulation in-place, such that only one buffer is needed.

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

Sidebar

Related Questions

My goal is to convert list into array using split method then remove element(depends)
I made a Python function to convert dictionaries to formatted strings. My goal was
Goal: Trying to convert some of the lines of an algorithm written in python
My end goal here is to resize the input image to 100px width, 125px
My goal is to convert a PDF into an image (specifically in TIFF). There
My goal is to convert a string into a dictionary. Here's how it looks
My goal is to write the more concise/effective function to convert a value to
My goal is to convert a into b: a = [[a,b], [d, c], [a,
Goal: Gain datatype date with year and month Problem: Need to help to convert
Goal : I wants when I drag image it become fade so we can

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.