将图片存到堆中

C++语言 码拜 8年前 (2016-04-18) 1017次浏览
c++新手:怎么将视频中的图片存到内存的堆中?
求思路,谢谢大家将图片存到堆中
解决方案

20

可以试试opencv的imencode函数

10

#include <iostream>
#include <vector>
#include <string>
#include <opencv2\core\core.hpp>
#include <opencv2\highgui\highgui.hpp>
#include <opencv2\imgproc\imgproc.hpp>
using namespace std;
using namespace cv;
int main() {
	string fname("E:\Videos\tree.avi");
	VideoCapture capture(fname);
	if (!capture.isOpened())
		return 1;
	vector<Mat> frames;
	cout << "Frame Count: " << capture.get(CV_CAP_PROP_FRAME_COUNT) << endl;
	while (true) {
		Mat frame;
		if (!capture.read(frame))
			break;
		imshow("frame", frame);
		char key = waitKey(25);
		if (key == 27)
			break;
		frames.emplace_back(frame);
		cout << "frame --> " << frames.size() << endl;
	}
	destroyWindow("frame");
	capture.release();
	cout << "vector size: " << frames.size() << endl;
	for (auto it = frames.begin(); it != frames.end(); ++it) {
		imshow("vector", *it);
		waitKey(27);
	}
	return 0;
}
#include <iostream>
#include <vector>
#include <string>
#include <memory>
#include <opencv2\core\core.hpp>
#include <opencv2\highgui\highgui.hpp>
#include <opencv2\imgproc\imgproc.hpp>
using namespace std;
using namespace cv;
int main() {
	string fname("E:\Videos\tree.avi");
	VideoCapture capture(fname);
	if (!capture.isOpened())
		return 1;
	vector<shared_ptr<Mat>>  frames;
	cout << "Frame Count: " << capture.get(CV_CAP_PROP_FRAME_COUNT) << endl;
	int width = capture.get(CV_CAP_PROP_FRAME_WIDTH);
	int height = capture.get(CV_CAP_PROP_FRAME_HEIGHT);
	while (true) {
		Mat* frame = new Mat;
		if (!capture.read(*frame)) {
			delete frame;
			break;
		}
		imshow("frame", *frame);
		waitKey(25);
		frames.push_back(shared_ptr<Mat>(frame));
	}

	destroyWindow("frame");
	capture.release();
	cout << "vector size: " << frames.size() << endl;
	for (auto it = frames.begin(); it != frames.end(); ++it) {
		imshow("vector", **it);
		waitKey(27);
	}
}

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明将图片存到堆中
喜欢 (0)
[1034331897@qq.com]
分享 (0)