Python3.6 + opencv3.4 allows you to view image pixels through mouse interaction,

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.