opencv将图像像素保存为txt文档

qq_30115427 2015-10-13 10:43:26
运行出不来结果直接return0
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include<opencv2/opencv.hpp>


using namespace cv;

int main ( int argc , char** argv )
{
FILE* fp;

IplImage* img;

int width;
int height;
int step;
int channels;

int i,j,k,counter=0,row=0;

uchar* data;

CvMat* mat = cvCreateMat(480,512,CV_8UC1);

CvFileStorage* fs = cvOpenFileStorage("example.xml",0,CV_STORAGE_WRITE);

//Load image

if ( argc==3 && (img = cvLoadImage ( argv[1],0))!=0)
{
printf ("Load image successfully!\n");
//getchar();


//Processing the image data

width =img -> width;
height = img -> height;
step = img -> widthStep;
printf("%d\n",step);
channels = img -> nChannels;
printf("%d\n",channels);
data = (uchar*)img -> imageData;

printf ("Processing a size of %d X %d and step %d with %d channels image.\n", width, height , step, channels);

//getchar();

fp=fopen("img.txt","w");

for ( i=0; i<height; i++)
{
fprintf (fp,"%d\t",row );
for ( j=0; j<width; j++)
{
for (k=0; k<channels; k++)
{
fprintf (fp,"%d\t", data[i*step+j*channels+k]);
counter++;
}
//fprintf(fp,"\t");
}
row++;
fprintf (fp,"\n");
}

fprintf(fp,"%d\n",counter);

fclose(fp);

for ( i=0; i<height; i++)
{

for ( j=0; j<width; j++)
{
for (k=0; k<channels; k++)
{
((uchar*)(mat->data.ptr + mat->step*i))[j]=data[i*step+j*channels+k];

}

}


}


cvWrite(fs,"A",mat,cvAttrList(0,0));

cvSaveImage(argv[2],mat);

cvReleaseFileStorage(&fs);

cvReleaseMat(&mat);


//Show the image

cvNamedWindow ( "image" , 1 );

cvShowImage ( "image" , img );

cvWaitKey ( 0 );

cvDestroyWindow ( "image" );

cvReleaseImage ( & img );
}
return 0;

}



程序“[4116] save_img_txt.exe”已退出,返回值为 0 (0x0)。
...全文
550 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq_30115427 2015-10-17
  • 打赏
  • 举报
回复
引用 2 楼 zhao4zhong1 的回复:
仅供参考:
#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, NULL);

    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 (UINT y = 0; y < height; y++)
    for (UINT 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;
}
如果是深度数据应该怎么改,用kinect
qq_30115427 2015-10-17
  • 打赏
  • 举报
回复
谢谢赵老师!!!!!!!
shiter 2015-10-15
  • 打赏
  • 举报
回复
顶楼上,直接用赵老师代码,多简介
赵4老师 2015-10-14
  • 打赏
  • 举报
回复
仅供参考:
#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, NULL);

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 (UINT y = 0; y < height; y++)
for (UINT 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;
}
笨笨仔 2015-10-14
  • 打赏
  • 举报
回复
你自己设断点看一下判断条件不就行了?
像素数据中包含大量的非显示字符,如果你要以TXT形式打开并保存,请先将它们转换成可显示的ASCII字符。

19,469

社区成员

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

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