有哪位老兄做过PPM文件的读写?

Previewer 2005-09-12 10:32:12
我要做个PPM文件的解析,此PPM文件存储的时一个表格,要把单元格里的内容提取出来。
全是2进制的,没一点头绪,哪位老兄给些提示,必高分答谢!:)
...全文
3725 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
Previewer 2005-09-20
  • 打赏
  • 举报
回复
谢谢 十里平湖
qiansen 2005-09-19
  • 打赏
  • 举报
回复
建议你搜索一下linux下的图像处理包,大部分都有关于ppm格式的读写的代码的。
qiansen 2005-09-19
  • 打赏
  • 举报
回复
/* a simple image class
filename: image.h */

#ifndef IMAGE_H
#define IMAGE_H

#include <cstring>

template <class T>
class image {
public:
/* create an image */
image(const int width, const int height, const bool init = true);

/* delete an image */
~image();

/* init an image */
void init(const T &val);

/* copy an image */
image<T> *copy() const;

/* get the width of an image. */
int width() const { return w; }

/* get the height of an image. */
int height() const { return h; }

/* image data. */
T *data;

/* row pointers. */
T **access;

private:
int w, h;
};

/* use imRef to access image data. */
#define imRef(im, x, y) (im->access[y][x])

/* use imPtr to get pointer to image data. */
#define imPtr(im, x, y) &(im->access[y][x])

template <class T>
image<T>::image(const int width, const int height, const bool init) {
w = width;
h = height;
data = new T[w * h]; // allocate space for image data
access = new T*[h]; // allocate space for row pointers

// initialize row pointers
for (int i = 0; i < h; i++)
access[i] = data + (i * w);

if (init)
memset(data, 0, w * h * sizeof(T));
}

template <class T>
image<T>::~image() {
delete [] data;
delete [] access;
}

template <class T>
void image<T>::init(const T &val) {
T *ptr = imPtr(this, 0, 0);
T *end = imPtr(this, w-1, h-1);
while (ptr <= end)
*ptr++ = val;
}


template <class T>
image<T> *image<T>::copy() const {
image<T> *im = new image<T>(w, h, false);
memcpy(im->data, data, w * h * sizeof(T));
return im;
}

#endif
qiansen 2005-09-19
  • 打赏
  • 举报
回复
/*pnmfile.h */

#ifndef PNM_FILE_H
#define PNM_FILE_H

#include <cstdlib>
#include <climits>
#include <cstring>
#include <fstream>
#include "image.h"
#include "misc.h"
#include <iostream.h>//for debug,qiansen

#define BUF_SIZE 256

class pnm_error { };

static void read_packed(unsigned char *data, int size, std::ifstream &f) {
unsigned char c = 0;

int bitshift = -1;
for (int pos = 0; pos < size; pos++) {
if (bitshift == -1) {
c = f.get();
bitshift = 7;
}
data[pos] = (c >> bitshift) & 1;
bitshift--;
}
}

static void write_packed(unsigned char *data, int size, std::ofstream &f) {
unsigned char c = 0;

int bitshift = 7;
for (int pos = 0; pos < size; pos++) {
c = c | (data[pos] << bitshift);
bitshift--;
if ((bitshift == -1) || (pos == size-1)) {
f.put(c);
bitshift = 7;
c = 0;
}
}
}

/* read PNM field, skipping comments */
static void pnm_read(std::ifstream &file, char *buf) {
char doc[BUF_SIZE];
char c;

file >> c;
while (c == '#') {
file.getline(doc, BUF_SIZE);
file >> c;
}
file.putback(c);

file.width(BUF_SIZE);
file >> buf;
file.ignore();
}

static image<uchar> *loadPBM(const char *name) {
char buf[BUF_SIZE];

/* read header */
std::ifstream file(name, std::ios::in | std::ios::binary);
pnm_read(file, buf);
if (strncmp(buf, "P4", 2))
throw pnm_error();

pnm_read(file, buf);
int width = atoi(buf);
pnm_read(file, buf);
int height = atoi(buf);

/* read data */
image<uchar> *im = new image<uchar>(width, height);
for (int i = 0; i < height; i++)
read_packed(imPtr(im, 0, i), width, file);

return im;
}

static void savePBM(image<uchar> *im, const char *name) {
int width = im->width();
int height = im->height();
std::ofstream file(name, std::ios::out | std::ios::binary);

file << "P4\n" << width << " " << height << "\n";
for (int i = 0; i < height; i++)
write_packed(imPtr(im, 0, i), width, file);
}

