C++ 是一门古老而复杂的语言,绝不是一门可以速成的语言,学习它需要有意识的刻意练习和长时间的持续不断的磨练。而大多数人不太能耐得住寂寞,喜欢速成,所以像《 21 天学通 C++ 》这种书就比较受欢迎,卖得很好。通常那些干了一两年就说自己熟悉(甚至精通) C++ 的程序员只能算是轻浮的。(貌似说的就是我自己,哈哈)
Linux 之父 Linus 就曾说:“ C++ 是一门很恐怖的语言,而比它更恐怖的是很多不合格的程序员在使用着它”。
所以,要学习 C++ 并打算将其作为自己的首选编程语言,就要做好吃十年寒窗苦的准备,要耐得住寂寞,经得起考验,最终才能举重若轻,有所成就。
也正因为此,很多人不大愿意学习 C++,还会找一些理由,比如 C++ 没落了、应用前景不广泛了作为理由。其实 C++ 仍然在不断发展,生命力依然旺盛,有大量的行业和不计其数的应用正在源源不断地引入 C++ 语言,它的前景依然看好。(最近的项目让我体会到了C++无处不在)
由此,小编决定重拾C++,刻苦钻研,今天正好碰到了一个图像处理知识:
大体任务需要知识点:遍历文件夹下所有图片,就是将YUV图像转换成RGB,opencv又无此格式转换函数,如何将图像拷贝进buffer, 从buffer中读取图像
1、从文件夹中读取图像进入文件进入buffer
// read a file into memory
#include // std::cout
#include // std::ifstream
int main () {
std::ifstream is ("test.txt", std::ifstream::binary);
if (is) {
// get length of file:
is.seekg (0, is.end);
int length = is.tellg();
is.seekg (0, is.beg);
char * buffer = new char [length];
std::cout << "Reading " << length << " characters... ";
// read data as a block:
is.read (buffer,length);
if (is)
std::cout << "all characters read successfully.";
else
std::cout << "error: only " << is.gcount() << " could be read";
is.close();
// ...buffer contains the entire file...
delete[] buffer;
}
return 0;
}
以此为例,将图像拷贝进buffer
输入图像的文件 String str_Route,
输出图像的动态内存unsigned char *psrc_image
int ReadFileYUV(String str_Route, unsigned char *psrc_image) {
std::ifstream is(str_Route, std::ifstream::binary);
if (is) {
// get length of file:
is.seekg(0, is.end);
int length = is.tellg();
is.seekg(0, is.beg);
char * buffer = new char[length];
std::cout << "Reading " << length << " characters... ";
// read data as a block:
is.read(buffer, length);
if (is)
std::cout << "all characters read successfully.";
else
std::cout << "error: only " << is.gcount() << " could be read";
is.close();
// ...buffer contains the entire file...
memcpy(psrc_image, buffer, length);
delete[] buffer;
}
return 0;
}
遍历文件夹下图片(opencv glob函数)
vector read_images_in_folder(cv::String pattern)
{
vector fn;
glob(pattern, fn, false);
vector imagesname;
size_t count = fn.size(); //number of png files in images folder
for (size_t i = 0; i < count; i++)
{
imagesname.push_back(fn[i]);
//cout << fn[i]<
3、调用代码
src = (unsigned char *)malloc(2560 * 720);
ReadFileYUV(filename[i], src);
YUV转RGB代码
#include "opencv2/opencv.hpp"
#include
#include
#include
using namespace cv;
using namespace std;
string num2str(int i)
{
stringstream ss;
ss << i;
return ss.str();
}
#define RANGE_INT(iVal, iMin, iMax) ( ( ( iVal ) > ( iMin ) ) ? ( ( ( iVal ) <= ( iMax ) ) ? ( iVal ) : ( iMax ) ) : ( iMin ) )
#define ROUND_SHR_POSITIVE(Dividend, iShiftRightCount) ( ( ( Dividend ) & ( 1 << ( ( iShiftRightCount ) - 1 ) ) ) ? ( ( Dividend ) >> ( iShiftRightCount ) ) + 1 : ( ( Dividend ) >> ( iShiftRightCount ) ) )
#define ROUND_SHR_NEGATIVE(Dividend, iShiftRightCount) ( -( ( ( -( Dividend ) ) & ( 1 << ( ( iShiftRightCount ) - 1 ) ) ) ? ( ( -( Dividend ) ) >> ( iShiftRightCount ) ) + 1 : ( ( -( Dividend ) ) >> ( iShiftRightCount ) ) ) )
#define ROUND_SHR(Dividend, iShiftRightCount) ( ( ( Dividend ) >= 0 ) ? ROUND_SHR_POSITIVE( Dividend, iShiftRightCount ) : ROUND_SHR_NEGATIVE( Dividend, iShiftRightCount ) )
vector read_images_in_folder(cv::String pattern)
{
vector fn;
glob(pattern, fn, false);
vector imagesname;
size_t count = fn.size(); //number of png files in images folder
for (size_t i = 0; i < count; i++)
{
imagesname.push_back(fn[i]);
//cout << fn[i]< filename(read_images_in_folder(pathname));
std::fstream fs;
for (int i = 0; i < filename.size(); i++)
{
cout << "["<