可视化 如何获取图像中像素的颜色(以C++ (sfml) 为单位)
visual How to get the color of a pixel in an image in C++ (sfml)
我正在制作一个简单的赛车游戏,您可以在赛道上驾驶汽车。 赛道是灰色的,背景是绿色的,每当我在给定点(汽车的前部)上得到的颜色不是灰色时,汽车应该停下来,因为它离开了赛道。
但是,该曲目不是用 sfml 绘制的,而是我制作的下载图像。 那么,只要 rgb 值匹配,是否有任何方法可以在 IMAGE 上获取像素的颜色?
这是执行此操作的伪代码:
游戏运行时
获取颜色(汽车 X 值,汽车 Y 值)
如果颜色不是灰色
,汽车停止
谢谢!
您可以使用适当的方法获取sf::Image
中像素的sf::Color
:sf::Image::getPixel
。它需要 X 和 Y 坐标。
例:
sf::Image track;
if (!track.loadFromFile("track.jpg"))
{
// oops, loading failed, handle it
}
sf::Color color = track.getPixel(0, 0); // gets the color of the upper left corner pixel
你需要使用 sf::Image 的 getPixel(int x, int y) funciton。您的伪代码如下所示:
sf::Image track;
sf::Color grey_color; // You'll need to define what the grey color is.
// Take sf::Color(100, 100, 100) as an example.
if (!track.loadFromFile("track.jpg")) {
std::cout << "Uhoh" << std::endl;
}
while (gameIsRunning) {
sf::Color color_at_car = track.getPixel(car.getx(), car.gety());
if (color_at_car != grey_color) {
car.stop();
}
}
希望这有帮助!
相关文章:
- 将"打开的CV图像"中的"颜色"转换为整数格式
- 如何在内核C++中使用1920x1080x16M图形或类似的16M颜色?(VGA)
- 如何在24位SDL_Surface上设置像素的颜色
- 如何从SDL_Surface获取特定像素的颜色
- 列表视图更改选择颜色
- GtkTreeView 交替行颜色
- dx11 渲染到纹理仅显示透明颜色
- 使用 OpenGL 4.5 更改所选顶点的颜色
- 为什么我的 LEGACY OPENGL 颜色反转了?
- 双击更改 mfc 中列表控件中的行的颜色
- 更改 QT 中按钮的颜色
- 当我使用CHAR_INFO结构时,控制台会无缘无故地改变颜色
- 列表视图项的替代颜色
- GLSL 将 uint 转换为浮点颜色
- std::带有颜色和标题的 clog 包装器无法正确打印整数
- Vec3b:r/g/b 颜色的最大值为 254,而不是 255
- 如何将矢量对象更改为不同的颜色?
- PPM 颜色显示错误
- 如何更改 QGraphicsItem 对象的颜色?
- 如何防止每次 Arduino 启动时颜色数组都是相同的随机数组