如何用C++提取图片的像素坐标和RGB值,跪求大神!!!

jwh039 2013-10-07 06:36:12
想把一张图片的每个像素的坐标以及RGB值提取出来,有没有大神愿意抽点时间帮我搞下啊?实在是不会。
是每一个像素,最好存到文本文件中吧。格式是:a,b,c,d,e;其中a和b代表像素X和Y坐标,其余分别代表R,G,B值。每组用分号隔开。
谢谢了!!真的是急用啊!!!
...全文
8207 32 打赏 收藏 转发到动态 举报
写回复
用AI写文章
32 条回复
切换为时间正序
请发表友善的回复…
发表回复
tonny_Code 2013-10-09
  • 打赏
  • 举报
回复
// Output image gray value IplImage *img = cvLoadImage("C:\\xxx.tif", CV_LOAD_IMAGE_COLOR); CvScalar p; ofstream outfile1("C:\\b.txt"); outfile1 << "图像宽和高:" << width << "*" << height << endl; outfile1 << "图像B值" << endl;//R,G,B Value for(unsigned int i=0; i<width; i++) { for(unsigned int j=0; j<height; j++) { p = cvGet2D(&img, i, j);//(j,i) outfile1 << p.val[0] << " "; } outfile1 << endl; }
attilax 2013-10-09
  • 打赏
  • 举报
回复
我是用QT C++ 做过这样的图片处理,源码可发你。。追求开发速度的可用php,也可以以做这样的事。
SKATE11 2013-10-08
  • 打赏
  • 举报
回复
这个不同文件格式图片会不同吧
赵4老师 2013-10-08
  • 打赏
  • 举报
回复
推荐使用WinHex软件查看硬盘或文件或内存中的原始字节内容。
stereoMatching 2013-10-08
  • 打赏
  • 举报
回复

#include <fstream>

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>

void write_pixel_to_txt(std::string const &name, ofstream &stream)
{
    cv::Mat const image = cv::imread(name);
    if(image.empty()){
        std::cerr<<"input is empty"<<std::endl;
        return;
    }

    for(int row = 0; row != image.rows; ++row){
      auto ptr = image.ptr<uchar>(row);
      for(int col = 0; col != image.cols; ++col){
        //the channels of openCV is BGR but not RGB        
        stream<<row<<", "<<col<<", "<<static_cast<int>(ptr[2])<<", "<<
        static_cast<int>(ptr[1])<<", "<<static_cast<int>(ptr[0])<<std::endl;
        ptr += 3;
      }
    }
}

int main()
{
  ofstream stream("church00.txt");
  write_pixel_to_txt("church00.png", stream);

  return 0;
}
忘了一些细节,补充
ryfdizuo 2013-10-08
  • 打赏
  • 举报
回复
bmp格式没有压缩,读取最简单 直接以二进制文件形式读取。png是无损lzw压缩,jpeg是有损小波压缩,这些格式文件最好找开源库,自己解析太累。
xiaohuh421 2013-10-08
  • 打赏
  • 举报
回复
你是windows编程, 还是什么呢? 如果windows编程,就相对简单些. 有Api GetPixel可用. 还可以创建设备无关的内存位图(CreateDIBSection), 然后直接访问其数据. 如果是其它环境, 可以先了解下图片文件保存格式, 然后读取文件,并解析即可.
Inhibitory 2013-10-08
  • 打赏
  • 举报
回复
可以学学 Qt,用 QRgb QImage::pixel(int x, int y) const 就能很容易得到每个像素的RGB 颜色值。
jwh039 2013-10-08
  • 打赏
  • 举报
回复
引用 29 楼 ananluowei 的回复:
[quote=引用 28 楼 jwh039 的回复:] [quote=引用 27 楼 ananluowei 的回复:] [quote=引用 25 楼 jwh039 的回复:] [quote=引用 24 楼 ananluowei 的回复:] [quote=引用 22 楼 jwh039 的回复:] 谢谢!!请问可以直接用VC编译吗?
vs2010建立c++控制台项目,可以编译。 自己把相关的图片文件名以及生成的txt文件名改下。这应该会吧。[/quote] 不行哦。VC编译有错误,而且我这边同学也没有装VS的。能不能请你抽出点时间帮我弄一下啊,真的,我现在非常急。跪谢!![/quote] 没编译器我也帮不了。[/quote] 我把照片发给你,然后你把txt发给我行吗?我真的不想做伸手党,主要是我现在下的话来不及丫。[/quote] 试试看吧,我的邮箱 ananluowei@sina.com[/quote] 好的,谢谢!!
大尾巴猫 2013-10-08
  • 打赏
  • 举报
