在OpenCV中使用inRange()检测范围内的颜色 我用OpenCV编写了一个C++程序,用于月球陨石坑探测,它只精确检测了一小部分陨石坑。这种方法的策略是首先将图像转换为HSV,然后使用inRange()捕捉一系列值中的颜色以生成阈值,然后对其进行高斯模糊,并使用HoughCircles()检测圆
我不完全理解的一件事是,当我在颜色周围给出一个高低阈值时,它根本不返回任何东西。只是一张黑色的照片。仅当我将低阈值设置为标量(0,0,0)时,它才起作用,但我相信这会使它有点不准确。有什么我不明白的吗?我的测试图像如下 月球表面 这是我用来测试此图像的代码:在OpenCV中使用inRange()检测范围内的颜色 我用OpenCV编写了一个C++程序,用于月球陨石坑探测,它只精确检测了一小部分陨石坑。这种方法的策略是首先将图像转换为HSV,然后使用inRange()捕捉一系列值中的颜色以生成阈值,然后对其进行高斯模糊,并使用HoughCircles()检测圆,c++,opencv,feature-detection,hough-transform,C++,Opencv,Feature Detection,Hough Transform,我不完全理解的一件事是,当我在颜色周围给出一个高低阈值时,它根本不返回任何东西。只是一张黑色的照片。仅当我将低阈值设置为标量(0,0,0)时,它才起作用,但我相信这会使它有点不准确。有什么我不明白的吗?我的测试图像如下 月球表面 这是我用来测试此图像的代码: #include <cstdio> #include <iostream> #include "opencv2/core/core.hpp" #include "opencv2/highgui/highgui.hp
#include <cstdio>
#include <iostream>
#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/features2d/features2d.hpp"
using namespace std;
using namespace cv;
int main(int argc, char** argv) {
// using namespace cv;
printf("%s\n", argv[1]);
Mat src=imread(argv[1]);
if (!src.data) {
std::cout << "ERROR:\topening image" <<std::endl;
return -1;
}
// converts the image to hsv so that circle detection is more accurate
Mat hsv_image;
cvtColor(src, hsv_image, COLOR_BGR2HSV);
// high contrast black and white
Mat imgThreshold;
inRange(hsv_image,
Scalar(0, 0, 0),
Scalar(48, 207, 74),
imgThreshold);
// Applies a gaussian blur to the image
GaussianBlur( imgThreshold, imgThreshold, Size(9, 9), 2, 2 );
// fastNlMeansDenoisingColored(imgThreshold, imgThreshold, 10, 10, 7, 21);
vector<Vec3f> circles;
// applies a hough transform to the image
HoughCircles(imgThreshold, circles, CV_HOUGH_GRADIENT,
2, // accumulator resolution (size of image / 2)
100, //minimum dist between two circles
400, // Canny high threshold
10, // minimum number of votes
10, 65); // min and max radius
cout << circles.size() << endl;
cout << "end of test" << endl;
vector<Vec3f>::
const_iterator itc = circles.begin();
// Draws the circles on the source image
while (itc!=circles.end()) {
circle(src, // src_gray2
Point((*itc)[0], (*itc)[1]), // circle center
(*itc)[2], // circle radius
Scalar(0,0,255), // color
5); // thickness
++itc;
}
namedWindow("Threshold",CV_WINDOW_AUTOSIZE);
resize(imgThreshold, imgThreshold, Size(src.cols/2,src.rows/2) ); // resizes it so it fits on our screen
imshow("Threshold",imgThreshold); // displays the source iamge
namedWindow("HSV Color Space",CV_WINDOW_AUTOSIZE);
resize(hsv_image, hsv_image, Size(src.cols/2,src.rows/2) ); // resizes it so it fits on our screen
imshow("HSV Color Space",hsv_image); // displays the source iamge
namedWindow("Source Image",CV_WINDOW_AUTOSIZE);
resize(src, src, Size(src.cols/2,src.rows/2) ); // resizes it so it fits on our screen
imshow("Source Image",src); // displays the source iamge
waitKey(0);
return 0;
}
#包括
#包括
#包括“opencv2/core/core.hpp”
#包括“opencv2/highgui/highgui.hpp”
#包括“opencv2/imgproc/imgproc.hpp”
#包括“opencv2/features2d/features2d.hpp”
使用名称空间std;
使用名称空间cv;
int main(int argc,字符**argv){
//使用名称空间cv;
printf(“%s\n”,argv[1]);
Mat src=imread(argv[1]);
如果(!src.data){
std::cout这是我的尝试:
int main(int argc, char** argv)
{
Mat src;
src = imread("craters1.jpg", 1);
cvtColor(src, hsv_image, COLOR_BGR2HSV);
Mat imgThreshold1, imgThreshold2, imgThreshold;
inRange(hsv_image,
Scalar(0, 0, 0),
Scalar(48, 207, 74),
imgThreshold1);
inRange(hsv_image,
Scalar(140, 0, 0),
Scalar(180, 207, 114),
imgThreshold2);
imgThreshold = max(imgThreshold1, imgThreshold2); // combining the two thresholds
Mat element_erode = getStructuringElement(MORPH_ELLIPSE, Size(5, 5));
Mat element_dilate = getStructuringElement(MORPH_ELLIPSE, Size(10, 10));
/// Apply the erosion and dilation operations
erode(imgThreshold, imgThreshold, element_erode);
dilate(imgThreshold, imgThreshold, element_dilate);
GaussianBlur(imgThreshold, imgThreshold, Size(9, 9), 2, 2);
vector<vector<Point> > contours;
vector<Vec4i> hierarchy;
/// Find contours
findContours(imgThreshold, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0));
for (int i = 0; i < contours.size(); i++)
{
drawContours(src, contours, i, Scalar(0,0,255), 2, 8, hierarchy, 0, Point());
}
namedWindow("Display Image", WINDOW_AUTOSIZE);
imshow("Display Image", imgThreshold);
imshow("Final result", src);
waitKey(0);
return 0;
}
int main(int argc,char**argv)
{
Mat-src;
src=imread(“1.jpg”,1);
cvtColor(src、hsv_图像、彩色BGR2HSV);
Mat imgThreshold 1、imgThreshold 2、imgThreshold;
在范围内(hsv_图像,
标量(0,0,0),
标量(4820774),
imgThreshold1);
在范围内(hsv_图像,
标量(140,0,0),
标量(180207114),
imgThreshold2);
imgThreshold=max(imgThreshold1,imgThreshold2);//组合两个阈值
Mat element_腐蚀=getStructuringElement(变形椭圆,大小(5,5));
Mat element_deplate=getStructuringElement(变形椭圆,大小(10,10));
///应用侵蚀和膨胀操作
侵蚀(imgThreshold,imgThreshold,元素侵蚀);
扩张(imgThreshold,imgThreshold,element_扩张);
GaussianBlur(imgThreshold,imgThreshold,Size(9,9),2,2);
矢量等值线;
向量层次;
///寻找轮廓
findContours(imgThreshold、等高线、层次、CV_RETR_树、CV_CHAIN_APPROX_SIMPLE、点(0,0));
对于(int i=0;i
你的代码的主要区别在于我没有使用HoughCircles
。我不确定它是否会产生好的效果,因为陨石坑没有完美的圆形。相反,我使用findContours
来环绕陨石坑。我得到的结果如下:
希望能有帮助!我想你是用(28,50100)
的下限和(7216213)的上限打电话的
?文档非常清楚函数的作用。在本例中,对于通道0,您计算的是28@DanMašek No的结果,这是不正确的,我没有尝试调用它。这只是对几个上界的未标记注释,我对下界(0,0,0)进行了测试是的,我彻底检查了文档,这是我做的第一件事。那么,什么部分不起作用呢?我的意思是,只有一个函数调用,根据您所写的“只有当我将低阈值设置为标量(0,0,0)
”时,它才起作用。此外,如果注释不相关,那么不要在问题中显示它们(删除不相关的垃圾需要几秒钟。)@DanMašek正如我在问题“当我给inRange()一个关于颜色的高低阈值时,它根本不返回任何东西。只是一个黑色图像”中提到的那样,为了更详细地说明这一点,我将给出一个例子。假设我的RGB颜色为(66,53,10)当我在两个边界上使用inRange(比如说(13206,56)到(33226,76)时,我从图像中选择并将其转换为hsv(23216,66)围绕该颜色,它只是返回一个黑色图像,就好像它没有找到任何颜色一样。我这样做是否正确?即使我进一步打开边界,它似乎也不起作用。@shakked:D花更多时间研究堆栈溢出,并让我知道您是否仍然持有这种观点。我想,opencv
标记本身就是一个充分的证据恰恰相反。哦,哇,这是一个非常好的解决方案。我有点意识到HoughCircles对此应用程序的局限性,但我不知道您的FindOntours解决方案!谢谢您!而且,我不知道可以通过这种方式组合两个阈值。谢谢您的帮助,这很好。