static image<uchar> *loadPGM(const char *name) {
char buf[BUF_SIZE];

/* read header */
std::ifstream file(name, std::ios::in | std::ios::binary);
pnm_read(file, buf);
if (strncmp(buf, "P5", 2))
throw pnm_error();

pnm_read(file, buf);
int width = atoi(buf);
pnm_read(file, buf);
int height = atoi(buf);

pnm_read(file, buf);
if (atoi(buf) > UCHAR_MAX)
throw pnm_error();

/* read data */
image<uchar> *im = new image<uchar>(width, height);
file.read((char *)imPtr(im, 0, 0), width * height * sizeof(uchar));

return im;
}

static void savePGM(image<uchar> *im, const char *name) {
int width = im->width();
int height = im->height();
std::ofstream file(name, std::ios::out | std::ios::binary);

file << "P5\n" << width << " " << height << "\n" << UCHAR_MAX << "\n";
file.write((char *)imPtr(im, 0, 0), width * height * sizeof(uchar));
}

static image<rgb> *loadPPM(const char *name) {
char buf[BUF_SIZE], doc[BUF_SIZE];

/* read header */
std::ifstream file(name, std::ios::in | std::ios::binary);
pnm_read(file, buf);
if (strncmp(buf, "P5", 2)){
//throw pnm_error();
cout<<"pnm version is P6,may be not supported."<<endl;
}
pnm_read(file, buf);
int width = atoi(buf);
pnm_read(file, buf);
int height = atoi(buf);

pnm_read(file, buf);
if (atoi(buf) > UCHAR_MAX)
throw pnm_error();

/* read data */
image<rgb> *im = new image<rgb>(width, height);
file.read((char *)imPtr(im, 0, 0), width * height * sizeof(rgb));

return im;
}

static void savePPM(image<rgb> *im, const char *name) {
int width = im->width();
int height = im->height();
std::ofstream file(name, std::ios::out | std::ios::binary);

file << "P6\n" << width << " " << height << "\n" << UCHAR_MAX << "\n";
file.write((char *)imPtr(im, 0, 0), width * height * sizeof(rgb));
}

template <class T>
void load_image(image<T> **im, const char *name) {
char buf[BUF_SIZE];

/* read header */
std::ifstream file(name, std::ios::in | std::ios::binary);
pnm_read(file, buf);
if (strncmp(buf, "VLIB", 9))
throw pnm_error();

pnm_read(file, buf);
int width = atoi(buf);
pnm_read(file, buf);
int height = atoi(buf);

/* read data */
*im = new image<T>(width, height);
file.read((char *)imPtr((*im), 0, 0), width * height * sizeof(T));
}

template <class T>
void save_image(image<T> *im, const char *name) {
int width = im->width();
int height = im->height();
std::ofstream file(name, std::ios::out | std::ios::binary);

file << "VLIB\n" << width << " " << height << "\n";
file.write((char *)imPtr(im, 0, 0), width * height * sizeof(T));
}

#endif
Previewer 2005-09-12
  • 打赏
  • 举报
回复
没做过图像的,PPM文件没有一种标准格式码?可以自己定义?
djfu 2005-09-12
  • 打赏
  • 举报
回复
这个你必须了解原始PPM 文件是怎么序列化的以及它的数据结构存储方式,否则真是半点头绪都没有。
Previewer 2005-09-12
  • 打赏
  • 举报
回复
没有人可以给些提示吗?
内容概要:本文围绕水陆两栖无人机的任务规划与执行问题,提出了一种基于Matlab实现的智能路径规划解决方案,重点融合粒子群优化算法(PSO)与遗传算法(GA)进行三维环境下的避障路径规划。研究系统性地构建了从复杂地形建模、任务需求分析到算法设计与仿真实验的完整流程,实现了在多约束、动态障碍物等复杂环境下无人机的高效任务调度。通过对GA与PSO两种智能优化算法在路径长度、收敛速度、路径平滑度及稳定性等方面的对比分析,深入探讨了各自在无人机路径规划中的适用场景与性能差异,验证了所提方法在提升任务执行效率与安全性方面的有效性。该方案不仅适用于水陆交互通用场景,也为多模态无人系统自主导航提供了可扩展的技术框架。; 适合人群:具备一定Matlab编程能力和算法基础,从事无人机路径规划、智能优化算法研究、自动化控制或相关领域科研工作的研究生、科研人员及工程技术人员。; 使用场景及目标:①应用于水陆两栖无人机在复杂自然环境(如江河湖海与陆地交错区域)中的自主巡航与任务执行;②比较遗传算法与粒子群算法在三维动态路径规划中的优化性能,指导实际工程中算法选型;③为多约束条件下无人系统的自主决策与实时避障提供算法支持与仿真验证平台。; 阅读建议:建议读者结合提供的Matlab代码进行动手实践,重点关注两种算法的数学建模过程、适应度函数设计、参数调优策略及路径生成逻辑,通过仿真实验直观对比算法性能差异,深入理解智能优化算法在复杂路径规划问题中的应用机制与优化潜力。

19,464

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 图形处理/算法
社区管理员
  • 图形处理/算法社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