回复
引用 28 楼 jwh039 的回复:
[quote=引用 27 楼 ananluowei 的回复:] [quote=引用 25 楼 jwh039 的回复:] [quote=引用 24 楼 ananluowei 的回复:] [quote=引用 22 楼 jwh039 的回复:] 谢谢!!请问可以直接用VC编译吗?
vs2010建立c++控制台项目,可以编译。 自己把相关的图片文件名以及生成的txt文件名改下。这应该会吧。[/quote] 不行哦。VC编译有错误,而且我这边同学也没有装VS的。能不能请你抽出点时间帮我弄一下啊,真的,我现在非常急。跪谢!![/quote] 没编译器我也帮不了。[/quote] 我把照片发给你,然后你把txt发给我行吗?我真的不想做伸手党,主要是我现在下的话来不及丫。[/quote] 试试看吧,我的邮箱 ananluowei@sina.com
jwh039 2013-10-08
  • 打赏
  • 举报
回复
引用 27 楼 ananluowei 的回复:
[quote=引用 25 楼 jwh039 的回复:] [quote=引用 24 楼 ananluowei 的回复:] [quote=引用 22 楼 jwh039 的回复:] 谢谢!!请问可以直接用VC编译吗?
vs2010建立c++控制台项目,可以编译。 自己把相关的图片文件名以及生成的txt文件名改下。这应该会吧。[/quote] 不行哦。VC编译有错误,而且我这边同学也没有装VS的。能不能请你抽出点时间帮我弄一下啊,真的,我现在非常急。跪谢!![/quote] 没编译器我也帮不了。[/quote] 我把照片发给你,然后你把txt发给我行吗?我真的不想做伸手党,主要是我现在下的话来不及丫。
大尾巴猫 2013-10-08
  • 打赏
  • 举报
回复
引用 25 楼 jwh039 的回复:
[quote=引用 24 楼 ananluowei 的回复:] [quote=引用 22 楼 jwh039 的回复:] 谢谢!!请问可以直接用VC编译吗?
vs2010建立c++控制台项目,可以编译。 自己把相关的图片文件名以及生成的txt文件名改下。这应该会吧。[/quote] 不行哦。VC编译有错误,而且我这边同学也没有装VS的。能不能请你抽出点时间帮我弄一下啊,真的,我现在非常急。跪谢!![/quote] 没编译器我也帮不了。
赵4老师 2013-10-08
  • 打赏
  • 举报
回复
引用 15 楼 ananluowei 的回复:
gdi+的Bitmap对象就可以做到了,能读取多种文件格式。
#include <iostream>
#include <fstream>
#include <string>
#include <windows.h>
#include <gdiplus.h>
#pragma comment(lib, "gdiplus.lib")

using namespace std;
using namespace Gdiplus;


int main()
{
	GdiplusStartupInput gdiplusstartupinput;
	ULONG_PTR gdiplustoken;
	GdiplusStartup(&gdiplustoken, &gdiplusstartupinput, nullptr);
	
	wstring infilename(L"1.jpg");
	string outfilename("color.txt");
	//读图片
	Bitmap* bmp = new Bitmap(infilename.c_str());
	UINT height = bmp->GetHeight();
	UINT width = bmp->GetWidth();
	cout << "width " << width << ", height " << height << endl;
	
	Color color;
	ofstream fout(outfilename.c_str());

	for (int y = 0; y < height; y++)
		for (int x = 0; x < width; x++)
		{
			bmp->GetPixel(x, y, &color);
			fout << x << ";" << y << ";"
				 << (int)color.GetRed() << ";"
				 << (int)color.GetGreen() << ";"
				 << (int)color.GetBlue() << endl;
		}
	
	fout.close();
	
	delete bmp;
	GdiplusShutdown(gdiplustoken);
	return 0;
}
学习了。
jwh039 2013-10-08
  • 打赏
  • 举报
回复
引用 24 楼 ananluowei 的回复:
[quote=引用 22 楼 jwh039 的回复:] 谢谢!!请问可以直接用VC编译吗?
vs2010建立c++控制台项目,可以编译。 自己把相关的图片文件名以及生成的txt文件名改下。这应该会吧。[/quote] 不行哦。VC编译有错误,而且我这边同学也没有装VS的。能不能请你抽出点时间帮我弄一下啊,真的,我现在非常急。跪谢!!
大尾巴猫 2013-10-08
  • 打赏
  • 举报
