图像处理-以最快的方式跳到文件中的位置(C/C++)

image processing - Fastest way to skip to position in file (C/C++)

本文关键字:位置 C++ 图像处理 方式跳 文件      更新时间:2023-10-16

我有一些二进制格式的6k张图片(uint8(,我一次处理30行左右的图片块。这意味着我最终不得不跳过文件的大部分内容,直到到达我需要的位置。到目前为止,最快的方法似乎是将不必要的部分读取到临时变量中,然后删除该变量,但从逻辑上讲,这似乎不是最有效的方法。

ifstream img;
int startingPixel = N;
for(int frame=0;frame<numFrames;frame++){
    img.open("myfileAsFunctionOfFrame.bin",ios::in | ios::binary);
    img.read((char*) &tempArray[0], startingPixel*sizeof(uint8));
    img.read((char*) &myArray[frame*pixelsToRead], pixelsToRead*sizeof(uint8));
    img.close();
}
delete [] tempArray;

我试着使用img.ignore((,但速度变慢了。

有什么建议吗?

使用seekg方法。它正是为你想要做的事情而制作的。http://www.cplusplus.com/reference/iostream/istream/seekg/

希望它能帮助

对于ifstream,您希望使用seekg()

http://www.cplusplus.com/reference/iostream/istream/seekg/

对于ifstream,您可以使用seekg方法

文件:http://cplusplus.com/reference/iostream/istream/seekg/

img.seekg( 0, std::ios_base::cur ); // replace 0 with desired offset