是否可以将1024x1024位图的RGB值与仅具有1,048,576比较的输入文件的值进行比较?
我所做的是:
打开一个输入文件(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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将像素颜色作为索引到表中可能是有问题的。颜色
{255,255,255}
将具有索引16777215
(255*65536 + 255*255*256 + 255
)。它可能太大也可能太大。类似的想法可能是使用某种地图
,例如unordered_map
并将64bmicolors
在其中存储并映射到找到的计数在bitmap
文件中。示例:
然后,主要程序将
unordered_map
,bmicolors
,通过读取包含64个值的文件。bitmap
文件中读取并使用bmicolors.find
查看颜色是否存在。Using the pixel color as the index into a table may be problematic. The color
{255, 255, 255}
would have the index16777215
(255*65536 + 255*256 + 255
). It may or may not be too large. A similar idea could be to use some kind ofmap
, like anunordered_map
and store the 64bmiColors
in that and map them to the count found in thebitmap
file.Example:
The main program would then
unordered_map
,bmiColors
, by reading the file containing the 64 values.bitmap
file and usebmiColors.find
to see if the color exists.