回复
引用 22 楼 jwh039 的回复:
谢谢!!请问可以直接用VC编译吗?
vs2010建立c++控制台项目,可以编译。 自己把相关的图片文件名以及生成的txt文件名改下。这应该会吧。
jwh039 2013-10-08
  • 打赏
  • 举报
回复
引用 21 楼 stereoMatching 的回复:
[quote=引用 13 楼 jwh039 的回复:] [quote=引用 3 楼 stereoMatching 的回复:] 安裝openCV2即可,以下是pseudo codes(随手写一写,没测过)

#include <fstream>

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>

void write_pixel_to_txt(std::string const &name, ofstream &stream)
{
    cv::Mat const image = cv::imread(name);
    if(image.empty()){
        std::cerr<<"input is empty"<<std::endl;
        return;
    }

    for(int row = 0; row != image.rows; ++row){
      auto ptr = image.ptr<uchar>(row);
      for(int col = 0; col != image.cols; ++col){
        //the channels of openCV is BGR but not RGB
        stream<<row<<", "<<col<<", "<<ptr[2]<<", "<<ptr[1]<<", "<<ptr[0]<<std::endl;
      }
    }
}

int main()
{
  ofstream stream("church00.txt");
  write_pixel_to_txt("church00.png", stream);

  return 0;
}
谢谢!!我没学过C++,现在初学java,请问能不能代我搞一下这个啊?[/quote] openCV2有提供java的wrapper,java自己就有提供读写图片的libs了 自己上网动手查一查该怎么做,这应该不难 如果你现在的程度只能当伸手小超人的话 1 : 如果是学校作业,你可以花好好的学习,大不了被当 下学期再重修就好,重要的是有学到东西 2 : 如果是工作,这代表你应该转行或多花几年的时间练功[/quote] 谢谢!!我才刚开始学习java。
jwh039 2013-10-08
  • 打赏
  • 举报
回复
引用 20 楼 ananluowei 的回复:
[quote=引用 17 楼 jwh039 的回复:] [quote=引用 15 楼 ananluowei 的回复:] gdi+的Bitmap对象就可以做到了,能读取多种文件格式。
#include <iostream>
#include <fstream>
#include <string>
#include <windows.h>
#include <gdiplus.h>
#pragma comment(lib, "gdiplus.lib")

using namespace std;
using namespace Gdiplus;


int main()
{
	GdiplusStartupInput gdiplusstartupinput;
	ULONG_PTR gdiplustoken;
	GdiplusStartup(&gdiplustoken, &gdiplusstartupinput, nullptr);
	
	wstring infilename(L"1.jpg");
	string outfilename("color.txt");
	//读图片
	Bitmap* bmp = new Bitmap(infilename.c_str());
	UINT height = bmp->GetHeight();
	UINT width = bmp->GetWidth();
	cout << "width " << width << ", height " << height << endl;
	
	Color color;
	ofstream fout(outfilename.c_str());

	for (int y = 0; y < height; y++)
		for (int x = 0; x < width; x++)
		{
			bmp->GetPixel(x, y, &color);
			fout << x << ";" << y << ";"
				 << (int)color.GetRed() << ";"
				 << (int)color.GetGreen() << ";"
				 << (int)color.GetBlue() << endl;
		}
	
	fout.close();
	
	delete bmp;
	GdiplusShutdown(gdiplustoken);
	return 0;
}
大神!!帮我搞一下行吗?我现在真的十万火急啊。。百度谷歌都没办法。。[/quote] 不是按照你的要求做好了吗? 读取1个jpg图片,生成1个txt文件,每行是x,y,r,g,b[/quote]
引用 20 楼 ananluowei 的回复:
[quote=引用 17 楼 jwh039 的回复:] [quote=引用 15 楼 ananluowei 的回复:] gdi+的Bitmap对象就可以做到了,能读取多种文件格式。
#include <iostream>
#include <fstream>
#include <string>
#include <windows.h>
#include <gdiplus.h>
#pragma comment(lib, "gdiplus.lib")

using namespace std;
using namespace Gdiplus;


