一、实验目的
(1)掌握图像特征点提取
(2)掌握图像的边缘提取
(3)掌握Hough形状检测
(4)掌握LeNet的架构
二、实验内容与记录
(1)使用OpenCV对图像进行Harris,SIFT特征点提取,并标注特征点。
(2)使用OpenCV对图像Canny边缘检测,显示并保存。
(3)使用OpenCV对house.tif进行霍夫直线检测,对硬币图片进行霍夫圆形检测。
(4)使用OpenCV对两幅有重叠的图片匹配后进行拼接,生成全景图。
import cv2
import numpy as np
# 读取两张重叠的图片
img1 = cv2.imread("1.jpg")
img2 = cv2.imread("2.jpg")
# 创建SIFT特征提取器
sift = cv2.xfeatures2d.SIFT_create()
# 提取图片特征点及描述符
kp1, des1 = sift.detectAndCompute(img1, None)
kp2, des2 = sift.detectAndCompute(img2, None)
# 创建FLANN匹配器
flann = cv2.FlannBasedMatcher()
# 匹配特征点
matches = flann.knnMatch(des1, des2, k=2)
# 选择优秀的匹配点
good_matches = []
for m, n in matches:
if m.distance < 0.7 * n.distance:
good_matches.append(m)
# 获取匹配点坐标
src_pts = np.float32([kp1[m.queryIdx].pt for m in good_matches]).reshape(-1, 1, 2)
dst_pts = np.float32([kp2[m.trainIdx].pt for m in good_matches]).reshape(-1, 1, 2)
# 计算单应性矩阵
H, mask = cv2.findHomography(src_pts, dst_pts, cv2.RANSAC, 5.0)
# 计算图像变换后的尺寸
h, w = img1.shape[:2]
img2_aligned = cv2.warpPerspective(img2, H, (w, h))
# 进行拼接
result = np.zeros((h, w*2, 3), dtype=np.uint8)
result[:, :w, :] = img1
result[:, w:, :] = img2_aligned
# 输出全景图
cv2.imwrite("panorama.jpg", result)
(5)复现LeNet 5 ,实现对MNIST手写数字的识别
from multiprocessing import freeze_support
import torch
import torch.nn as nn
import torch.optim as optim
import torchvision
import torchvision.transforms as transforms
# Define LeNet-5 model
class LeNet5(nn.Module):
def __init__(self):
super(LeNet5, self).__init__()
self.conv1 = nn.Conv2d(1, 6, 5)
self.pool1 = nn.MaxPool2d(2)
self.conv2 = nn.Conv2d(6, 16, 5)
self.pool2 = nn.MaxPool2d(2)
self.fc1 = nn.Linear(16 * 4 * 4, 120)
self.fc2 = nn.Linear(120, 84)
self.fc3 = nn.Linear(84, 10)
def forward(self, x):
x = self.pool1(torch.relu(self.conv1(x)))
x = self.pool2(torch.relu(self.conv2(x)))
x = x.view(-1, 16 * 4 * 4)
x = torch.relu(self.fc1(x))
x = torch.relu(self.fc2(x))
x = self.fc3(x)
return x
if __name__ == '__main__':
freeze_support()
# Load MNIST dataset
transform = transforms.Compose(
[transforms.ToTensor(),
transforms.Normalize((0.5,), (0.5,))]) # Normalize pixel values to [-1, 1]
trainset = torchvision.datasets.MNIST(root='./data', train=True,
download=True, transform=transform)
trainloader = torch.utils.data.DataLoader(trainset, batch_size=64,
shuffle=True, num_workers=2)
testset = torchvision.datasets.MNIST(root='./data', train=False,
download=True, transform=transform)
testloader = torch.utils.data.DataLoader(testset, batch_size=64,
shuffle=False, num_workers=2)
# Initialize LeNet-5 model, loss function, and optimizer
net = LeNet5()
criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(net.parameters(), lr=0.001, momentum=0.9)
# Train the model
for epoch in range(10): # Change the number of epochs as needed
running_loss = 0.0
for i, data in enumerate(trainloader, 0):
inputs, labels = data
optimizer.zero_grad()
outputs = net(inputs)
loss = criterion(outputs, labels)
loss.backward()
optimizer.step()
running_loss += loss.item()
print('Epoch [%d], Loss: %.4f' % (epoch + 1, running_loss / len(trainloader)))
print('Finished training')
# Test the model
correct = 0
total = 0
with torch.no_grad():
for data in testloader:
images, labels = data
outputs = net(images)
_, predicted = torch.max(outputs.data, 1)
total += labels.size(0)
correct += (predicted == labels).sum().item()
print('Accuracy of the network on the 10000 test images: %.2f %%' % (
100 * correct / total))