Создание файла .csv в opencv
Я создаю файл .csv в opencv. Внутри в столбиках по горизонтали коэффициент, но когда получаю файл, результат не выводится. Есть у кого мысли по этому поводу?
#include "StdAfx.h"
#include <iostream>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include "stdafx.h"
#include<math.h>
#include<stdlib.h>
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include <cmath>
#include <functional>
#include <fstream>
using std::cout;
using std::cin;
using std::endl;
using namespace cv;
using namespace std;
void imhist(Mat image, int histogram[])
{
// initialize all intensity values to 0
for(int i = 0; i < 256; i++)
{
histogram[i] = 0;
}
// calculate the no of pixels for each intensity values
for(int y = 0; y < image.rows; y++)
for(int x = 0; x < image.cols; x++)
histogram[(int)image.at<uchar>(y,x)]++;
}
void histDisplay(int histogram[], const char* name)
{
int hist[256];
for(int i = 0; i < 256; i++)
{
hist[i]=histogram[i];
}
// draw the histograms
int hist_w = 512; int hist_h = 400;
int bin_w = cvRound((double) hist_w/256);
Mat histImage(hist_h, hist_w, CV_8UC1, Scalar(255, 255, 255));
// find the maximum intensity element from histogram
int max = hist[0];
for(int i = 1; i < 256; i++){
if(max < hist[i]){
max = hist[i];
}
}
// normalize the histogram between 0 and histImage.rows
for(int i = 0; i < 256; i++){
hist[i] = ((double)hist[i]/max)*histImage.rows;
}
// draw the intensity line for histogram
for(int i = 0; i < 256; i++)
{
line(histImage, Point(bin_w*(i), hist_h),
Point(bin_w*(i), hist_h - hist[i]),
Scalar(0,0,0), 1, 8, 0);
}
}
int main(int argc, char * argv[])
{
for (int iter = 1; iter <= 20; iter++) {
ofstream myfile;
char output_filename[100] = {0, };
sprintf(output_filename, "D:/output_%d.csv", iter);
myfile.open (output_filename);////////
std::cout << "Open File: " << output_filename << std::endl;
// from 128 to 224
for (int tg_intname = 30; tg_intname <=31 ; tg_intname += 1) {
for(int intname = 0; intname <= 256-(8*tg_intname); intname++)/////////////
{
char name[100] = {0, };
const int fn_tg_intname = 32 - tg_intname;
sprintf(name, "D:/test%d/%d/img_%d.bmp", iter, fn_tg_intname, intname);///////
std::cout << "Processing: " << name << std::endl;
//cout <<name<<"-";
//myfile<<name<<" ";
// Load the image
// Mat image = imread("D:/Research/Research/test1/cropped_100.bmp", CV_LOAD_IMAGE_GRAYSCALE);
Mat image = imread(name, CV_LOAD_IMAGE_GRAYSCALE);
// Generate the histogram
int histogram[256];
imhist(image, histogram);
int ysum, tmpys, xsum, tmpxs;
xsum=0;
ysum=0;
tmpxs=0;
float AVG = 0, PixelSum = 0, NumberPixel = 0;
for(int x = 0; x < image.rows; x++) {
for(int y = 0; y < image.cols; y++)
{
PixelSum += image.at<uchar>(x,y);
}
NumberPixel = image.rows*image.cols;
AVG = float(PixelSum / NumberPixel);
float Diff = 0;
for(int x = 0; x < image.rows; x++) {
for(int y = 0; y < image.cols; y++)
{
Diff += ((image.at<uchar>(x,y)/255) - (AVG/255)) * ((image.at<uchar>(x,y)/255) - (AVG/255));
}
float RMS =0;
RMS = sqrt((1/(NumberPixel))* Diff);
// cout<<"- RMS = "<<RMS<<endl ;
myfile << RMS << " ";
}
}
}
myfile << std::endl;
}
myfile.close();
}
waitKey();
return 0;
}
Источник: Stack Overflow на русском