int main()
{
	GdiplusStartupInput gdiplusstartupinput;
	ULONG_PTR gdiplustoken;
	GdiplusStartup(&gdiplustoken, &gdiplusstartupinput, nullptr);
	
	wstring infilename(L"1.jpg");
	string outfilename("color.txt");
	//读图片
	Bitmap* bmp = new Bitmap(infilename.c_str());
	UINT height = bmp->GetHeight();
	UINT width = bmp->GetWidth();
	cout << "width " << width << ", height " << height << endl;
	
	Color color;
	ofstream fout(outfilename.c_str());

	for (int y = 0; y < height; y++)
		for (int x = 0; x < width; x++)
		{
			bmp->GetPixel(x, y, &color);
			fout << x << ";" << y << ";"
				 << (int)color.GetRed() << ";"
				 << (int)color.GetGreen() << ";"
				 << (int)color.GetBlue() << endl;
		}
	
	fout.close();
	
	delete bmp;
	GdiplusShutdown(gdiplustoken);
	return 0;
}
大神!!帮我搞一下行吗?我现在真的十万火急啊。。百度谷歌都没办法。。[/quote] 不是按照你的要求做好了吗? 读取1个jpg图片,生成1个txt文件,每行是x,y,r,g,b[/quote] 谢谢!!请问可以直接用VC编译吗?
stereoMatching 2013-10-08
  • 打赏
  • 举报
回复
引用 13 楼 jwh039 的回复:
[quote=引用 3 楼 stereoMatching 的回复:] 安裝openCV2即可,以下是pseudo codes(随手写一写,没测过)

#include <fstream>

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>

void write_pixel_to_txt(std::string const &name, ofstream &stream)
{
    cv::Mat const image = cv::imread(name);
    if(image.empty()){
        std::cerr<<"input is empty"<<std::endl;
        return;
    }

    for(int row = 0; row != image.rows; ++row){
      auto ptr = image.ptr<uchar>(row);
      for(int col = 0; col != image.cols; ++col){
        //the channels of openCV is BGR but not RGB
        stream<<row<<", "<<col<<", "<<ptr[2]<<", "<<ptr[1]<<", "<<ptr[0]<<std::endl;
      }
    }
}

int main()
{
  ofstream stream("church00.txt");
  write_pixel_to_txt("church00.png", stream);

  return 0;
}
谢谢!!我没学过C++,现在初学java,请问能不能代我搞一下这个啊?[/quote] openCV2有提供java的wrapper,java自己就有提供读写图片的libs了 自己上网动手查一查该怎么做,这应该不难 如果你现在的程度只能当伸手小超人的话 1 : 如果是学校作业,你可以花好好的学习,大不了被当 下学期再重修就好,重要的是有学到东西 2 : 如果是工作,这代表你应该转行或多花几年的时间练功
大尾巴猫 2013-10-08
  • 打赏
  • 举报
回复
引用 17 楼 jwh039 的回复:
[quote=引用 15 楼 ananluowei 的回复:] gdi+的Bitmap对象就可以做到了,能读取多种文件格式。
#include <iostream>
#include <fstream>
#include <string>
#include <windows.h>
#include <gdiplus.h>
#pragma comment(lib, "gdiplus.lib")

using namespace std;
using namespace Gdiplus;


int main()
{
	GdiplusStartupInput gdiplusstartupinput;
	ULONG_PTR gdiplustoken;
	GdiplusStartup(&gdiplustoken, &gdiplusstartupinput, nullptr);
	
	wstring infilename(L"1.jpg");
	string outfilename("color.txt");
	//读图片
	Bitmap* bmp = new Bitmap(infilename.c_str());
	UINT height = bmp->GetHeight();
	UINT width = bmp->GetWidth();
	cout << "width " << width << ", height " << height << endl;
	
	Color color;
	ofstream fout(outfilename.c_str());

	for (int y = 0; y < height; y++)
		for (int x = 0; x < width; x++)
		{
			bmp->GetPixel(x, y, &color);
			fout << x << ";" << y << ";"
				 << (int)color.GetRed() << ";"
				 << (int)color.GetGreen() << ";"
				 << (int)color.GetBlue() << endl;
		}
	
	fout.close();
	
	delete bmp;
	GdiplusShutdown(gdiplustoken);
	return 0;
}
大神!!帮我搞一下行吗?我现在真的十万火急啊。。百度谷歌都没办法。。[/quote] 不是按照你的要求做好了吗? 读取1个jpg图片,生成1个txt文件,每行是x,y,r,g,b
jwh039 2013-10-08
  • 打赏
  • 举报
回复
引用 18 楼 u012368316 的回复:
jpeg格式不是有专门的lib嘛,这些东西一般都有现成的解析工具,哪里需要你自己来写呢,啥子看源文件格式的就更扯了,要是那么麻烦,游戏引擎还有啥用,不要重复造轮子
那应该怎么弄啊?请指点。。
加载更多回复(12)

64,281

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

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