C++,如何加载图像并计算某些颜色的像素
这是我的问题:
我正在寻找一种方法将图像导入 C++,然后遍历其像素,每次找到某种颜色的像素时递增计数器。
我做了一些研究,但没有发现任何特别有用的东西。 DevIL 看起来是一个不错的选择,但我不知道从哪里开始。
这里有一些 C++/python 伪代码,希望能够说明我正在尝试做的事情:
for image in folder:
A = 0;
B = 0;
for pixel in image:
if (pixel == colourA) {A++}
if (pixel == colourB) {B++}
//Output the count of colours for each image
outputToFile(A, B);
有人有一些关于从哪里开始的提示吗?
谢谢
编辑 一些额外信息:我使用的是 Windows 7,所有图像都是 .pngs
EDIT2 除了实际找出当前像素的颜色之外,我已经完成了所有工作。目前我正在使用这个:
int blue = ((uchar *)(img->imageData + pixelX*img->widthStep))[pixelY*img->nChannels + 0];
但它不起作用,我不知道它是如何工作的。我无法找到任何我能理解的内容。谁能给我指出如何找到某个像素的 RGB 值的正确方向?
编辑3 完毕!对于任何发现此尝试做类似事情的人,可以找到我剩下的大部分问题和相当多的代码 此处。感谢您的帮助!
here's my problem:
I'm looking for a way to import an image into C++ then traverse its pixels, incrementing a counter every time a pixel of a certain colour is found.
I've done some research, but I haven't found anything particularly useful. DevIL looks like a good option, but I'm not sure where to start.
Here's a bit of C++/python pseudo-code hopefully illustrating what I'm trying to do:
for image in folder:
A = 0;
B = 0;
for pixel in image:
if (pixel == colourA) {A++}
if (pixel == colourB) {B++}
//Output the count of colours for each image
outputToFile(A, B);
Does anyone have some tips on where to start?
Thanks
EDIT
Some extra information: I'm using Windows 7 and all the images are .pngs
EDIT2
I've got everything working, except actually finding out the colour of the current pixel. Currently I'm using this:
int blue = ((uchar *)(img->imageData + pixelX*img->widthStep))[pixelY*img->nChannels + 0];
But it doesn't work, and I have no idea how it works. I haven't been able to find anything about this that I could understand. Could anyone point me in the right direction on how to find the RGB values of a certain pixel?
Edit3
Done! For anyone who finds this trying to do a similar thing, most of my remaining questions and a fair bit of code can be found here. Thanks for the help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你应该看看 OpenCV。
You should take a look at OpenCV.
一定要看看 OpenCV 因为当你开始需要更多的移动空间时OpenCV 将让您完成更多计算机视觉任务。并使用 boost::filesystem 来执行“对于目录中的每个图像”代码。
请注意, cv::compare 函数基本上完成了一半的工作已经为您准备好了...我将让您阅读该内容并享受利用 OpenCV API 的乐趣。
Definiately take a look at OpenCV because when you begin to need more room to move then OpenCV will let you do many more computer vision tasks. And use boost::filesystem to do the 'for each image in dir' code.
Note that the cv::compare function basically does half the work for you already...I'll let you read that and enjoy leveraging the OpenCV API.
取决于平台和图像格式。在 Windows 中,本机支持 BMP 图像(即在 API 中)。 ImageMagick 是一个跨平台库,非常通用,可以处理任何格式,但它很重。 Qt 也有一些图像处理功能 - 仅限于最常见的格式。
Depends on platform and image format. In Windows, BMP images are supported natively (i. e. in the API). ImageMagick is a cross-platform library, pretty universal, takes about any format out there, but it's heavy. Qt has some image processing as well - limited to the most common formats.
SDL 有一些有用的像素操作内容。
http://www.libsdl.org/
它也非常干净。
SDL has some useful pixel manipulation stuff.
http://www.libsdl.org/
It's very clean as well.