使用Dev C+加载映像+; 我的目标比较简单,就是用C++加载图像。我用DEV C++编译和运行这些代码。我已经在编译器选项和参数中链接了这些各自的标题 #include <vector> #include <fstream> #include <GL/gl.h> #include <GL/glu.h> #include <cstdint> #include <stdexcept> #include <cstring> typedef union PixelInfo { std::uint32_t Colour; struct { std::uint8_t R, G, B, A; }; } *PPixelInfo; class Tga { private: std::vector<std::uint8_t> Pixels; bool ImageCompressed; std::uint32_t width, height, size, BitsPerPixel; public: Tga(const char* FilePath); std::vector<std::uint8_t> GetPixels() {return this->Pixels;} std::uint32_t GetWidth() const {return this->width;} std::uint32_t GetHeight() const {return this->height;} bool HasAlphaChannel() {return BitsPerPixel == 32;} }; Tga::Tga(const char* FilePath) { std::fstream hFile(FilePath, std::ios::in | std::ios::binary); if (!hFile.is_open()){throw std::invalid_argument("File Not Found.");} std::uint8_t Header[18] = {0}; std::vector<std::uint8_t> ImageData; static std::uint8_t DeCompressed[12] = {0x0, 0x0, 0x2, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}; static std::uint8_t IsCompressed[12] = {0x0, 0x0, 0xA, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}; hFile.read(reinterpret_cast<char*>(&Header), sizeof(Header)); if (!std::memcmp(DeCompressed, &Header, sizeof(DeCompressed))) { BitsPerPixel = Header[16]; width = Header[13] * 256 + Header[12]; height = Header[15] * 256 + Header[14]; size = ((width * BitsPerPixel + 31) / 32) * 4 * height; if ((BitsPerPixel != 24) && (BitsPerPixel != 32)) { hFile.close(); throw std::invalid_argument("Invalid File Format. Required: 24 or 32 Bit Image."); } ImageData.resize(size); ImageCompressed = false; hFile.read(reinterpret_cast<char*>(ImageData.data()), size); } else if (!std::memcmp(IsCompressed, &Header, sizeof(IsCompressed))) { BitsPerPixel = Header[16]; width = Header[13] * 256 + Header[12]; height = Header[15] * 256 + Header[14]; size = ((width * BitsPerPixel + 31) / 32) * 4 * height; if ((BitsPerPixel != 24) && (BitsPerPixel != 32)) { hFile.close(); throw std::invalid_argument("Invalid File Format. Required: 24 or 32 Bit Image."); } PixelInfo Pixel = {0}; int CurrentByte = 0; std::size_t CurrentPixel = 0; ImageCompressed = true; std::uint8_t ChunkHeader = {0}; int BytesPerPixel = (BitsPerPixel / 8); ImageData.resize(width * height * sizeof(PixelInfo)); do { hFile.read(reinterpret_cast<char*>(&ChunkHeader), sizeof(ChunkHeader)); if(ChunkHeader < 128) { ++ChunkHeader; for(int I = 0; I < ChunkHeader; ++I, ++CurrentPixel) { hFile.read(reinterpret_cast<char*>(&Pixel), BytesPerPixel); ImageData[CurrentByte++] = Pixel.B; ImageData[CurrentByte++] = Pixel.G; ImageData[CurrentByte++] = Pixel.R; if (BitsPerPixel > 24) ImageData[CurrentByte++] = Pixel.A; } } else { ChunkHeader -= 127; hFile.read(reinterpret_cast<char*>(&Pixel), BytesPerPixel); for(int I = 0; I < ChunkHeader; ++I, ++CurrentPixel) { ImageData[CurrentByte++] = Pixel.B; ImageData[CurrentByte++] = Pixel.G; ImageData[CurrentByte++] = Pixel.R; if (BitsPerPixel > 24) ImageData[CurrentByte++] = Pixel.A; } } } while(CurrentPixel < (width * height)); } else { hFile.close(); throw std::invalid_argument("Invalid File Format. Required: 24 or 32 Bit TGA File."); } hFile.close(); this->Pixels = ImageData; } int main() { Tga info = Tga("C:/Users/Lenovo/Desktop/OpenGL/sabahpride.tga"); GLuint texture = 0; glGenTextures(1, &texture); glBindTexture(GL_TEXTURE_2D, texture); glTexImage2D(GL_TEXTURE_2D, 0, info.HasAlphaChannel() ? GL_RGBA : GL_RGB, info.GetWidth(), info.GetWidth(), 0, info.HasAlphaChannel() ? GL_RGBA : GL_RGB, GL_UNSIGNED_BYTE, info.GetPixels().data()); }

