OpenCV 摄像头中心点偏移量检测:Python 与 C++ 实现对比

本文将演示如何使用 OpenCV 库实现摄像头中心点偏移量检测,并对比 Python 和 C++ 版本的代码实现。

Python 版本

import cv2
import numpy as np

# 中心点定义
center = 320

# 打开摄像头,图像尺寸640*480(长*高),opencv存储值为480*640(行*列)
cap = cv2.VideoCapture(0)
while (1):
    ret, frame = cap.read()
    # 转化为灰度图
    if ret == False:  # 如果是最后一帧这个值为False
       break
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    # 大津法二值化
    retval, dst = cv2.threshold(gray, 0, 255, cv2.THRESH_OTSU)
    # 膨胀,白区域变大
    dst = cv2.dilate(dst, None, iterations=2)
    # # 腐蚀,白区域变小
    # dst = cv2.erode(dst, None, iterations=6)
    cv2.imshow('dst',dst)
    # 单看第400行的像素值
    color = dst[400]
    # 找到白色的像素点个数
    white_count = np.sum(color == 0)
    # 找到白色的像素点索引
    white_count_judge = np.sum(color == 255) #利用这个变量来查找摄像头是否观察到黑色
    if white_count_judge == 640:
        print('黑色像素点为0')
        pass
    else:
        white_index = np.where(color == 0)
        # 防止white_count=0的报错
        if white_count == 0:
            white_count = 1

        # 找到白色像素的中心点位置
        center = (white_index[0][white_count - 1] + white_index[0][0]) / 2
        direction = center - 320
        print(direction)
        # 计算出center与标准中心点的偏移量
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

# 释放清理
cap.release()
cv2.destroyAllWindows()

C++ 版本

#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;

int main()
{
    int center = 320;

    // 打开摄像头,图像尺寸640*480(长*高),opencv存储值为480*640(行*列)
    VideoCapture cap(0);
    Mat frame, gray, dst;
    while (1)
    {
        cap >> frame;
        // 转化为灰度图
        cvtColor(frame, gray, COLOR_BGR2GRAY);
        // 大津法二值化
        threshold(gray, dst, 0, 255, THRESH_OTSU);
        // 膨胀,白区域变大
        dilate(dst, dst, Mat(), Point(-1, -1), 2);
        // # 腐蚀,白区域变小
        // dst = cv2.erode(dst, None, iterations=6);
        imshow('dst', dst);
        // 单看第400行的像素值
        Mat color = dst.row(400);
        // 找到白色的像素点个数
        int white_count = countNonZero(color == 0);
        // 找到白色的像素点索引
        int white_count_judge = countNonZero(color == 255); //利用这个变量来查找摄像头是否观察到黑色
        if (white_count_judge == 640)
        {
            cout << '黑色像素点为0' << endl;
        }
        else
        {
            Mat white_index;
            findNonZero(color == 0, white_index);
            // 防止white_count=0的报错
            if (white_count == 0)
            {
                white_count = 1;
            }
            // 找到白色像素的中心点位置
            center = (white_index.at<Point>(0).x + white_index.at<Point>(white_count - 1).x) / 2;
            int direction = center - 320;
            cout << direction << endl;
            // 计算出center与标准中心点的偏移量
        }
        if (waitKey(1) == 'q')
        {
            break;
        }
    }

    // 释放清理
    cap.release();
    destroyAllWindows();
    return 0;
}

代码解释

  1. 打开摄像头:使用 cv2.VideoCapture(0)VideoCapture(0) 打开默认摄像头。
  2. 图像处理
    • 将图像转换为灰度图。
    • 使用大津法二值化图像,将图像分为黑色和白色区域。
    • 膨胀处理,将白色区域扩大。
  3. 中心点检测
    • 获取第 400 行的像素值。
    • 计算白色像素的个数。
    • 找到白色像素的索引,并计算白色像素的中心点位置。
  4. 偏移量计算
    • 计算中心点与标准中心点的偏移量。
  5. 显示结果
    • 使用 cv2.imshow()imshow() 显示处理后的图像。
    • 输出中心点偏移量。

代码对比

  • Python 版本使用 NumPy 库进行数组操作,代码简洁易懂。
  • C++ 版本使用 OpenCV 库提供的函数进行操作,代码效率更高。

总结

本文介绍了使用 OpenCV 库实现摄像头中心点偏移量检测,并对比了 Python 和 C++ 版本的代码实现。选择哪种语言取决于项目的具体需求。

OpenCV 摄像头中心点偏移量检测:Python 与 C++ 实现对比

原文地址: https://www.cveoy.top/t/topic/oyOy 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录