Opencv-python uses the mouse to click on the picture to display the coordinates and pixel values of the point && IPM reverse perspective transformation lane line && binarization processing

OpenCV mouse operation

The function of obtaining pixels is mainly based on OpenCV's built-in function cv2.setMouseCallback(), which is the mouse event callback

setMouseCallback(winname, onMouse, userdata=0)

winname: the name of the window that receives the mouse event
onMouse: the pointer to the callback function that handles the mouse event
userdata: the user data passed to the callback function

 Code implementation: when the mouse clicks on the picture, read the pixel value of the current mouse position (the order is BGR), and display the current coordinate value and pixel value in the upper left corner of the mouse position; when the mouse moves, the old text box disappears

import cv2
import numpy as np

img = 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 code:

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)

 Original image:

Renderings:

Binarization processing:

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)

 

Guess you like

Origin blog.csdn.net/weixin_64043217/article/details/132358089