Vote count:
0
I have read from file a data set of RGB values, I am trying to code K means algorithm so I need to find the closest values in the data set to three numbers (3 values), can any of you help me in showing me how to do this?
I am noob to programming so please be gentle with the abuse :L
Thank you for your time
This code below reads in the file:
#include "stdafx.h"
#include <iostream>;
#include <fstream>;
#include <string>;
#include <cstdlib>;
using namespace std;
class rgb {
public:
int r;
int g;
int b;
};
int _tmain(int argc, _TCHAR* argv[])
{
char filename[2000];
ifstream infile;
infile.open("file.txt");
if (infile.fail()) {
cerr << "something went wrong :(" << endl;
system("pause");
exit(1);
}
rgb array[500];
cout << "reading in file: " << endl;
for (int i = 0; i < 500; i++)
{
infile >> array[i].r >> array[i].g >> array[i].b;
cout << "r: " << array[i].r << " ";
cout << "g: " << array[i].g << " ";
cout << "b: " << array[i].b << " " << endl;
}
Then this code finds the closest value in the dataset to centroid one (value1), but read on and you will see what i really need at the bottom, This code was a practice there are probably better ways of doing it :L
int num = 180; // random value assigned at 180
int centroid1x;
int distance = 400;
for (int i = 0; i < 500; i++)
{
if (abs(num - array[i].r) <= distance)
{
distance = abs(num - array[i].r);
centroid1x = array[i].r;
}
cout << centroid1x << " " ;
}
cout << endl << endl;
int centroid1y;
distance = 400;
for (int i = 0; i < 500; i++)
{
if (abs(num - array[i].g) <= distance)
{
distance = abs(num - array[i].g);
centroid1y = array[i].g;
}
cout << centroid1y << " " ;
}
cout << endl << endl;
int centroid1z;
distance = 400;
for (int i = 0; i < 500; i++)
{
if (abs(num - array[i].b) <= distance)
{
distance = abs(num - array[i].b);
centroid1z = array[i].b;
}
cout << centroid1z << " " ;
}
cout << endl << endl;
cout << "The closest x axis of centroid one is: " << centroid1x << endl;
cout << "The closest y axis of centroid one is: " << centroid1y << endl;
cout << "The closest z axis of centroid one is: " << centroid1z << endl << endl;
cout << "The closest point to centroid one is " << centroid1x << "." << centroid1y << "." << centroid1z << endl;
system("pause");
return 0;
}
What i need is for the code to find all the numbers that are closer to 180, and all the numbers that are closer to 40 and all the numbers that are closer to 100.
asked 36 secs ago
find closest values c++
Aucun commentaire:
Enregistrer un commentaire