java读取ppm图片_用C读取PPM文件(只有P6格式没有注释)并另存为图像
我有一个练习,我需要读取PPM文件并将其内容保存为C中的图像 . 然后我想计算平均亮度 . 我有3个头文件 . 我设法读取文件并检查P6值,宽度,高度和深度 . 如何从文件中提取图像数据以制作图像对象以计算平均亮度?
Color.h
namespace imaging
{
/*! An alias for the floating point representation of color components (32bit per color channel).
*/
typedef float component_t;
class Color
{
public:
// members
component_t r,
g,
b;
// member functions
component_t & operator [] (size_t index)
{
return *(&r + index);
}
Color operator + (Color & right)
{
Color left;
left.r = r + right.r;
left.g = g + right.g;
left.b = b + right.b;
return left;
}
// constructors
Color(component_t r, component_t g, component_t b) : r(r), g(g), b(b) {}
Color() : r(0), g(0), b(0) {}
};
image.h的
namespace imaging {
class Image
{
public:
enum channel_t {RED=0,GREEN, BLUE};
protected:
component_t * buffer;
unsigned int width,
height;
public:
// metric accessors
const unsigned int getWidth() const {return width;}
const unsigned int getHeight() const {return height;}
// data accessors
component_t * getRawDataPtr();
Color getPixel(unsigned int x, unsigned int y) const;
// data mutators
void setPixel(unsigned int x, unsigned int y, Color & value);
void setData(const component_t * & data_ptr);
void resize(unsigned int new_width, unsigned int new_height);
// constructors and destructor
/*! Default constructor.
*/
Image();
/*! Constructor with width and height specification.
*
*/
Image(unsigned int width, unsigned int height);
// Constructor with data initialization
Image(unsigned int width, unsigned int height, const component_t * data_ptr);
/*! Copy constructor.
*/
Image(const Image &src);
/*! The Image destructor.
*/
~Image();
/*! Copy assignment operator.*/
Image & operator = (const Image & right);
};
}
ppm_format.h
namespace imaging
{
/*! Reads a PPM image file and returns a pointer to a newly allocated Image object containing the image.
*/
Image * ReadPPM(const char * filename);
} //namespace imaging
我为读取PPM文件方法编写了这段代码:
Image * ReadPPM(const char* filename) {
ifstream ifs;
string v1; //for the P6 value
int v2,v3,v4; //for width, height and color depth
cin.getline(filename, 20);
ifs.open(filename,ios::binary);
if (ifs.is_open()) {
while (ifs.good()){
if (ifs>>v1>>v2>>v3>>v4){
int height = v2;
int width = v3;
if (v1!="P6") {
cout<
ifs.close();
exit(0);
}
if (v2<1){
cout<
ifs.close();
exit(0);
}
if (v3<1){
cout<
ifs.close();
exit(0);
}
if (v4<1 || v4>255){
cout<
ifs.close();
exit(0);
}
//cout<
}
}
}
else if (!ifs.is_open()) {
cout<
exit(EXIT_FAILURE);
}
ifs.close();
// end of reading the file
//start making the image
Image image = new Image(v2,v3);
return image;
}
我写了这段代码来读取和保存图像数据 . 问题是我想将图像数据提取为component_t类型(浮点数) .
char* data = new char[3*height*width]; int bytesPerLn = width*3; for (int i=0;i
java读取ppm图片_用C读取PPM文件(只有P6格式没有注释)并另存为图像相关推荐
- python读取bmp图片_用Python读取bmp文件
我意识到这是一个老问题,但我自己解决这个问题时发现了这个问题,我想这可能会在将来帮助别人. 实际上很容易将BMP文件读取为二进制数据.当然,这取决于你需要支持的范围有多广,需要支持的角落案例有多少. ...
- python 读取excel图片_如何用Python读取Excel中图片?
公众号: 早起Python 作者:刘早起 大家好,在使用Python进行办公自动化操作时,一定少不了与Excel表格的交互,我们通常是用pandas处理表格数据,但大多数情况下,都是读取表格中的数值进 ...
- java编程石头剪刀布图片_石头、剪刀、布!10分钟带你打开深度学习大门,代码已开源...
原标题:石头.剪刀.布!10分钟带你打开深度学习大门,代码已开源 沉沉 发自 宇宙中心 量子位 出品 | 公众号 QbitAI 深度学习技术的不断普及,越来越多的语言可以用来进行深度学习项目的开发,即 ...
- 原声php 读取excel乱码_使用PHPExcel读取Excel文件时会读出乱码
这是我读取文件的代码: require_once dirname(__FILE__) . "/PHPExcel/Classes/PHPExcel.php";$PHPReader = ...
- java中复制图片_如何在Java中实现复制图片
packagecopyImage;importjava.io.BufferedReader;importjava.io.File;importjava.io.FileInputStream;impor ...
- python读取word图片_Python中如何读取Word中的图片
Python能够快速的编写.调试,用来提取各类软件中的图片再好不过了.今天小编就为大家带来在Python中提取Word图片的方法. 方法 需要批量的修改文件后缀名,并且解压之后将图片拷贝到需要存放的地 ...
- python读取大智慧数据_大智慧数据读取接口 大智慧股票本地数据读取接口
大智慧数据读取接口 大智慧股票本地数据读取接口 时间:2020-7-28 1:42:29 点击: 核心提示:所属分类:C资源描述:组件方法和属性说明:(1)方法GetData语法:public str ...
- java word 转换 图片_怎样把手机上的图片转换成word?
在当今这个快速发展的时代,人手一部手机是件很正常的事,小编的手机上往往会存有许多喜欢的文字图片,可是那么多的图片又会占用很多手机的空间,这样就有些不好了.庆幸的是最近小编学习到一个可以将手机上的图片转 ...
- java 企业号 临时素材_查看“获取临时素材文件”的源代码
因为以下原因,你没有权限编辑本页: 您刚才请求的操作只对以下1个用户组开放:用户. 您可以查看并复制此页面的源代码: 通过media_id获取图片.语音.视频等文件,协议和普通的http文件下载完全相 ...
最新文章
- 创业者如何小步快跑,从0开始实现自己的产品?
- 16年寒假随笔(1)
- [NOI2008]志愿者招募
- msf payload php,Metasploit(四)--Msfpayload命令
- 【 HRBUST - 1055】Single(模拟,dp,打表)(总结)
- C语言 socket 编程学习
- 牛客假日团队赛1 D.Promotion Counting
- Grad-CAM: Visual Explanations from Deep Networks via Gradient-based Localization
- 一种使用SOC精确控制脉冲的方法
- 【Bugs】Hbase:File system needs to be upgraded. You have version null and I want version(habse缓冲问题)
- 写文章没高质量配图?python爬虫绕过限制一键搜索下载图虫创意图片!
- 心肺复苏,“星火急救”推广三年,也是深圳民生微实事项目
- 计算机控制室防火危险级为,8.2 空气调节 - 【已作废】火力发电厂与变电站设计防火规范 GB50229-2006 - 消防规范大全 - 消防资源网!...
- Opencv学习笔记(二十六)文本检测mser+非极大值抑制nms
- JS for循环实现 My cats are called Bill, Jeff, Pete, Biggles, and Jasmin.
- 三菱fx3u型PLC和昆仑通泰MCGS触摸屏控制松下伺服电机控制程序
- Python Numpy鸢尾花实训,数据处理
- 1.1计算机简介、Python语言简介
- Mac磁盘格式化与系统重装
- R6025 ---Pure virtual functmm call的错误之一