19,469
社区成员
发帖
与我相关
我的任务
分享
#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;
}
#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;
}