M, mask = cv2.findHomography(src_pts, dst_pts, cv2.RANSAC, 5.0) print("========变换矩阵为========") print(M)修改上述代码,使得M不以科学表示
时间: 2023-09-06 14:07:11 浏览: 174
可以使用numpy库中的set_printoptions函数来设置打印格式,例如:
```
import numpy as np
np.set_printoptions(suppress=True, precision=4) # 设置不使用科学计数法,小数点后保留4位
M, mask = cv2.findHomography(src_pts, dst_pts, cv2.RANSAC, 5.0)
print("========变换矩阵为========")
print(M)
```
这样就可以将M以普通小数形式打印出来。
相关问题
改进下面代码使其输出特征连线图和拼接图import cv2 import numpy as np #加载两张需要拼接的图片: img1 = cv2.imread('men3.jpg') img2 = cv2.imread('men4.jpg') #将两张图片转换为灰度图像: gray1 = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY) gray2 = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY) #使用Shi-Tomasi角点检测器找到两张图片中的特征点: # 设定Shi-Tomasi角点检测器的参数 feature_params = dict(maxCorners=100, qualityLevel=0.3, minDistance=7, blockSize=7) # 检测特征点 p1 = cv2.goodFeaturesToTrack(gray1, **feature_params) p2 = cv2.goodFeaturesToTrack(gray2, **feature_params) #使用Lucas-Kanade光流法计算特征点的移动向量: # 设定Lucas-Kanade光流法的参数 lk_params = dict(winSize=(15, 15), maxLevel=2, criteria=(cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 0.03)) # 计算特征点的移动向量 p1, st, err = cv2.calcOpticalFlowPyrLK(gray1, gray2, p1, None, **lk_params) p2, st, err = cv2.calcOpticalFlowPyrLK(gray2, gray1, p2, None, **lk_params) #计算两张图片的变换矩阵: # 使用RANSAC算法计算变换矩阵 M, mask = cv2.findHomography(p1, p2, cv2.RANSAC, 5.0) #将两张图片拼接成一张: # 计算拼接后的图像大小 h, w = img1.shape[:2] pts = np.array([[0, 0], [0, h - 1], [w - 1, h - 1], [w - 1, 0]], dtype=np.float32).reshape(-1, 1, 2) dst = cv2.perspectiveTransform(pts, M) xmin, ymin = np.int32(dst.min(axis=0).ravel() - 0.5) xmax, ymax = np.int32(dst.max(axis=0).ravel() + 0.5) tx, ty = -xmin, -ymin H, W = xmax - xmin, ymax - ymin # 计算拼接后的图像 timg = np.zeros((H, W, 3), dtype=np.uint8) timg[ty:ty + h, tx:tx + w] = img1 new_p2 = cv2.perspectiveTransform(p2, M) timg = cv2.polylines(timg, [np.int32(new_p2 + (tx, ty))], True, (0, 255, 255), 1, cv2.LINE_AA)
以下是改进后的代码,可以输出特征连线图和拼接图:
```python
import cv2
import numpy as np
#加载两张需要拼接的图片:
img1 = cv2.imread('men3.jpg')
img2 = cv2.imread('men4.jpg')
#将两张图片转换为灰度图像:
gray1 = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY)
gray2 = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY)
#使用Shi-Tomasi角点检测器找到两张图片中的特征点:
# 设定Shi-Tomasi角点检测器的参数
feature_params = dict(maxCorners=100, qualityLevel=0.3, minDistance=7, blockSize=7)
# 检测特征点
p1 = cv2.goodFeaturesToTrack(gray1, **feature_params)
p2 = cv2.goodFeaturesToTrack(gray2, **feature_params)
#使用Lucas-Kanade光流法计算特征点的移动向量:
# 设定Lucas-Kanade光流法的参数
lk_params = dict(winSize=(15, 15), maxLevel=2, criteria=(cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 0.03))
# 计算特征点的移动向量
p1, st, err = cv2.calcOpticalFlowPyrLK(gray1, gray2, p1, None, **lk_params)
p2, st, err = cv2.calcOpticalFlowPyrLK(gray2, gray1, p2, None, **lk_params)
#计算两张图片的变换矩阵:
# 使用RANSAC算法计算变换矩阵
M, mask = cv2.findHomography(p1, p2, cv2.RANSAC, 5.0)
#将两张图片拼接成一张:
# 计算拼接后的图像大小
h, w = img1.shape[:2]
pts = np.array([[0, 0], [0, h - 1], [w - 1, h - 1], [w - 1, 0]], dtype=np.float32).reshape(-1, 1, 2)
dst = cv2.perspectiveTransform(pts, M)
xmin, ymin = np.int32(dst.min(axis=0).ravel() - 0.5)
xmax, ymax = np.int32(dst.max(axis=0).ravel() + 0.5)
tx, ty = -xmin, -ymin
H, W = xmax - xmin, ymax - ymin
# 计算拼接后的图像
timg = np.zeros((H, W, 3), dtype=np.uint8)
timg[ty:ty + h, tx:tx + w] = img1
# 计算特征连线图
new_p2 = cv2.perspectiveTransform(p2, M)
timg_line = cv2.polylines(timg.copy(), [np.int32(new_p2 + (tx, ty))], True, (0, 255, 255), 1, cv2.LINE_AA)
# 输出特征连线图
cv2.imshow('Feature Lines', timg_line)
cv2.waitKey(0)
# 拼接图加上特征连线
timg = cv2.polylines(timg, [np.int32(new_p2 + (tx, ty))], True, (0, 255, 255), 1, cv2.LINE_AA)
# 输出拼接图
cv2.imshow('Stitched Image', timg)
cv2.waitKey(0)
```
import cv2 import numpy as np #读入需要配准的两张图像 img1 = cv2.imread('men4.jpg') img2 = cv2.imread('men3.jpg') #将图像转换为灰度图像 gray1 = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY) gray2 = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY) #使用 Shi-Tomasi 算法寻找关键点并计算特征描述子 sift = cv2.SIFT_create() kp1, des1 = sift.detectAndCompute(gray1, None) kp2, des2 = sift.detectAndCompute(gray2, None) #使用 FLANN 匹配器进行特征匹配 FLANN_INDEX_KDTREE = 0 index_params = dict(algorithm=FLANN_INDEX_KDTREE, trees=5) search_params = dict(checks=50) flann = cv2.FlannBasedMatcher(index_params, search_params) matches = flann.knnMatch(des1, des2, k=2) #选择好的匹配点 good = [] for m, n in matches: if m.distance < 0.7 * n.distance: good.append(m) #获取匹配点对应的坐标 src_pts = np.float32([kp1[m.queryIdx].pt for m in good]).reshape(-1, 1, 2) dst_pts = np.float32([kp2[m.trainIdx].pt for m in good]).reshape(-1, 1, 2) #使用 RANSAC 算法进行配准 M, mask = cv2.findHomography(src_pts, dst_pts, cv2.RANSAC, 5.0) #对第一张图像进行变换并输出结果 result = cv2.warpPerspective(img1, M, (img1.shape[1] + img2.shape[1], img1.shape[0])) #将第二张图像拼接到全景图中 result[0:img2.shape[0], img1.shape[1]:img1.shape[1] + img2.shape[1]] = img2 #输出全景图 cv2.namedWindow("result",cv2.WINDOW_NORMAL) cv2.imshow('result', result) cv2.waitKey(0) cv2.destroyAllWindows()改进这段代码,使其能够输出匹配连线图
可以使用cv2.drawMatchesKnn函数绘制匹配连线图,代码如下:
```
import cv2
import numpy as np
# 读入需要配准的两张图像
img1 = cv2.imread('men4.jpg')
img2 = cv2.imread('men3.jpg')
# 将图像转换为灰度图像
gray1 = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY)
gray2 = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY)
# 使用 Shi-Tomasi 算法寻找关键点并计算特征描述子
sift = cv2.SIFT_create()
kp1, des1 = sift.detectAndCompute(gray1, None)
kp2, des2 = sift.detectAndCompute(gray2, None)
# 使用 FLANN 匹配器进行特征匹配
FLANN_INDEX_KDTREE = 0
index_params = dict(algorithm=FLANN_INDEX_KDTREE, trees=5)
search_params = dict(checks=50)
flann = cv2.FlannBasedMatcher(index_params, search_params)
matches = flann.knnMatch(des1, des2, k=2)
# 选择好的匹配点
good = []
for m, n in matches:
if m.distance < 0.7 * n.distance:
good.append(m)
# 绘制匹配连线图
img3 = cv2.drawMatchesKnn(img1, kp1, img2, kp2, [good], None, flags=cv2.DrawMatchesFlags_NOT_DRAW_SINGLE_POINTS)
# 获取匹配点对应的坐标
src_pts = np.float32([kp1[m.queryIdx].pt for m in good]).reshape(-1, 1, 2)
dst_pts = np.float32([kp2[m.trainIdx].pt for m in good]).reshape(-1, 1, 2)
# 使用 RANSAC 算法进行配准
M, mask = cv2.findHomography(src_pts, dst_pts, cv2.RANSAC, 5.0)
# 对第一张图像进行变换并输出结果
result = cv2.warpPerspective(img1, M, (img1.shape[1] + img2.shape[1], img1.shape[0]))
# 将第二张图像拼接到全景图中
result[0:img2.shape[0], img1.shape[1]:img1.shape[1] + img2.shape[1]] = img2
# 输出全景图和匹配连线图
cv2.namedWindow("result", cv2.WINDOW_NORMAL)
cv2.imshow('result', result)
cv2.namedWindow("matches", cv2.WINDOW_NORMAL)
cv2.imshow('matches', img3)
cv2.waitKey(0)
cv2.destroyAllWindows()
```
其中,cv2.drawMatchesKnn函数的参数含义如下:
- img1:第一张图像
- kp1:第一张图像中的关键点
- img2:第二张图像
- kp2:第二张图像中的关键点
- matches:匹配结果
- outImg:输出图像,设为None表示不输出
- matchColor:匹配连线颜色
- singlePointColor:单个关键点颜色
- flags:绘制参数
阅读全文