Color image to black and white image

How to make a black and white image from color image . Maybe using OpenCV? Because I don’t need combination of black and white pixels , but I need a gray gradient image ( I hope you understand what I mean )

There’s a number of ways to do this. The easiest way is to use the filter() function. The filter function can be used on the entire sketch or you can use it on a PImage. You could also do it manually by looping through the pixels of an image like:

img.loadPixels();
for (int i = 0; i < img.pixels.length; i++) {
  img.pixels[i] = color(brightness(img.pixels[i]));
}
img.updatePixels();

Which would also give you more control if you want to do something other than the basic filters.

It sounds like you’re looking for a grayscale image but if you want one thats just black and white pixels. You can use dithering and here’s a video about dithering in processing.

2 Likes

As figraham says, filter(GRAY) is probably what you want.

If you are looking to design your own custom black and white (or gray) effect, then in addition to dithering directly in Processing as in the video you can also check out PShader. The PShader includes an example of a black and white shader:

Code listing 8.1: Sketch using the black&white shader.
https://processing.org/tutorials/pshader/

and there are lots of other photo filter effects that you can do using shaders. These tend to be advanced projects, however, as you are working in a different language (GLSL) to write them.