OpenCV (Open-Source Computer Vision) is an open-source and free-to-use library of computer vision and machine learning techniques. It is used in various processes, such as video stabilization, image processing, and object detection. It is written in C++ but offers interfaces for Python, Java, and C#, as well as other computer languages. With Windows, Linux, and macOS support, OpenCV is widely used in academic and industrial projects.
One of the tasks includes checking whether the image has a glare in it so that it can be later used to demonstrate during presentations.
Here is an example of how you might use this function to detect glare in your image:
In this solution, we use the morphology function of the Opencv library
import cv2 import numpy as np # read image img = cv2.imread('apple.png') # convert to gray gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # threshold grayscale image to extract glare mask = cv2.threshold(gray, 220, 255, cv2.THRESH_BINARY) # Optionally add some morphology close and open, if desired #kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (7,7)) #mask = cv2.morphologyEx(mask, cv2.MORPH_CLOSE, kernel, iterations=1) #kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3,3)) #mask = cv2.morphologyEx(mask, cv2.MORPH_OPEN, kernel, iterations=1) # use mask with input to do inpainting result = cv2.inpaint(img, mask, 21, cv2.INPAINT_TELEA) # write result to disk cv2.imwrite("apple_mask.png", mask) cv2.imwrite("apple_inpaint.png", result) # display it cv2.imshow("IMAGE", img) cv2.imshow("GRAY", gray) cv2.imshow("MASK", mask) cv2.imshow("RESULT", result) cv2.waitKey(0)
- Copy the code using the "Copy" button above, and paste it in a Python file in your IDE.
- Modify the name, location of the image to be resized in the code.
- Run the file to resize the image.
I hope you found this useful. I have added the link to dependent libraries, version information in the following sections.
Shell 3491 Version:72 License: Permissive (MIT)
If you do not have OpenCV that is required to run this code, you can install it by clicking on the above link and copying the pip Install command from the OpenCV page in kandi.
You can search for any dependent library on kandi like OpenCV.
I tested this solution in the following versions. Be mindful of changes when working with other versions.
- The solution is created in Python3.11.
- The solution is tested on OpenCV-Python 4.7 version.