是否可以将1024x1024位图的RGB值与仅具有1,048,576比较的输入文件的值进行比较?

发布于 2025-02-04 15:04:31 字数 1200 浏览 4 评论 0原文

我所做的是:

打开一个输入文件(TXT),其RGB值为64个颜色条目。

打开另一个输入文件,即1024x1024位图,带有颜色值(每个像素3个字节)。

从TXT文件中读取并将其从该值分配到结构数组中。

结构阵列具有R,G,B值和所讨论的颜色名称。 我要做的下一件事是读取RGB值的位图,并将其与结构中的值进行比较。

我创建了一个以1,048,576为限制的循环,另一个用64作为比较值的限制。问题是比较计数将导致67,108,864。 理论最低为1,048,576。

这就是我到目前为止的:

 for (int i = 0;!bitmap.eof(); i += 3)
{
    unsigned char red, green, blue;
    bitmap.read((char*)&blue, sizeof(unsigned char));
    bitmap.read((char*)&green, sizeof(unsigned char));
    bitmap.read((char*)&red, sizeof(unsigned char));
    for (int j = 0; j < 64; j++)
    {
        if (int(blue) == bmiColors[j].b && int(green) == bmiColors[j].g && int(red) == bmiColors[j].r)
        {
            //cout << "B: " << int(b) << " G: " << int(g) << " R: " << int(r) << endl;
                //cout << "Position: " << bitmap.tellg() << endl;
            bmiColors[j].matchCount += 1;
            count++;
        }
        else
        {
            bmiColors[j].skipCount += 1;
            count++;
        }
    }
}
                           

您将如何优化它?我得到的提示是将像素颜色用作索引中的索引。请帮助我。

What I did was:

Open an input file (txt) with the RGB values of 64 color entries.

Open another input file that is a 1024x1024 bitmap with color values (3 bytes per pixel).

Read from the txt file and allocate the values from that into an array of structures.

The array of structures holds R, G, B values and the name of the color in question.
Next thing that I did was read the bitmap for RGB values and compare them to the values in the struct.

I created a for loop with 1,048,576 as the limit and another for loop inside with 64 as the limit to compare the values. Problem is the count of comparisons will result in 67,108,864.
Theoretical minimum is 1,048,576.

This is what I have so far:

 for (int i = 0;!bitmap.eof(); i += 3)
{
    unsigned char red, green, blue;
    bitmap.read((char*)&blue, sizeof(unsigned char));
    bitmap.read((char*)&green, sizeof(unsigned char));
    bitmap.read((char*)&red, sizeof(unsigned char));
    for (int j = 0; j < 64; j++)
    {
        if (int(blue) == bmiColors[j].b && int(green) == bmiColors[j].g && int(red) == bmiColors[j].r)
        {
            //cout << "B: " << int(b) << " G: " << int(g) << " R: " << int(r) << endl;
                //cout << "Position: " << bitmap.tellg() << endl;
            bmiColors[j].matchCount += 1;
            count++;
        }
        else
        {
            bmiColors[j].skipCount += 1;
            count++;
        }
    }
}
                           

How would you go about optimizing it? The hint that I got was to use the pixel color as the index into a table. Please help me out.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

路还长,别太狂 2025-02-11 15:04:31

将像素颜色作为索引到表中可能是有问题的。颜色{255,255,255}将具有索引16777215255*65536 + 255*255*256 + 255)。它可能太大也可能太大。类似的想法可能是使用某种地图,例如unordered_map并将64 bmicolors在其中存储并映射到找到的计数在bitmap文件中。

示例:

#include <cstdint>
#include <fstream>
#include <iostream>
#include <iterator>
#include <unordered_map>

// A class to store one pixel
struct RGB {
    uint8_t r;
    uint8_t g;
    uint8_t b;

    // compare two RGB:s:
    bool operator==(const RGB& rhs) const { return r == rhs.r
                                                && g == rhs.g
                                                && b == rhs.b; }

    // read one RGB from an istream:
    friend std::istream& operator>>(std::istream& is, RGB& rgb) {
        is.read(reinterpret_cast<char*>(&rgb.r), 1);
        is.read(reinterpret_cast<char*>(&rgb.g), 1);
        is.read(reinterpret_cast<char*>(&rgb.b), 1);
        return is;
    }
};

