【问题标题】:C++ OpenCV downsized videoCapture frame stretched and croppedC ++ OpenCV缩小了videoCapture帧的拉伸和裁剪
【发布时间】:2020-11-18 21:27:38
【问题描述】:

我正在尝试获取从 python 转换为 C++ 的捕获热视频的校准程序,该过程的第一步是将图像中的像素从 480x640 缩小到 240x320,因此像素箱为 2x2。合并后返回的图像(使用与正确运行的 python 版本相同的逻辑)返回的图像是图像的左半部分,在图像的宽度上拉伸,而不是仅以较小的分辨率给出整个图像。

#include <opencv2/opencv.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/videoio/videoio.hpp>
#include <opencv2/video.hpp>
#include <opencv2/imgcodecs.hpp>
#include <iostream>

using namespace std;
using namespace cv;

Mat binImg(Mat);
int asInt(uint8_t);
uint8_t as8bit(int);
Mat rotate(Mat, double);


int main(int argc, char *argv[]){
    VideoCapture cap(argv[1]);
    int frameCount = cap.get(cv::CAP_PROP_FRAME_COUNT);
    int frameWidth = cap.get(cv::CAP_PROP_FRAME_WIDTH);
    int frameHeight = cap.get(cv::CAP_PROP_FRAME_HEIGHT);

    Mat buf [frameCount]; //create new array of Mat for the calibrated video
    int fc = 0;
    VideoWriter video("cppThermalTest.avi",CV_FOURCC('X','V','I','D'), 15, Size(240, 320), false); //create empty video @15fps, 320x240, isColor=false
    
    while (fc < frameCount){
        Mat frame(640, 480, CV_8UC1, Scalar(70));
        cap >> frame;
        Mat temp = binImg(frame);//bin the frame
        imshow("test", temp);
        imwrite("test.jpg", temp);
        waitKey(0);
        video.write(temp); //write the binned frame to the video
        cout << fc << endl;
        fc++;
    }
    cap.release();
    video.release();
    return 0;
}

Mat binImg(Mat frame){
    int frameWidth = frame.cols / 2;  //480 / 2
    int frameHeight = frame.rows / 2; //640 / 2
    cout << frameHeight << " " << frameWidth << endl;
    Mat binFrame(frameHeight, frameWidth, CV_8UC1);
    for(int i=0; i<binFrame.rows; i++){

        for(int j=0; j<binFrame.cols; j++){
        
            int ul = asInt(frame.at<uint8_t>((2*i),(2*j)));
            int bl = asInt(frame.at<uint8_t>(((2*i)+1),(2*j)));
            int ur = asInt(frame.at<uint8_t>((2*i), ((2*j)+1)));
            int br = asInt(frame.at<uint8_t>(((2*i)+1),((2*j)+1)));
            int avg = (ul + ur + bl + br) / 4;
            binFrame.at<uint8_t>(i,j) = as8bit(avg); //set the matrix element to the new value
        }
    }
    return binFrame;
}

int asInt(uint8_t val){
    //convert unsigned 8 bit int to int
    int temp = val;
    return temp;
}

uint8_t as8bit(int val){
    //convert int to unsigned 8 bit int
    uint8_t temp = val;
    return temp;
}

Mat rotate(Mat src, double angle){   //rotate function returning mat object with parametres imagefile and angle    
    Mat dst;      //Mat object for output image file
    Point2f pt(src.cols/2., src.rows/2.);          //point from where to rotate    
    Mat r = getRotationMatrix2D(pt, angle, 1.0);      //Mat object for storing after rotation
    warpAffine(src, dst, r, Size(src.cols, src.rows));  ///applie an affine transforation to image.
    return dst;         //returning Mat object for output image file
}

【问题讨论】:

    标签: c++ opencv image-processing


    【解决方案1】:

    玩了一会儿之后,我发现在执行“at”调用时,将 0 指定为第三个参数(例如 binFrame.at&lt;uint8_t&gt;(i,j, 0) = as8bit(avg);)可以解决问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多