Converting a black image to white means changing the color of the image from black to white. This can be done using increasing and decreasing the pixels of the color. The specific steps will vary depending on the software being used, but the basic idea is to increase the brightness until the black pixels become white.
OpenCV is widely used in the field of computer vision for tasks such as object recognition, face detection, and image and video analysis. It has a large community of developers and users and is continuously updated and improved. cv2.threshold is a function in OpenCV (Open Source Computer Vision Library) that applies a threshold to an image. Thresholding is a process of converting an image into a binary image by dividing the image into two regions, foreground, and background. The foreground pixels have a certain intensity level and all other pixels are set to the background. The threshold is the value that separates the foreground and background pixels.
- Reads an image using cv2.imread and stores it in the img variable.
- Converts the image to grayscale using cv2.cvtColor and stores it in the gray variable.
- Applies a binary threshold to the grayscale image using cv2.threshold, which means that all pixels with a value greater than or equal to 240 are set to 255 (white) and all other pixels are set to 0 (black). The thresholded image is stored in the thresh variable.
- Defines two arrays white_px and black_px to represent the white and black pixels, respectively.
- Loops through each row and column of the thresh image and if the pixel value is equal to white, it sets the corresponding pixel in the img_array to black.
- Creates a structuring element using cv2.getStructuringElement with an elliptical shape and size 5x5.
- Applies morphological erosion to the img_array using the created structuring element and 1 iteration. The result is stored in the erosion variable.
- Displays the erosion image in a window using cv2.imshow and waits for a key press using cv2.waitKey. The window is closed using cv2.destroyAllWindows..
Here is an example of how you can convert white pixels to Black using openCV.
Preview of the output that you will get on running this code from your IDE
In this solution we use the Imread function of the OpenCV.
img = cv2.imread("Images/test.pnt") gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) ret, thresh = cv2.threshold(gray, 240, 255, cv2.THRESH_BINARY) img[thresh == 255] = 0 kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5, 5)) erosion = cv2.erode(img, kernel, iterations = 1) cv2.namedWindow('image', cv2.WINDOW_NORMAL) cv2.imshow("image", erosion) cv2.waitKey(0) cv2.destroyAllWindows() img = cv2.imread("Images/test.png") gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) ret, thresh = cv2.threshold(gray, 240, 255, cv2.THRESH_BINARY) white_px = np.asarray([255, 255, 255]) black_px = np.asarray([0, 0, 0]) (row, col) = thresh.shape img_array = np.array(img) for r in range(row): for c in range(col): px = thresh[r][c] if all(px == white_px): img_array[r][c] = black_px kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5, 5)) erosion = cv2.erode(img_array, kernel, iterations = 1) cv2.namedWindow('image', cv2.WINDOW_NORMAL) cv2.imshow("image", erosion) cv2.waitKey(0) cv2.destroyAllWindows()
- Copy the code using the "Copy" button above, and paste it in a Python file in your IDE.
- Import open Cv library and Numpy library.
- Modify the name, location of the image in the code.
- Run the file to get Output
I hope you found this useful. I have added the link to dependent libraries, version information in the following sections.
i found this code snippet by searching for "Convert White Pixels to Black in OpenCV python" in kandi. You can try any such use case!
I tested this solution in the following versions. Be mindful of changes when working with other versions
- The solution is created and executed in python version 3.7.15 .
- The solution is tested on OpenCV 4.6.0
- The solution is tested on numpy 1.21.6
Using this solution, we are going to convert white Pixels to Black using the OpenCV library in Python with simple steps. This process also facilities an easy-to-use, hassle-free method to create a hands-on working version of code which would help us change the pixels in Python