// A specialization of `std::hash` for RGB to be able to use it in an `unorderded_map`:
namespace std {
template<>
struct hash<RGB> {
    size_t operator()(const RGB& rgb) const noexcept { return rgb.r << 16
                                                            | rgb.g << 8
                                                            | rgb.b; }
};
} // namespace std

然后,主要程序将

  • 创建unordered_mapbmicolors,通过读取包含64个值的文件。
  • bitmap文件中读取并使用bmicolors.find查看颜色是否存在。
    • 如果存在,请增加映射的值。
  • 打印结果
int main() {
    std::unordered_map<RGB, std::uint32_t> bmiColors;
    {
        std::ifstream bmi("bmi.bmp");
        if(not bmi) return 1;
        std::transform(std::istream_iterator<RGB>(bmi), std::istream_iterator<RGB>{},
                       std::inserter(bmiColors, bmiColors.end()), [](const RGB& rgb) {
                           return std::pair{rgb, 0};
                       });
    }

    if(std::ifstream bitmap("file.bmp"); bitmap) {
        std::for_each(std::istream_iterator<RGB>(bitmap), std::istream_iterator<RGB>{}, 
                      [&bmiColors](const RGB& rgb) {
                          if(auto it = bmiColors.find(rgb); it != bmiColors.end()) {
                              ++it->second;
                          }
                      });
    }

    // print the result
    for(auto& [rgb, count] : bmiColors) {
        if(count) {
            std::cout << static_cast<unsigned>(rgb.r) << ' '
                      << static_cast<unsigned>(rgb.g) << ' '
                      << static_cast<unsigned>(rgb.b) << ' ' << count << '\n';
        }
    }
}

Using the pixel color as the index into a table may be problematic. The color {255, 255, 255} would have the index 16777215 (255*65536 + 255*256 + 255). It may or may not be too large. A similar idea could be to use some kind of map, like an unordered_map and store the 64 bmiColors in that and map them to the count found in the bitmap file.

Example:

#include <cstdint>
#include <fstream>
#include <iostream>
#include <iterator>
#include <unordered_map>

// A class to store one pixel
struct RGB {
    uint8_t r;
    uint8_t g;
    uint8_t b;

    // compare two RGB:s:
    bool operator==(const RGB& rhs) const { return r == rhs.r
                                                && g == rhs.g
                                                && b == rhs.b; }

    // read one RGB from an istream:
    friend std::istream& operator>>(std::istream& is, RGB& rgb) {
        is.read(reinterpret_cast<char*>(&rgb.r), 1);
        is.read(reinterpret_cast<char*>(&rgb.g), 1);
        is.read(reinterpret_cast<char*>(&rgb.b), 1);
        return is;
    }
};

// A specialization of `std::hash` for RGB to be able to use it in an `unorderded_map`:
namespace std {
template<>
struct hash<RGB> {
    size_t operator()(const RGB& rgb) const noexcept { return rgb.r << 16
                                                            | rgb.g << 8
                                                            | rgb.b; }
};
} // namespace std

The main program would then

  • Create an unordered_map, bmiColors, by reading the file containing the 64 values.
  • Read from the bitmap file and use bmiColors.find to see if the color exists.
    • If it exists, increase the mapped value.
  • Print the result
int main() {
    std::unordered_map<RGB, std::uint32_t> bmiColors;
    {
        std::ifstream bmi("bmi.bmp");
        if(not bmi) return 1;
        std::transform(std::istream_iterator<RGB>(bmi), std::istream_iterator<RGB>{},
                       std::inserter(bmiColors, bmiColors.end()), [](const RGB& rgb) {
                           return std::pair{rgb, 0};
                       });
    }

    if(std::ifstream bitmap("file.bmp"); bitmap) {
        std::for_each(std::istream_iterator<RGB>(bitmap), std::istream_iterator<RGB>{}, 
                      [&bmiColors](const RGB& rgb) {
                          if(auto it = bmiColors.find(rgb); it != bmiColors.end()) {
                              ++it->second;
                          }
                      });
    }

    // print the result
    for(auto& [rgb, count] : bmiColors) {
        if(count) {
            std::cout << static_cast<unsigned>(rgb.r) << ' '
                      << static_cast<unsigned>(rgb.g) << ' '
                      << static_cast<unsigned>(rgb.b) << ' ' << count << '\n';
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文