常用代码片记录

本文详细介绍使用OpenCV进行图像倾斜矩形校正的方法,同时分享了如何将一系列图像合成视频的实用技巧,包括视频格式的保存及播放效果优化。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1,科学计数法,把小数写成String 1e-X的形式;
 

    double Cells_density9 =0.2;

    int ex9 = 0;
    while (true) {
        ex9++;
        Cells_density9 = Cells_density9 * 10;
        if (Cells_density9 > 1) {
            break;
        }
    }

    int intCells_density9 = Cells_density9 * 100;

    Cells_density9 = (double)intCells_density9 / 100;

    std::string Cells_density_QSTR9;
    Cells_density_QSTR9 = std::to_string(Cells_density9) + "e-" + std::to_string(ex9);

 

2,opencv倾斜矩形转正的矩形

#include "highgui/highgui.hpp"    
#include "imgproc/imgproc.hpp"    
#include "iostream"  

using namespace std;
using namespace cv;

int main(int argc, char* argv[])
{
    Mat imageSource = imread("D:/testi/QQ图片20200813163358.jpg", 1);
    imshow("Source Image", imageSource);
    vector<Point> contour;
    Point p1(1648, 416), p2(1723, 723), p3(1787, 375), p4(1869, 685);//四个矩形点大概的就可
    contour.push_back(p1);
    contour.push_back(p2);
    contour.push_back(p3);
    contour.push_back(p4);
    RotatedRect rect = minAreaRect(contour);//外接矩形

    Point2f vertices[4];
    rect.points(vertices);//外接矩形的4个顶点
    for (int i = 0; i < 4; i++)//画矩形
        line(imageSource, vertices[i], vertices[(i + 1) % 4], Scalar(255, 0, 0));

    /*Rect brect = rect.boundingRect();
    rectangle(imageSource, brect, Scalar(255, 0, 0));*/
    imshow("Source Image1", imageSource);
    Point2f center = rect.center;//外接矩形中心点坐标
    Mat rot_mat = getRotationMatrix2D(center, rect.angle, 1.0);//求旋转矩阵
    Mat rot_image;
    Size dst_sz(imageSource.size());
    warpAffine(imageSource, rot_image, rot_mat, dst_sz);//原图像旋转
    imshow("rot_image", rot_image);
    Mat result1 = rot_image(Rect(center.x - (rect.size.width / 2), center.y - (rect.size.height / 2), rect.size.width, rect.size.height));//提取ROI
    imshow("result", result1);

    waitKey(0);
    return 0;
}

3,保存视频,记得ffmpeg dll放到exe同文件夹下

#include<windows.h>
#include<Shlwapi.h>
#include <opencv2/opencv_modules.hpp>
#include<opencv2/core/core.hpp>
#include<opencv2/highgui/highgui.hpp>
#include<opencv2/imgproc/imgproc.hpp>
#include <iostream>
#include<string>
#include<vector>

using namespace cv;
using namespace std;
int main()
{

	VideoWriter video("C:\\Users\\MLOONG\\Desktop\\yolov4\\darknet-master\\build\\darknet\\x64\\data\\testvideo.mp4", CV_FOURCC('D', 'I', 'V', 'X'), 15.0, Size(1200, 600));

	String img_path = "C:\\Users\\MLOONG\\Desktop\\yolov4\\darknet-master\\build\\darknet\\x64\\JPEGImages\\*.jpg";
	vector<String> img;

	glob(img_path, img, false);

	size_t count = img.size();
	for (size_t i = 0; i < count; i++)
	{
		Mat image = imread(img[i]);
		resize(image, image, Size(1200, 600));
		video << image;
		video << image;
		video << image;
		video << image;
		video << image;
		cout <<"加入"<< img[i] << endl;
		imshow("img", image);
		cvWaitKey(1);
	}
	cout << "处理完毕!" << endl;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值