繁体   English   中英

打开jpeg或png图像作为c或c ++中的像素数据

[英]opening a jpeg or png image as pixel data in c or c++

这是我的第一篇文章,所以请让我知道我是否清楚地回答了这个问题。

我正在尝试在dev c ++中打开一个(黑白)jpeg或png图像文件作为数组,以便我可以隔离白色像素的位置。 我了解的指针和循环的隔离。 但是,打开图像文件没有用,并且无法找到我可以使用的示例。 除了给定的#include“ cstdlib” #include“ iostream” #include“ stdio.h”

我没有其他头文件。 我知道fopen可以打开文件,但是我的尝试都没有显示图像数据。

感谢您的任何帮助

这不是我的密码。 这是BIV1991提供给我的示例,我正在发布,因为找不到gdiplus.h的标头。 建议使用标题unknwn,windows,objidl来解决该问题

#include <cstdlib>
#include <iostream>
#include <Unknwn.h>
#include <windows.h>
#include <objidl.h>
#include <gdiplus.h>
using namespace Gdiplus;
#pragma comment(lib,"gdiplus.lib") 
int main(int argc, char *argv[])
{
   // initialize gdiplus
Gdiplus::GdiplusStartupInput si;
ULONG_PTR token;
Gdiplus::GdiplusStartup(&token,&si,0);

// load image as bitmap, any formats supported
Gdiplus::Bitmap image(L"C:\\image.jpg",false);
int w = image.GetWidth();
// image.LockBits - to read pixels 
    system("PAUSE");
    return EXIT_SUCCESS;
}

如果您为Windows编程,则可以使用GDIPlus读取Jpeg和其他图像类型并对其进行操作,例如绘制或获取它们的像素。 保证100%。 我做了很多次。

内容包括

#include <gdiplus.h>
#pragma comment(lib,"gdiplus.lib")

代码做什么:

// initialize gdiplus
Gdiplus::GdiplusStartupInput si;
ULONG_PTR token;
Gdiplus::GdiplusStartup(&token,&si,0);

// load image, any formats supported
Gdiplus::Bitmap image(L"C:\\image.jpg",false);
int w = image.GetWidth();
// image.LockBits - to read pixels

首先,没有标准的C ++库来处理图像。 其次,jpeg和png图像格式是压缩图像格式,需要某种复杂的解压缩算法。 因此,开始使用图像有两种方法:

  1. 使用简单的图像格式,如bmp。 bmp只是带有一些光标题的像素缓冲区,描述了该像素缓冲区的格式。 您可以自己编写一个加载bmp的程序,而这个程序要自己花费一半(而要花大约一个小时来了解格式本身)。 有关bmp的更多信息: http : //en.wikipedia.org/wiki/BMP_file_format

  2. 使用EXTERNAL库。 也就是说,您的语言或编译器包中没有该库。 您有时需要下载甚至安装它。 通常,要使用外部库,您需要做以下五个步骤:1)在Internet中找到图像处理库。 2)将xxx.lib文件放在您的项目文件夹中。 3)告诉您的链接器您要使用xxx.lib。4)在您的代码中添加#include“ xxx.h”。 5)使用库API加载图像。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM