OpenCV的鼠标操作
实现获取像素点的功能主要基于OpenCV的内置函数cv2.setMouseCallback(),即鼠标事件回调
setMouseCallback(winname, onMouse,userdata=0)
winname: 接收鼠标事件的窗口名称
onMouse: 处理鼠标事件的回调函数指针
userdata: 传给回调函数的用户数据
代码实现:鼠标点击图片时,读取当前鼠标对应位置的像素值(顺序为BGR),在鼠标所在位置的左上角显示当前坐标值和像素值;鼠标移动时,旧的文本框消失
import cv2
import numpy as npimg = cv2.imread('./158.jpg')#读取图片
font_face,font_scale,thickness=cv2.FONT_HERSHEY_SIMPLEX,0.5,1
#鼠标交互
def mouseHandler(event,x,y,flags,param):points = (x,y)global imgCopy#鼠标左键双击事件if event == cv2.EVENT_LBUTTONDBLCLK:#输出坐标print(x,y)#拷贝一张与原图像格式相同的新图像imgCopy = img.copy()#拼接文字text = '['+str(x)+','+str(y)+']'+str(img[x,y])#读取文字(宽,高),下基线(t_w,t_h),baseLine = cv2.getTextSize(text,font_face,font_scale,thickness)#在鼠标当前位置的左上角显示文字cv2.putText(imgCopy,text,(x-t_w,y),font_face,font_scale,(125,125,125))cv2.imshow('win',imgCopy)#鼠标移动事件elif event == cv2.EVENT_MOUSEMOVE:#显示原图片能使文本框消失cv2.imshow('win',img)cv2.namedWindow('win')
#窗口与回调函数绑定
cv2.setMouseCallback('win',mouseHandler)
cv2.imshow('win',img)
cv2.waitKey()
IPM代码:
import cv2
import numpy as np def multi_transform(img, pts1): ROI_HEIGHT = 30000 ROI_WIDTH = 3750 # 设定逆透视图的宽度 IPM_WIDTH = 500 N = 5 # 保证逆透视图的宽度大概为N个车头宽 sacale=(IPM_WIDTH/N)/ROI_WIDTH IPM_HEIGHT=ROI_HEIGHT*sacale pts2 = np.float32([[IPM_WIDTH/2-IPM_WIDTH/(2*N), 0], [IPM_WIDTH/2+IPM_WIDTH/(2*N), 0], [IPM_WIDTH/2-IPM_WIDTH/(2*N), IPM_HEIGHT], [IPM_WIDTH/2+IPM_WIDTH/(2*N), IPM_HEIGHT]]) print(IPM_HEIGHT,IPM_WIDTH) matrix = cv2.getPerspectiveTransform(pts1, pts2) output = cv2.warpPerspective(img, matrix, (int(IPM_WIDTH),int(IPM_HEIGHT+50))) for i in range(0, 4): cv2.circle(img, (pts1[i][0], pts1[i][1]), 6, (0, 0, 255), cv2.FILLED) for i in range(0,4): cv2.circle(output, (pts2[i][0], pts2[i][1]),6, (0, 0, 255), cv2.FILLED) # p1 = (0, 250) # p2 = (img.shape[1], img.shape[0]-100) # point_color = (255, 0, 0) # cv2.rectangle(img, p1, p2, point_color, 2) cv2.imshow("src image", img) cv2.imshow("output image", output) cv2.imwrite("output.jpg", output) # 新增的代码,将处理后的图像保存为 "output.jpg" cv2.waitKey(0) if __name__ == '__main__': # 图像1 img = cv2.imread("./158.jpg") pts1 = np.float32([[543, 462], # p1 [749, 466], # p2 [277, 536], # p3 [937, 546]]) # p4 # 图像2 # img = cv2.imread("./789.jpeg") # pts1 = np.float32([[243, 189], # p1 # [383, 186], # p2 # [77, 253], # p3 # [533, 253]]) # p4 multi_transform(img, pts1)
原图:
效果图:
二值化处理:
import cv2 # 读取原始图像
img = cv2.imread('./498_1.jpg') # 将图像转换为灰度图像
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # 对灰度图像进行二值化处理
thresh, binary = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY) # 保存二值化后的图像
cv2.imwrite('./498_3.jpg', binary)