一. 项目进展:
1.利用cpp程序直接生成视频和转图片
2.添加CRC码进行循环校验
3.进行传输速率测试
三. 技术尝试:
尝试使用海明校验,后改为CRC校验。
代码展示:
以下是裁剪二维码代码:
#include <opencv2/opencv.hpp>
#include <iostream>
#include <cmath>
using namespace cv;
using namespace std;
// 计算两点之间的距离
double distance(Point2f pt1, Point2f pt2) {
return sqrt(pow(pt2.x - pt1.x, 2) + pow(pt2.y - pt1.y, 2));
}
int* firstBlack(cv::Mat& image)
{
int* p = new int[2];
for (int i = 0; i < image.rows; ++i)
{
for (int j = 0; j < image.cols; ++j)
{
if (image.at<unsigned char>(i, j) <= 15)
{
p[0] = i;
p[1] = j;
return p;
}
}
}
}
int widtht(int x, int y, cv::Mat& image)
{
while (1)
{
++x;
if (image.at<unsigned char>(y, x) >= 15)
break;
}
return x - 1;
}
int main() {
// 读取手机拍摄的图片
Mat original_image = imread("1_1.png");
// 将原始图片转换为灰度图
Mat gray_image;
cvtColor(original_image, gray_image, COLOR_BGR2GRAY);
// 使用边缘检测找到定位区域
Mat edges;
Canny(gray_image, edges, 50, 150);
// 找到定位区域的轮廓
vector<vector<Point>> contours;
findContours(edges, contours, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE);
// 初始化第一个和第三个定位区域的坐标
Point2f first_locator, third_locator;
int t = 0;
// 遍历所有满足条件的定位区域,找到第一个和第三个定位区域
for (size_t i = 0; i < contours.size(); ++i) {
// 计算轮廓的面积
double area = contourArea(contours[i]);
// 计算轮廓的外接矩形
Rect locator_rect = boundingRect(contours[i]);
// 根据面积和高宽比筛选定位区
if (area > 5000 && area < 20000 && (double)locator_rect.width / locator_rect.height >= 0.95 && (double)locator_rect.width / locator_rect.height <= 1.05) {
// 计算当前定位区的中心点
Point2f center(locator_rect.x + locator_rect.width / 2.0, locator_rect.y + locator_rect.height / 2.0);
Mat cropped_image = original_image(locator_rect);
imshow("Cropped Image " + to_string(i + 1), cropped_image);
waitKey(0);
// 更新第一个和第三个定位区的坐标
cout << center.x << " " << center.y << endl;
if (center.x < 600 & center.y < 600) {
first_locator = center;
}
else if (center.x > 600 && center.y < 600) {
third_locator = center; // 改成第三次找到的定位区的中心点
// 找到第一个和第三个定位区域后退出循环
}
}
}
// 检查是否找到了第一个和第三个定位区域
if (first_locator != Point2f(0, 0) && third_locator != Point2f(0, 0)) {
// 计算旋转角度,使第一个和第三个定位区的中心点的纵坐标相同
double vertical_offset = third_locator.y - first_locator.y;
cout << first_locator.x << " " << first_locator.y << " " << third_locator.x << " " << third_locator.y << endl;
// 计算旋转中心
Point2f center(first_locator.x, first_locator.y + vertical_offset / 2.0); // 向上或向下移动一半的垂直偏移
// 计算旋转角度
double angle = atan2(third_locator.y - first_locator.y, third_locator.x - first_locator.x) * 180 / CV_PI;
// 计算旋转矩阵
Mat rot_mat = getRotationMatrix2D(center, angle, 1.0);
// 旋转图像
Mat rotated_image;
warpAffine(original_image, rotated_image, rot_mat, original_image.size(), INTER_CUBIC);
/*imshow("Rotated Image", rotated_image);*/
// 将原始图片转换为灰度图
Mat gray_image;
cvtColor(rotated_image, gray_image, COLOR_BGR2GRAY);
// 使用边缘检测找到定位区域
Mat edges;
Canny(gray_image, edges, 50, 150);
// 找到定位区域的轮廓
vector<vector<Point>> contours;
findContours(edges, contours, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE);
// 初始化最大最小坐标
int minX = INT_MAX, minY = INT_MAX;
int maxX = 0, maxY = 0;
// 遍历所有满足条件的定位区域,找到第一个和第三个定位区域
for (size_t i = 0; i < contours.size(); ++i) {
// 计算轮廓的面积
double area = contourArea(contours[i]);
// 计算轮廓的外接矩形
Rect locator_rect = boundingRect(contours[i]);
// 根据面积和高宽比筛选定位区
if (area > 5000 && area < 20000 && (double)locator_rect.width / locator_rect.height >= 0.8 && (double)locator_rect.width / locator_rect.height <= 1.2) {
/* Mat cropped_image = original_image(locator_rect);
imshow("Cropped Image " + to_string(i + 1), cropped_image);
waitKey(0);*/
minX = min(minX, locator_rect.x);
minY = min(minY, locator_rect.y);
maxX = max(maxX, locator_rect.x + locator_rect.width);
maxY = max(maxY, locator_rect.y + locator_rect.height);
}
}
// 检查是否找到了有效的定位区域
if (minX != INT_MAX && minY != INT_MAX && maxX != 0 && maxY != 0) {
// 计算定位区域的宽度和高度
int width = maxX - minX;
int height = maxY - minY;
// 裁剪图片
if (width > 0 && height > 0) {
Mat cropped_image = rotated_image(Rect(minX, minY, width, height));
// 例如,假设要压缩为 10 * 10 大小的图像
Mat resized_image;
// 缩放图像
//resize(cropped_image, resized_image, Size(new_width, new_height));
// 显示裁剪的图像
imshow("Cropped Image", cropped_image);
// 保存裁剪的图像到文件
imwrite("cropped_image2.jpg", cropped_image);
waitKey(0);
}
else {
cout << "Invalid width or height for cropping." << endl;
}
}
else {
cout << "No valid contour found." << endl;
}
}
else {
cout << "First and third locator not found." << endl;
}
return 0;
}