OpenCV#39;s Canny Edge Detection in C++(OpenCV 在 C++ 中的 Canny 边缘检测)
问题描述
我想提取手的边缘,但得到以下结果.我已尝试调整低阈值和高阈值,但仍然无法获得所需的输出.我已经包含在代码及其输出下面.似乎是什么问题?
I want to extract the edges of hand but I get the following result. I've tried adjusting the low and high threshold but I still can't get the desired output. I have included below the code and its output. What seems to be the problem?
这是由以下代码生成的输出图像.
This is the output image generated by the code below.
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
int main(){
cv::Mat image= cv::imread("open_1a.jpg");
cv::Mat contours;
cv::Mat gray_image;
cvtColor( image, gray_image, CV_RGB2GRAY );
cv::Canny(image,contours,10,350);
cv::namedWindow("Image");
cv::imshow("Image",image);
cv::namedWindow("Gray");
cv::imshow("Gray",gray_image);
cv::namedWindow("Canny");
cv::imshow("Canny",contours);
cv::waitKey(0);
}
推荐答案
更改此行
cvtColor( image, gray_image, CV_RGB2GRAY );
到
std::vector<cv::Mat> channels;
cv::Mat hsv;
cv::cvtColor( image, hsv, CV_RGB2HSV );
cv::split(hsv, channels);
gray_image = channels[0];
问题似乎是你的手的灰度与灰色背景非常接近.我在色调(颜色)上应用了 Canny,因为肤色应该足够不同.
The problem seems to be that your hand in gray scale is very close to the gray background. I have applied Canny on the hue (color) because the skin color should be sufficiently different.
此外,Canny 阈值看起来有点疯狂.公认的规范是较高的应该是较低的 2 到 3 倍.350有点太多了,无助于解决主要问题.
Also, the Canny thresholds look a bit crazy. The accepted norm is that the higher one should be 2x to 3x the lower. 350 is a bit too much and it doesn't help solve the main problem.
编辑
通过这些阈值,我能够提取出相当不错的轮廓
with these thresholds I was able to extract quite a good contour
cv::Canny(image,contours,35,90);
cv::Canny(image,contours,35,90);
阅读有关算法的一些理论将帮助您了解会发生什么以及您应该采取哪些措施来改进.wiki canny
在 google 上
Reading a bit of theory about the algorithm will help you understand what happens and what you should do to improve. wiki canny
on google
然而,上述改进会给你带来更好的结果(假设你使用比 10, 350 更好的阈值.试试 (40, 120) )
However, the improvement above will give you much better results (provided you use better thresholds than 10, 350. Try (40, 120) )
这篇关于OpenCV 在 C++ 中的 Canny 边缘检测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:OpenCV 在 C++ 中的 Canny 边缘检测
基础教程推荐
- 您如何将 CreateThread 用于属于类成员的函数? 2021-01-01
- C++ 标准:取消引用 NULL 指针以获取引用? 2021-01-01
- C++ 程序在执行 std::string 分配时总是崩溃 2022-01-01
- 如何在 C++ 中处理或避免堆栈溢出 2022-01-01
- 设计字符串本地化的最佳方法 2022-01-01
- 什么是T&&(双与号)在 C++11 中是什么意思? 2022-11-04
- 调用std::Package_TASK::Get_Future()时可能出现争用情况 2022-12-17
- C++,'if' 表达式中的变量声明 2021-01-01
- 运算符重载的基本规则和习语是什么? 2022-10-31
- 如何定义双括号/双迭代器运算符,类似于向量的向量? 2022-01-01