opencv 3.0 windows 计时函数 timer

本文对比了C语言中三种不同的计时方法:纯C高精度测量、纯C低精度测量及C++ OpenCV高精度测量,通过实际代码示例详细解析了每种方法的原理、实现及应用场景。

计时函数

1. 纯C 高精度(100ns)测量慢

#include <Windows.h>
#include <stdio.h>
int timer1_test()
{
    LARGE_INTEGER start;
    LARGE_INTEGER end ;
    LARGE_INTEGER frequency;

    int i = 0;

    if (!QueryPerformanceFrequency(&frequency))
    {
        return -1;
    }

    QueryPerformanceCounter(&start); //开始计时

//     for (int i = 0; i < 100000; ++i)
//     {
//         ;// 用循环来测试计时
//     }
    Sleep(1);

    QueryPerformanceCounter(&end); //结束计时

    printf("main cost:%f\n", (double)(end.QuadPart - start.QuadPart) / (double)frequency.QuadPart); //打印for循环执行时间

    getchar();
    return 0;
}

2.纯C 低精度(1ms)测量快

#include <stdio.h>
#include <time.h>

void timer2_test()
{
 	clock_t start,end;
 	start=clock();

    Sleep(1000);

 	end=clock();
 	printf("The differences: %d ms\n",end-start);
}


3.C++  OPENCV测试 高精度

 

#include"opencv.hpp"

void timer3_test()
{
    int64 start=0,end=0;
    start = getTickCount();

    Sleep(1000);

    end = getTickCount();
    cout << "The differences: " << 1000.0*(end - start)/getTickFrequency()<<" ms"<< endl;
}


 

 

 

 

### 如何在PyQt中集成或使用OpenCV 为了使Python中的OpenCV能够与PyQt无缝协作,通常有两种主要方法来实现图像数据的交互:一种是通过`QImage`类转换OpenCV图像以便于显示;另一种是在两者之间传递视频流。 #### 方法一:将OpenCV图像转换为QImage用于静态图片展示 由于OpenCV默认使用的色彩空间为BGR而Qt则采用RGB模式,因此需要先调整颜色通道顺序再创建相应的`QImage`对象[^1]: ```python import cv2 from PyQt5.QtGui import QImage, QPixmap from PyQt5.QtWidgets import QLabel def convert_cv_qt(cv_img): """Convert from an opencv image to QPixmap""" rgb_image = cv2.cvtColor(cv_img, cv2.COLOR_BGR2RGB) h, w, ch = rgb_image.shape bytes_per_line = ch * w qt_image = QImage(rgb_image.data, w, h, bytes_per_line, QImage.Format_RGB888) return QPixmap.fromImage(qt_image) # 假设有一个名为label的QLabel实例用来显示图像 cv_img = cv2.imread(&#39;path_to_your_image&#39;) pixmap = convert_cv_qt(cv_img) label.setPixmap(pixmap) ``` 此段代码展示了如何读取一张本地存储的照片文件并将其转化为可以在PyQt界面组件上呈现的形式。 #### 方法二:实时捕获摄像头帧并在GUI界面上更新 对于涉及动态视觉输入的应用程序来说,比如访问计算机自带相机获取连续画面,则可以利用定时器机制定期刷新窗口内的视图内容[^2]: ```python import sys import cv2 from PyQt5.QtCore import QTimer from PyQt5.QtWidgets import QApplication, QMainWindow, QLabel class VideoWindow(QMainWindow): def __init__(self): super().__init__() self.setWindowTitle("Camera Feed") self.image_label = QLabel(self) self.setCentralWidget(self.image_label) self.cap = cv2.VideoCapture(0) # 打开默认摄像设备 self.timer = QTimer() self.timer.timeout.connect(self.update_frame) self.timer.start(30) def update_frame(self): ret, frame = self.cap.read() if not ret: print("Failed to grab frame") return pixmap = convert_cv_qt(frame) self.image_label.setPixmap(pixmap) if __name__ == &#39;__main__&#39;: app = QApplication(sys.argv) win = VideoWindow() win.show() sys.exit(app.exec_()) ``` 这段脚本定义了一个简单的应用程序框架,在其中实现了周期性的图像采集以及即时渲染功能。每当计时器触发一次超时时就会调用`update_frame()`函数从而完成新一轮的画面捕捉和UI重绘操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值