本文介绍了在opencv C ++中检测非均匀照明中的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用OpenCV C ++在视频/直播流/图像中执行功能检测。照明条件在视频的不同部分不同,导致一些部分在将RGB图像转换为二进制图像时被忽略。

I am performing feature detection in a video/live stream/image using OpenCV C++. The lighting condition varies in different parts of the video, leading to some parts getting ignored while transforming the RGB images to binary images.

在特定部分的照明条件视频也在视频过程中改变。我尝试了直方图均衡功能,但它没有帮助。

The lighting condition in a particular portion of the video also changes over the course of the video. I tried the 'Histogram equalization' function, but it didn't help.

我在MATLAB中有一个工作的解决方案在以下链接:

I got a working solution in MATLAB in the following link:

但是,上述链接中使用的大多数功能都不可用在OpenCV中。

However, most of the functions used in the above link aren't available in OpenCV.

您可以在OpenCV C ++中建议使用此MATLAB代码吗?

Can you suggest the alternative of this MATLAB code in OpenCV C++?

推荐答案

OpenCV在框架中提供了自适应阈值范例:

OpenCV has the adaptive threshold paradigm available in the framework: http://docs.opencv.org/modules/imgproc/doc/miscellaneous_transformations.html#adaptivethreshold

函数原型如下:

void adaptiveThreshold(InputArray src, OutputArray dst,
                      double maxValue, int adaptiveMethod,
                      int thresholdType, int blockSize, double C);

前两个参数是输入图像和存储输出阈值图像的位置。 maxValue 是通过标准时分配给输出像素的阈值值, adaptiveMethod 是用于自适应thresholding, thresholdType 是您要执行的阈值类型(更高版本), blockSize 是窗口的大小(稍后), C 是从每个窗口减去的常数。

The first two parameters are the input image and a place to store the output thresholded image. maxValue is the thresholded value assigned to an output pixel should it pass the criteria, adaptiveMethod is the method to use for adaptive thresholding, thresholdType is the type of thresholding you want to perform (more later), blockSize is the size of the windows to examine (more later), and C is a constant to subtract from each window. I've never really needed to use this and I usually set this to 0.

adaptiveThreshold 的默认方法。是分析 blockSize x blockSize 窗口并计算此窗口内的平均强度减去 C 。如果该窗口的中心高于平均强度,则输出图像的输出位置中的该相应位置被设置为 maxValue ,否则相同的位置被设置为0。这可以解决非均匀照明问题,而不是对图像应用全局阈值,而是对局部像素邻域执行阈值处理。

The default method for adaptiveThreshold is to analyze blockSize x blockSize windows and calculate the mean intensity within this window subtracted by C. If the centre of this window is above the mean intensity, this corresponding location in the output position of the output image is set to maxValue, else the same position is set to 0. This should combat the non-uniform illumination issue where instead of applying a global threshold to the image, you are performing the thresholding on local pixel neighbourhoods.

您可以阅读文档对其他方法的其他参数,但是让你开始,你可以这样做:

You can read the documentation on the other methods for the other parameters, but to get your started, you can do something like this:

// 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();
}

这篇关于在opencv C ++中检测非均匀照明中的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-16 08:05