使用Dev C+加载映像+; 我的目标比较简单,就是用C++加载图像。我用DEV C++编译和运行这些代码。我已经在编译器选项和参数中链接了这些各自的标题 #include <vector> #include <fstream> #include <GL/gl.h> #include <GL/glu.h> #include <cstdint> #include <stdexcept> #include <cstring> typedef union PixelInfo { std::uint32_t Colour; struct { std::uint8_t R, G, B, A; }; } *PPixelInfo; class Tga { private: std::vector<std::uint8_t> Pixels; bool ImageCompressed; std::uint32_t width, height, size, BitsPerPixel; public: Tga(const char* FilePath); std::vector<std::uint8_t> GetPixels() {return this->Pixels;} std::uint32_t GetWidth() const {return this->width;} std::uint32_t GetHeight() const {return this->height;} bool HasAlphaChannel() {return BitsPerPixel == 32;} }; Tga::Tga(const char* FilePath) { std::fstream hFile(FilePath, std::ios::in | std::ios::binary); if (!hFile.is_open()){throw std::invalid_argument("File Not Found.");} std::uint8_t Header[18] = {0}; std::vector<std::uint8_t> ImageData; static std::uint8_t DeCompressed[12] = {0x0, 0x0, 0x2, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}; static std::uint8_t IsCompressed[12] = {0x0, 0x0, 0xA, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}; hFile.read(reinterpret_cast<char*>(&Header), sizeof(Header)); if (!std::memcmp(DeCompressed, &Header, sizeof(DeCompressed))) { BitsPerPixel = Header[16]; width = Header[13] * 256 + Header[12]; height = Header[15] * 256 + Header[14]; size = ((width * BitsPerPixel + 31) / 32) * 4 * height; if ((BitsPerPixel != 24) && (BitsPerPixel != 32)) { hFile.close(); throw std::invalid_argument("Invalid File Format. Required: 24 or 32 Bit Image."); } ImageData.resize(size); ImageCompressed = false; hFile.read(reinterpret_cast<char*>(ImageData.data()), size); } else if (!std::memcmp(IsCompressed, &Header, sizeof(IsCompressed))) { BitsPerPixel = Header[16]; width = Header[13] * 256 + Header[12]; height = Header[15] * 256 + Header[14]; size = ((width * BitsPerPixel + 31) / 32) * 4 * height; if ((BitsPerPixel != 24) && (BitsPerPixel != 32)) { hFile.close(); throw std::invalid_argument("Invalid File Format. Required: 24 or 32 Bit Image."); } PixelInfo Pixel = {0}; int CurrentByte = 0; std::size_t CurrentPixel = 0; ImageCompressed = true; std::uint8_t ChunkHeader = {0}; int BytesPerPixel = (BitsPerPixel / 8); ImageData.resize(width * height * sizeof(PixelInfo)); do { hFile.read(reinterpret_cast<char*>(&ChunkHeader), sizeof(ChunkHeader)); if(ChunkHeader < 128) { ++ChunkHeader; for(int I = 0; I < ChunkHeader; ++I, ++CurrentPixel) { hFile.read(reinterpret_cast<char*>(&Pixel), BytesPerPixel); ImageData[CurrentByte++] = Pixel.B; ImageData[CurrentByte++] = Pixel.G; ImageData[CurrentByte++] = Pixel.R; if (BitsPerPixel > 24) ImageData[CurrentByte++] = Pixel.A; } } else { ChunkHeader -= 127; hFile.read(reinterpret_cast<char*>(&Pixel), BytesPerPixel); for(int I = 0; I < ChunkHeader; ++I, ++CurrentPixel) { ImageData[CurrentByte++] = Pixel.B; ImageData[CurrentByte++] = Pixel.G; ImageData[CurrentByte++] = Pixel.R; if (BitsPerPixel > 24) ImageData[CurrentByte++] = Pixel.A; } } } while(CurrentPixel < (width * height)); } else { hFile.close(); throw std::invalid_argument("Invalid File Format. Required: 24 or 32 Bit TGA File."); } hFile.close(); this->Pixels = ImageData; } int main() { Tga info = Tga("C:/Users/Lenovo/Desktop/OpenGL/sabahpride.tga"); GLuint texture = 0; glGenTextures(1, &texture); glBindTexture(GL_TEXTURE_2D, texture); glTexImage2D(GL_TEXTURE_2D, 0, info.HasAlphaChannel() ? GL_RGBA : GL_RGB, info.GetWidth(), info.GetWidth(), 0, info.HasAlphaChannel() ? GL_RGBA : GL_RGB, GL_UNSIGNED_BYTE, info.GetPixels().data()); },c++,opencv,opengl,dev-c++,C++,Opencv,Opengl,Dev C++,这项任务明天就要完成了,所以我真的需要尽可能多的帮助。谢谢。当您使用调试器单步执行程序时,当据称加载并显示图像时,调试器显示了什么?它正在读取文件吗?@Sam Varshavchik谢谢你的回复,我忘记输入当我使用调试器时,它显示[Error]无法创建Sabah.exe的预编译头标志:权限被拒绝的deniedDebuggers与创建预编译头无关。他们不编译任何东西。您应该花一些时间学习如何调试自己的代码。了解如何使用调试器是每个C++开发者所需的技能。1。您没有初始化OpenGL 2。您没有渲染

