找到最接近的值c ++

时间:2015-01-03 19:31:46

标签: c++ algorithm k-means

我从文件中读取了一个RGB值的数据集,我正在尝试编码K表示算法,所以我需要在数据集中找到最接近的值为三个数字(3个值),你们中的任何人都可以帮助我告诉我该怎么做?

我是编程的老手,所以请对这种虐待表示温柔:L

感谢您的时间

以下代码读入文件:

#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; 
    }    

然后这段代码在数据集中找到最接近质心的值(value1),但继续阅读,你会看到我真正需要的是底部,这段代码是一种做法,可能有更好的方法: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;
}

我需要的是让代码找到接近180的所有数字,以及接近40的所有数字以及接近100的所有数字。

1 个答案:

答案 0 :(得分:0)

你的问题不是很清楚,这可能不是你想要的,但无论如何我都会试一试。

我认为你有一个RBG值数组和一些质心,可能是三个,它们也是RGB值。对于数组中的每个RBG值,您想知道哪个质心最接近。这将有效地将您的数组拆分为与质心一样多的子数组。

您可以为每个质心创建单独的数组。我已经看到你使用C ++,虽然有许多C语言。例如,您使用的是固定大小的数组而不是std::vector

我让我的解决方案接近于此。我没有提供额外的数组,而是扩展了rgb类,以便对最近的质心进行整数引用。 split函数指定此值。

RGB值之间的距离测量为空间距离,就好像红色,绿色和蓝色值是三维空间中的x,y,z值。

下面的代码使用预定义的12个RGB值数组,为了简洁起见,省略了从文件中读取列表的过程。质心是纯红色,绿色和蓝色,但理论上它们可以在任何地方。

#include <iostream>

struct rgb {
    int r; 
    int g;
    int b;
    int ref;    // reference index of closest centroid
};

char hexdigit(int x)
{
    if (x < 0) return '?';
    if (x < 10) return '0' + x;
    if (x < 16) return 'a' + x - 10;
    return '?';
}

std::ostream &operator <<(std::ostream &os, rgb const &x)
{
    return os << '#'
        << hexdigit(x.r / 16) << hexdigit(x.r % 16)
        << hexdigit(x.g / 16) << hexdigit(x.g % 16)
        << hexdigit(x.b / 16) << hexdigit(x.b % 16);
}

void split(rgb pool[], size_t npool, rgb const cent[], size_t ncent)
{
    for (size_t i = 0; i < npool; i++) {
        rgb *p = pool + i;
        int dmin = 4*255*255;

        for (size_t j = 0; j < ncent; j++) {
            rgb const *c = cent + j;
            int rr = p->r - c->r;
            int gg = p->g - c->g;
            int bb = p->b - c->b;
            int dd = rr*rr + gg*gg + bb*bb;

            if (dd < dmin) {
                p->ref = j;
                dmin = dd;
            }
        }
    }
}

int main(int argc, char* argv[])
{
    rgb data[12] = {
        {106,  92,  69},
        {135,  16, 137},
        {208, 204, 220},
        { 11,  52, 120},
        { 33,  57,  81},
        {196, 199, 192},
        {236, 237,  85},
        { 89,  27, 225},
        {173,  74, 158},
        {220,   2,  66},
        {171, 145, 204},
        {221,  60, 235}
    };

    rgb cent[3] = {
        {255, 0, 0},
        {0, 255, 0},
        {0, 0, 255}
    };

    split(data, 12, cent, 3);

    for (int i = 0; i < 12; i++) {
        std::cout << data[i] << "    " << cent[data[i].ref] << std::endl;
    }

    return 0;
}
相关问题