我正在從視頻中創建一個影像。但是我閱讀了視頻并創建了幀,然后旋轉了幀并裁剪了幀,這些裁剪的幀被保存了。這些裁剪的幀應組合起來以創建單個影像。所以我將它們附加到 Mat 的向量中,然后想要水平連接它們。我做了,但它沒有顯示任何影像,也沒有保存影像。
我想做一些類似于 python 的事情
list_images = []
for img in glob.glob(crop_dir "*.jpg"):
n = cv2.imread(img,0)
list_images.append(n)
im_v = np.concatenate(list_images)
cv2.imwrite("finalimg.jpg",im_v)
我的變數imglist是型別
class std::vector<class cv::Mat,class std::allocator<class cv::Mat> >
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>
#include <filesystem>
using namespace std;
using namespace cv;
namespace fs = std::filesystem;
string crop_dir = "Task2_Resources/CropImages/";
void delete_dir_content(const fs::path& dir_path) {
for (auto& path : fs::directory_iterator(dir_path)) {
fs::remove_all(path);
}
}
int main() {
system("CLS");
if (!fs::is_directory(crop_dir) || !fs::exists(crop_dir)) { // Check if folder exists
fs::create_directory(crop_dir); // create folder
}
delete_dir_content(crop_dir);
VideoCapture vid_capture("Task2_Resources/sample201.avi");
int count = 0;
Mat finalimg,mimg;
vector<cv::Mat> imglist;
Mat image;
int x = 190, y = 59, h = 1, w = 962;
if (!vid_capture.isOpened()){
std::cout << "Cannot open the video file" << endl;
return -1;
}
while (true) {
Mat frame , rimg;
vector<Mat> blocks;
bool isSuccess = vid_capture.read(frame);
if (!isSuccess){
std::cout << "Cannot read the frame from video file" << endl;
break;
}
//imshow("Frames", frame);
count ;
float width = frame.cols;
float height = frame.rows;
Point2f center = Point2f((width / 2), (height / 2));
double degree = -0.2;
double scale = 1.0;
Mat change = getRotationMatrix2D(center, degree, scale); // Rotate & scale
warpAffine(frame, rimg, change, frame.size(), cv::INTER_CUBIC, cv::BORDER_CONSTANT, cv::Scalar(0, 0, 0));
Mat cropped_image = rimg(Range(y,y h), Range(x, x w));
string fname = to_string(count) ".jpg";
cv::imwrite(crop_dir fname, cropped_image);
imglist.push_back(cropped_image);
}
hconcat(imglist,image);
cout << typeid(imglist).name() << endl;
cv::imwrite("final.jpg", image);
std::cout << "Total frames created " << count << endl;
return 0;
}
Visual Studio 除錯控制臺

uj5u.com熱心網友回復:
我能注意到的唯一問題是h = 1- 這使得高度cropped_image僅為 1 個像素。
由于您沒有報告任何錯誤訊息,我們無法真正說出它為什么不起作用。
我建議您使用除錯器,并逐步迭代代碼(為此使用 IDE)。
為了構建一個作業示例,您可以按照接下來描述的階段進行操作。
其中一個問題可能與sample201.avi視頻檔案有關。
為了使答案可重現,我們可以使用
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/430352.html
上一篇:如何在不包括小時的情況下比較日期
下一篇:最近鄰搜索對于多個資料來說太長了
