简体   繁体   English

用户使用seekg从像素选择的C ++ RGB值

[英]C++ RGB values from pixel selected by user-using seekg

I need to create a program that loads a .raw image (generic 100x100 image), asks the user to select an (x, y) coordinate within the range, and display the red, green, and blue values for said pixel using the seekg function. 我需要创建一个程序来加载.raw图像(通用100x100图像),要求用户选择范围内的(x,y)坐标,并使用seekg显示该像素的红色,绿色和蓝色值功能。 I'm at a loss as to how to get the rgb values from the pixel. 我不知道如何从像素获取rgb值。 I've gone through every chapter of the textbook that we've covered so far, and there is nothing about retreiving rgb values. 到目前为止,我已经遍历了本书的每一章,也没有关于获取rgb值的任何知识。

The code asking for the coordinates and giving an error message if outside the range is working fine. 代码询问坐标,如果超出范围,则给出错误消息,可以正常工作。 Only when I try to come up with the code for using seekg/getting the rgb values am I running in to trouble. 只有当我尝试提出使用seekg /获取rgb值的代码时,我才会遇到麻烦。 I've looked at different questions on the site, and there is good information here, but I've not see any answers using seekg in order to get the rgb values. 我在网站上查看了不同的问题,这里有很好的信息,但是我没有看到使用seekg来获得rgb值的任何答案。

I'm not looking for anyone to produce the code for me, just looking for some guidance and a push in the right direction. 我并不是在寻找任何人为我编写代码,而是在寻找一些指导和朝着正确方向推进。

loc = (y * 100 + x) * 3;  // code given by professor with 100 being the width of the image
imageRaw.seekg(loc, ios::beg);

And then I'm at a loss. 然后我很茫然。

Any help would be greatly appreciated. 任何帮助将不胜感激。

From there, you probably need to read three bytes, which will represent the red, green, and blue values. 从那里,您可能需要读取三个字节,分别代表红色,绿色和蓝色值。 You haven't told us enough to be sure of the order; 您还没有告诉我们足够的信息来确定订单。 green is almost always in the middle, but RGB and BGR are both fairly common. 绿色几乎总是在中间,但是RGB和BGR都很常见。

From a practical viewpoint, for a picture of this size you don't normally want to use seekg at all though. 从实际的角度来看,对于这种大小的图片,您通常根本不希望使用seekg。 You'd read the entire image into memory, and lookup the values in the vector (or array, if you insist) that stores the data. 您已将整个图像读入内存,并在存储数据的向量(如果需要,则为数组)中查找值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM