This post will be helpful in learning OpenCV using Python programming. Here I will show how to implement OpenCV functions and apply them in various aspects using some great examples. Then the output will be visualized along with the comparisons.
We will also discuss the basic of image processing and provide the detail explanation related to the OpenCV functions.
Requirements:
- OpenCV 3.4+
- Python 3.6+
- Numpy
- Image, Webcam or Video input
- Documentation Source: OpenCV Official Documentation
First, you need to setup your Python Environment with OpenCV. You can easily do it by following Life2Coding’s tutorial on YouTube: Linking OpenCV with Python 3
Goals:
The goal is to make you understand how to access image pixel to get RGB color values
Documentation:
imread()
retval=cv.imread(filename[, flags]) |
Loads an image from a file.
- Parameters
-
filename Name of file to be loaded. flags Flag that can take values of cv::ImreadModes
namedWindow()
None=cv.namedWindow(winname[, flags]) |
Creates a window.
- Parameters
-
winname Name of the window in the window caption that may be used as a window identifier. flags Flags of the window. The supported flags are: (cv::WindowFlags)
imshow()
None=cv.imshow(winname, mat) |
Displays an image in the specified window.
- Parameters
-
winname Name of the window. mat Image to be shown.
waitKey()
retval=cv.waitKey([, delay]) |
Waits for a pressed key.
- Parameters
-
delay Delay in milliseconds. 0 is the special value that means “forever”.
destroyAllWindows()
None=cv.destroyAllWindows() |
Destroys all of the HighGUI windows.
Steps:
- Load the Original image using cv2.imread()
- Create a window by using cv2.namedWindow()
- Then we need to set mouse callbacks using cv2.setMouseCallback()
- Create a while loop to detect mouse movements on that window
- Wait for the cv2.EVENT_MOUSEMOVE in the callback functions
- We also need to convert the BGR value to RGB value by reversing the tupple value
- Display all the images using cv2.imshow()
- Wait for keyboard button press using cv2.waitKey()
- Exit window and destroy all windows using cv2.destroyAllWindows()
Example Code:
import cv2 def Life2CodingRGB(event, x, y, flags, param): if event == cv2.EVENT_MOUSEMOVE : # checks mouse moves colorsBGR = image[y, x] colorsRGB=tuple(reversed(colorsBGR)) #Reversing the OpenCV BGR format to RGB format print("RGB Value at ({},{}):{} ".format(x,y,colorsRGB)) # Read an image image = cv2.imread("hanif.jpg") # Create a window and set Mousecallback to a function for that window cv2.namedWindow('Life2CodingRGB') cv2.setMouseCallback('Life2CodingRGB', Life2CodingRGB) # Do until esc pressed while (1): cv2.imshow('Life2CodingRGB', image) if cv2.waitKey(10) & 0xFF == 27: break # if esc is pressed, close all windows. cv2.destroyAllWindows()
Output:
- How to Create a RGB Color Picker for Images using OpenCV Python - 22 April, 2022
- Combine Several Images Vertically with Padding using OpenCV Python - 21 April, 2022
- Combine Several Images Horizontally with Padding using OpenCV Python - 21 April, 2022
Hello,
could you please tell me how can i delete the old value so that it only shows the current coordinate value.
Thanks in Advance.
Regard,s
Waqar Ali