Python-openCV查看图像像素值,获取鼠标位置


#showPixelValue.py

import cv2
import sys

def getMousePos(imgName):
  def onmouse(event, x, y, flags, param):   
    cv2.imshow("img",img)
    #if event==cv2.EVENT_MOUSEMOVE:     
      #print(img[y,x], " pos: ", x, " x ", y) 
      
    #双击左键,显示鼠标位置
    if event == cv2.EVENT_LBUTTONDBLCLK:
      strtext = "(%s,%s)"%(x,y)
      cv2.circle(img,(x,y),2,(0,0,255),-1)
      cv2.putText(img, strtext, (x+2, y+15) ,cv2.FONT_HERSHEY_SIMPLEX,1,(0,0,200),2)
 	
  cv2.namedWindow("img", cv2.WINDOW_NORMAL)   
  img= cv2.imread(imgName)  
  print(img.shape)  
    
  cv2.setMouseCallback("img", onmouse)   
  
  if cv2.waitKey() & 0xFF == 27: #按下‘q'键,退出
    cv2.destroyAllWindows()         
  
	return 
	
	
def showPixelValue(imgName):

  img= cv2.imread(imgName)          
  def onmouse(event, x, y, flags, param):   
    if event==cv2.EVENT_MOUSEMOVE:     
      print(img[y,x]) 
      
  cv2.namedWindow("img")         
  cv2.setMouseCallback("img", onmouse)   
  cv2.imshow("img",img)        
  if cv2.waitKey() == ord('q'): #按下‘q'键,退出
    cv2.destroyAllWindows()         
  
if __name__ == '__main__':          
  arg1 = sys.argv[1]
  getMousePos(arg1)

Run:
python showPixelValue.py imgPath

你可能感兴趣的:(cv,python,opencv,python,计算机视觉)