C++OpenCV缩小了视频捕获帧的尺寸,并进行了拉伸和裁剪



我正在尝试为从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
}

玩了一段时间后,我发现在做"在";调用,将0指定为第三个参数(例如binFrame.at<uint8_t>(i,j, 0) = as8bit(avg);(可修复此问题。

最新更新