这项任务明天就要完成了,所以我真的需要尽可能多的帮助。谢谢。

当您使用调试器单步执行程序时,当据称加载并显示图像时,调试器显示了什么?它正在读取文件吗?@Sam Varshavchik谢谢你的回复,我忘记输入当我使用调试器时,它显示[Error]无法创建Sabah.exe的预编译头标志:权限被拒绝的deniedDebuggers与创建预编译头无关。他们不编译任何东西。您应该花一些时间学习如何调试自己的代码。了解如何使用调试器是每个C++开发者所需的技能。1。您没有初始化OpenGL 2。您没有渲染任何我看到的内容,没有旧的
glBegin/glEnd
也没有新的
glDraw
3。我看不到
glClear
或缓冲区的交换。。。有关新样式和旧样式,请参见。尝试将TGA复制到EXE所在的位置,并删除路径。。。您很可能有错误的路径语法
folder/folder/…
某些环境/函数需要
folder\\folder\\…
是否确实打开了文件?当您使用调试器单步执行程序时,当它据称加载并显示图像时,调试器显示了什么情况?它正在读取文件吗?@Sam Varshavchik谢谢你的回复,我忘记输入当我使用调试器时,它显示[Error]无法创建Sabah.exe的预编译头标志:权限被拒绝的deniedDebuggers与创建预编译头无关。他们不编译任何东西。您应该花一些时间学习如何调试自己的代码。了解如何使用调试器是每个C++开发者所需的技能。1。您没有初始化OpenGL 2。您没有渲染任何我看到的内容,没有旧的
glBegin/glEnd
也没有新的
glDraw
3。我看不到
glClear
或缓冲区的交换。。。有关新样式和旧样式,请参见。尝试将TGA复制到EXE所在的位置,并删除路径。。。您很可能有错误的路径语法
folder/folder/…
某些环境/功能需要
folder\\folder\\…
是否确实打开了文件?
#include "opencv2/opencv.hpp"
#include <iostream>

using namespace cv;
using namespace std;

int main(int argc, char** argv)
{
 // Read the image file
 Mat image = imread("C:/Users/Lenovo/Desktop/New folder/sabahpride.jpg");

 // Check for failure
 if (image.empty()) 
 {
  cout << "Could not open or find the image" << endl;
  cin.get(); //wait for any key press
  return -1;
 }

 String windowName = "The Flag"; //Name of the window

 namedWindow(windowName); // Create a window

 imshow(windowName, image); // Show our image inside the created window.

 waitKey(0); // Wait for any keystroke in the window

 destroyWindow(windowName); //destroy the created window

 return 0;
}