算是保存个副本吧:
#include "cv.h" #include "highgui.h" #include <fstream> #include <iostream> using namespace std; int main() { IplImage* img = cvLoadImage("F:\\易康小分类图1_seg.bmp", 1); /* always check */ assert(img); int nWidth = img->width; int nHeight = img->height; int step = img->widthStep; int channels = img->nChannels; int i,j; int sum1 = 0,sum2 = 0,sum3 = 0,sum4 = 0,sum5 = 0; for( i=0;i<nHeight;i++) for( j=0;j<nWidth;j++) { CvScalar s = cvGet2D(img,i,j); if((int)s.val[0] == 230 && (int)s.val[1] == 80 && (int)s.val[2] == 140) ++sum1; else if((int)s.val[0] == 140 && (int)s.val[1] == 80 && (int)s.val[2] == 30) ++sum2; else if((int)s.val[0] == 20 && (int)s.val[1] == 0 && (int)s.val[2] == 230) ++sum3; else if((int)s.val[0] == 30 && (int)s.val[1] == 140 && (int)s.val[2] == 90) ++sum4; else if((int)s.val[0] == 90 && (int)s.val[1] == 10 && (int)s.val[2] == 80) ++sum5; } //ofstream infile("E:\\infile.txt"); //int sum = sum1+sum2+sum3+sum5+sum4 ; float fsum = nWidth*nHeight; cout << "1:" << sum1/fsum<<endl << "房屋: "<<sum2/fsum << endl << "3: "<<sum3/fsum <<endl << "土地: "<<sum4/fsum <<endl << "绿地: "<<sum5/fsum <<endl; //infile << "图像总像素数为:"<< sum << endl; //infile <<"道路: "<< sum1 << ",占总面积百分比为: " << sum1/fsum << endl // <<"为损坏建筑物: "<< sum2 << ",占总面积百分比为: " << sum2/fsum << endl // <<"损坏建筑物: "<< sum3 << ",占总面积百分比为: " << sum3/fsum << endl // <<"林地: "<< sum4 << ",占总面积百分比为: " << sum4/fsum << endl // <<"土地: "<< sum5 << ",占总面积百分比为: " << sum5/fsum << endl; //infile.close(); cvSaveImage("E:\\m1.bmp",img); //cvShowImage("Image",img); //cvWaitKey(0); //等待按键 //cvDestroyWindow( "Image" );//销毁窗口 return 0; }