Python3.6 + opencv3.4 allows you to view image pixels through mouse interaction,
When using opencv for image processing, you often need to view the pixel values of the image's areas or locations of interest. I use python3.6 + opencv3.4 to compile a small tool for your use.
Process
1. Establish a standard mouse interaction function. When the mouse moves over the image, the pixel value of the mouse position is instantly displayed (opencv pixel is in BGR format ).
2. Create an image window and bind the mouse callback function.
3. Press 'q' to exit.
4. Only 15 lines of code are required.
Code
#-*-Coding: UTF-8-*-import cv2img = cv2.imread('006.png ') # define Image Position img = cv2.cvtColor (img, cv2.COLOR _ BGR2GRAY) # convert to grayscale image def onmouse (event, x, y, flags, param): # standard mouse interaction function if event = cv2.EVENT _ MOUSEMOVE: # print (img [y, x]) when the mouse moves # display the value of the mouse's pixel. Note the difference between the pixel representation method and the coordinate position def main (): cv2.namedWindow ("img") # construct the window cv2.setMouseCallback ("img", onmouse) # callback binding window while True: # infinite loop cv2.imshow ("img", img) # display image if cv2.waitKey () = ord ('q '): break # press the 'q' key to exit cv2.destroyAllWindows () # Close the window if _ name _ = '_ main _': # Run main ()
Color chart Running Effect
Grayscale image running effect
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.