视频处理 OpenCV?
我试图将 HSV 帧从视频传递到该函数,但该函数似乎没有对其执行任何操作。我做错了什么?该函数应该遍历每个像素,并根据其色调范围将其设为黑色或白色,从而给我留下一个二值图像。相反,它似乎根本不影响 HSV 图像......
谢谢 PS 对于错误的代码格式感到抱歉,StackOverflow 不允许我发布原始格式。
void sort (IplImage *skinmask)
{
for (int row=0; row<=skinmask->height;row++)
{
uchar* pixelrow=(uchar*)(skinmask->imageData+(row*(skinmask->widthStep)));
for (int column=0; column<=skinmask->width; column++)
{
if (6<pixelrow[3*column]<36)
{
pixelrow[3*column]=256;
pixelrow[(3*column)+1]=256;
pixelrow[(3*column)+2]=256;
}
else
{
pixelrow[3*column]=0;
pixelrow[(3*column)+1]=0;
pixelrow[(3*column)+2]=0;
}
column++;
}
row++;
}
cvMorphologyEx(skinmask,skinmask,NULL,NULL,CV_MOP_CLOSE,1);
}
I am trying to pass in a HSV frame from a video to the function, but the function does not seem to do anything to it. What am I doing wrong? The function is supposed to go through each pixel, and depending on its hue range supposed to make it black or white, leaving me with a binary image. Instead it doesn't seem to affect the HSV image at all....
Thanks
PS sorry for the bad code formatting, StackOverflow isn't allowing me to post the original format.
void sort (IplImage *skinmask)
{
for (int row=0; row<=skinmask->height;row++)
{
uchar* pixelrow=(uchar*)(skinmask->imageData+(row*(skinmask->widthStep)));
for (int column=0; column<=skinmask->width; column++)
{
if (6<pixelrow[3*column]<36)
{
pixelrow[3*column]=256;
pixelrow[(3*column)+1]=256;
pixelrow[(3*column)+2]=256;
}
else
{
pixelrow[3*column]=0;
pixelrow[(3*column)+1]=0;
pixelrow[(3*column)+2]=0;
}
column++;
}
row++;
}
cvMorphologyEx(skinmask,skinmask,NULL,NULL,CV_MOP_CLOSE,1);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 OpenCV 中,执行诸如逐像素阈值化之类的操作通常是错误的方法 - 有些函数可以在整个图像阵列上工作,这些函数更简单并且已经针对速度进行了优化。
在这种情况下,首先尝试分割图像以分离出H/S/V 通道,然后 色调通道上的阈值以获得遮罩(您可能必须使用两个蒙版的交集,您可以使用乘法或“按位与”来完成) - 生成的蒙版是您的黑白图像。
(我意识到我已经链接到了 C++ 文档,但我确信您可以在旧式 OpenCV 文档中找到等效的函数)
更新
好的,我将尝试编写一些代码来展示我的意思。我还找到了我正在寻找的函数,它比 Threshold 更好,它是 在范围内。这使您可以同时在所有通道上设置上限和下限,并将它们全部应用到您的蒙版中。
附言。我发现了你的原始代码的问题 - 你应该使用 255 而不是 256 作为你的“白色”值。不过这个方法还是更好:)
PPS。我们毕竟不需要它们,但供将来参考:
"bitwise and"< /a>:
如果您有两个黑色和白色蒙版,这将为您提供交集。使用 cvOr 获得 Union。
Doing an operation like thresholding pixel-by-pixel is usually the wrong way to go about achieving this in OpenCV - there are functions that work on whole image arrays that are simpler and are already optimized for speed.
In this case try first splitting the image to separate out the H/S/V channels, then threshold on the Hue channel to get a mask (you may have to use the intersection of two masks, which you can do using a multiply or "bitwise and") - the resulting mask is your black and white image.
(I realise I've linked to the C++ documentation, but I'm sure you can find the equivalent functions in the old-style OpenCV docs)
Update
Ok, I'll try to write some code to show what I mean. I also found the function I was looking for, which is better than Threshold, it is InRangeS. This lets you put upper and lower bounds on all the channels at once, and it applies them all into your mask for you.
PS. I figured out the problem with your original code - you should be using 255 instead of 256 as your "white" value. This method is still better though :)
PPS. We didn't need them after all but for future reference:
"bitwise and":
If you have two black and white masks, this will give you the intersection. Use cvOr to get the Union.