我正在使用 OpenCV C++ 在视频/实时流/图像中执行特征检测。视频不同部分的光照条件不同,导致在将 RGB 图像转换为二值图像时某些部分被忽略。
视频特定部分的照明条件也会随着视频的播放而变化。我尝试了“直方图均衡”功能,但没有帮助。
我在以下链接中获得了 MATLAB 中的有效解决方案:
http://in.mathworks.com/help/images/examples/correcting-nonuniform-illumination.html
但是,上述链接中使用的大部分函数在 OpenCV 中不可用。
您能否建议在 OpenCV C++ 中替代此 MATLAB 代码?
最佳答案
OpenCV 在框架中提供了自适应阈值范例:http://docs.opencv.org/modules/imgproc/doc/miscellaneous_transformations.html#adaptivethreshold
函数原型(prototype)如下:
void adaptiveThreshold(InputArray src, OutputArray dst,
double maxValue, int adaptiveMethod,
int thresholdType, int blockSize, double C);
前两个参数是输入图像和存储输出阈值图像的位置。 maxValue
是分配给输出像素的阈值,如果它通过标准,adaptiveMethod
是用于自适应阈值的方法,thresholdType
是您要执行的阈值类型(稍后详述),blockSize
是要检查的窗口的大小(稍后详述),C
是要从每个窗口中减去的常量.我从来没有真正需要使用它,我通常将它设置为 0。
adaptiveThreshold
的默认方法是分析 blockSize x blockSize
窗口并计算此窗口内的平均强度减去 C
。如果这个窗口的中心高于平均强度,输出图像的输出位置中的这个对应位置设置为 maxValue
,否则相同位置设置为 0。这应该对抗非均匀照明问题,您不是对图像应用全局阈值,而是对局部像素邻域执行阈值处理。
您可以阅读有关其他参数的其他方法的文档,但要开始使用,您可以执行以下操作:
// Include libraries
#include <cv.h>
#include <highgui.h>
// For convenience
using namespace cv;
// Example function to adaptive threshold an image
void threshold()
{
// Load in an image - Change "image.jpg" to whatever your image is called
Mat image;
image = imread("image.jpg", 1);
// Convert image to grayscale and show the image
// Wait for user key before continuing
Mat gray_image;
cvtColor(image, gray_image, CV_BGR2GRAY);
namedWindow("Gray image", CV_WINDOW_AUTOSIZE);
imshow("Gray image", gray_image);
waitKey(0);
// Adaptive threshold the image
int maxValue = 255;
int blockSize = 25;
int C = 0;
adaptiveThreshold(gray_image, gray_image, maxValue,
CV_ADAPTIVE_THRESH_MEAN_C, CV_THRESH_BINARY,
blockSize, C);
// Show the thresholded image
// Wait for user key before continuing
namedWindow("Thresholded image", CV_WINDOW_AUTOSIZE);
imshow("Thresholded image", gray_image);
waitKey(0);
}
// Main function - Run the threshold function
int main( int argc, const char** argv )
{
threshold();
}
关于c++ - opencv C++ 中非均匀光照物体的检测,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31375948/