立体矫正
# -*- coding: utf-8 -*-
import cv2
import numpy as np
import ZEDstereoconfig_040_2 # 导入相机标定的参数
import matplotlib.pyplot as plt
from PIL import Image
# 用arrays存储所有图片的object points 和 image points
objpoints = [] # 3d points in real world space
imgpointsR = [] # 2d points in image plane
imgpointsL = []
# image1 = cv2.imread(r'D:\dht\qipange\right\right_0.jpg', 0) # 右视图
# image2 = cv2.imread(r'D:\dht\qipange\left\left_0.jpg', 0) # 左视图
# 获取畸变校正和立体校正的映射变换矩阵、重投影矩阵
# @param:config是一个类,存储着双目标定的参数:config = stereoconfig.stereoCamera()
def getRectifyTransform(height, width, config):
# 读取内参和外参
left_K = config.cam_matrix_left
right_K = config.cam_matrix_right
left_distortion = config.distortion_l
right_distortion = config.distortion_r
R = config.R
T = config.T
# 计算校正变换
R1, R2, P1, P2, Q, roi1, roi2 = cv2.stereoRectify(left_K, left_distortion, right_K, right_distortion,
(width, height), R, T, alpha=0)
map1x, map1y = cv2.initUndistortRectifyMap(left_K, left_distortion, R1, P1, (width, height), cv2.CV_32FC1)
map2x, map2y = cv2.initUndistortRectifyMap(right_K, right_distortion, R2, P2, (width, height), cv2.CV_32FC1)
return map1x, map1y, map2x, map2y, Q
# 畸变校正和立体校正
def rectifyImage(image1, image2, map1x, map1y, map2x, map2y):
rectifyed_img1 = cv2.remap(image1, map1x, map1y, cv2.INTER_AREA)
#cv2.imshow("111",rectifyed_img1)
im_L = Image.fromarray(rectifyed_img1) # numpy 转 image类
rectifyed_img2 = cv2.remap(image2, map2x, map2y, cv2.INTER_AREA)
im_R = Image.fromarray(rectifyed_img2) # numpy 转 image 类
cv2.imwrite('Litijiaozheng222left.jpg', rectifyed_img1)
# cv2.imshow("1",rectifyed_img1)
cv2.imwrite('Litijiaozheng222right.jpg', rectifyed_img2)
# cv2.imshow("2", rectifyed_img2)
# cv2.waitKey()
return rectifyed_img1, rectifyed_img2
# 立体校正检验----画线
def draw_line(image2, image1):
# 建立输出图像
height = max(image1.shape[0], image2.shape[0])
width = image1.shape[1] + image2.shape[1]
output = np.zeros((height, width, 3), dtype=np.uint8)
output[0:image1.shape[0], 0:image1.shape[1]] = image1
output[0:image2.shape[0], image1.shape[1]:] = image2
# 绘制等间距平行线
line_interval = 50 # 直线间隔:50
for k in range(height // line_interval):
cv2.line(output, (0, line_interval * (k + 1)), (2 * width, line_interval * (k + 1)), (0, 255, 0), thickness=2,
lineType=cv2.LINE_AA)
return output
#ght = disparity_right.astype(np.float32) / 16.
return trueDisp_left, trueDisp_right
if __name__ == '__main__':
i = 222
string = 'image_0'
# 读取数据集的图片
t = str(i)
# iml = cv2.imread(r'D:\dht\qipange\left\left_0' + t + '.jpg', 1) # 左图
# imr = cv2.imread(r'D:\dht\qipange\right\right_0' + t + '.jpg', 1) # 右图
iml = cv2.imread(r'D:/dht/fenge/right/image_0.jpg', 1) # 右视图
imr = cv2.imread(r'D:/dht/fenge/left/image_0.jpg', 1) # 左视图
height, width = iml.shape[0:2]
print("width = %d \n" % width)
print("height = %d \n" % height)
# 读取相机内参和外参
config = ZEDstereoconfig_040_2.stereoCamera()
# 立体校正
map1x, map1y, map2x, map2y, Q = getRectifyTransform(height, width, config) # 获取用于畸变校正和立体校正的映射矩阵以及用于计算像素空间坐标的重投影矩阵
iml_rectified, imr_rectified = rectifyImage(iml, imr, map1x, map1y, map2x, map2y)
# cv2.imshow("l.jpg", iml_rectified)
# cv2.imshow("r.jpg", imr_rectified)
print("Print Q!")
print(Q)
#输出校正后的图片
# 绘制等间距平行线,检查立体校正的效果
line = draw_line(iml_rectified, imr_rectified)
cv2.imwrite("222.jpg",line)
# cv2.imshow("ZEDlitijiaozheng30.jpg",line)
#cv2.imwrite('./%s检验%d.png' % (string, i), line)
# points_3d = points_3d
# 显示图片
cv2.waitKey(0)
cv2.destroyAllWindows()
# plt.savefig(r'D:\dht\ZEDlitijiaozheng' + t + '.jpg')
# plt.show()
膨胀腐蚀
import cv2
import numpy as np
## 测试图片,为反斜杠
img = 'D:\dht\paper\photo\Litijiaozheng222right_fuben.jpg'
## a.图像的二值化 ,这里没有做阈值处理
src = cv2.imread(img, cv2.IMREAD_UNCHANGED)
## b.设置卷积核5*5
kernel = np.ones((5, 5), np.uint8)
## c.图像的腐蚀,默认迭代次数
erosion = cv2.erode(src, kernel)
## 图像的膨胀
dst = cv2.dilate(erosion, kernel)
## 效果展示
#cv2.imshow('origin', src)
# ## 腐蚀后
# cv2.imshow('after erosion', erosion)
# cv2.imwrite('FUSHI.jpg',erosion)
## 膨胀后
# cv2.imshow('after dilate', dst)
cv2.imwrite('image_1pengzhang.jpg',dst)
#cv2.waitKey(0)
高斯滤波
import cv2
img = cv2.imread('D:\pycharmProject\pythonProject1\ImageProcessing\image_1pengzhang.jpg')
# (3, 3)表示高斯滤波器的长和宽都为3,1.3表示滤波器的标准差
out = cv2.GaussianBlur(img, (3, 3), 1.3)
cv2.imwrite('u-image_1Gaussian.jpg', out)
#cv2.imshow('result', out)
#cv2.waitKey(0)
cv2.destroyAllWindows()
hough直线检测
import cv2 as cv
import numpy as np
from matplotlib import pyplot as plt
src = cv.imread("D:/pycharmProject/pythonProject1/ImageProcessing/u-image_1Gaussian.jpg")
#src = cv.imread('D:/dht/fenge/left/image_0.jpg')
gray_img = cv.cvtColor(src, cv.COLOR_BGR2GRAY)
dst = cv.equalizeHist(gray_img)
# 高斯滤波降噪
gaussian = cv.GaussianBlur(dst, (9, 9), 0)
# cv.imshow("gaussian", gaussian)
# 边缘检测
edges = cv.Canny(gaussian, 70, 150)
#cv.imshow("edges", edges)
#cv.imwrite('houghhhh.jpg',edges)
lines = cv.HoughLinesP(edges, 1.0, np.pi/180, 100,
minLineLength=100, maxLineGap=6)
#print(type(lines))
Ax = []
Ay = []
Bx = []
By = []
for line in lines:
x1, y1, x2, y2 = line[0]
cv.line(src, (x1, y1), (x2, y2), (0, 255, 0), 2)
Ax.append(x1)
Ay.append(y1)
Bx.append(x2)
By.append(y2)
#print(x1,y1,x2,y2)
plt.plot([x1,x2],[y1,y2],c='blue',ls='-') #y前加负号,上下翻转一下,因为python的坐标轴是左上角的
#cv.imshow("src", src)
print("Ax=",Ax)
print("Ay=", Ay)
print("Bx=", Bx)
print("By=", By)
cv.imwrite('u-image_1hough.jpg',src)
# plt.gca().invert_yaxis()
# plt.show()
#
# cv.waitKey()
# cv.destroyAllWindows()
matlab已知坐标点画线
Ax= [1319, 1403, 390, 1416, 1407, 1412, 110, 1268, 733, 1503, 1266, 1520, 646, 352, 1630, 1637, 1264, 470, 1317, 1341, 1629, 1337, 1477]
Ay= [656, 695, 719, 655, 646, 706, 820, 584, 596, 640, 623, 642, 620, 724, 748, 693, 577, 682, 655, 564, 695, 568, 629]
Bx= [1496, 1718, 562, 1730, 1695, 1720, 341, 1494, 865, 1720, 1669, 1717, 767, 468, 1809, 1829, 1508, 580, 1565, 1515, 1762, 1477, 1619]
By= [750, 862, 657, 808, 786, 870, 736, 694, 550, 736, 837, 729, 578, 684, 835, 778, 696, 644, 787, 641, 754, 630, 692]
X=[Ax ; Bx];%2*n维矩阵,第一行放起点x值,第二行放终点x值
Y=[-Ay ; -By];%y轴加了个负号,因为python的坐标是左上角开始的,要上下翻转
line(X,Y);