用opencv检测棕色物体
我想在图像中找到棕色物体。我已完成以下过程:用opencv检测棕色物体,opencv,image-processing,Opencv,Image Processing,我想在图像中找到棕色物体。我已完成以下过程: 将图像BGR转换为HSV 我使用opencv库的inRange函数来查找棕色 cv::inRange(src,标量(9,95,95),标量(17,255,255),dest) 找到了轮廓,但我没有得到轮廓 输入图像 问题 我想在上面的图像中检测眼睛的棕色。当我使用上面的棕色范围时,我得到了零轮廓 以上棕色范围是否正确?应该是什么?您可以在图像中分割一个棕色对象,并使用HSV范围进行播放。由于棕色在某种程度上是暗红色,因此需要稍微调整参数。如果你发布
以上棕色范围是否正确?应该是什么?您可以在图像中分割一个棕色对象,并使用HSV范围进行播放。由于棕色在某种程度上是暗红色,因此需要稍微调整参数。如果你发布一张参考图片,我们可以找到一个更精确的范围 一旦有了对象遮罩(通常应用一些形态学来清洁遮罩),就可以使用
findContours
轻松获得轮廓
下面的例子解释了这一点:
#include <iostream>
#include <vector>
#include "opencv2/opencv.hpp"
using namespace std;
using namespace cv;
int main()
{
Mat3b img = imread("path_to_image");
Mat3b hsv;
cvtColor(img, hsv, COLOR_BGR2HSV);
Mat1b mask1, mask2;
inRange(hsv, Scalar(0, 100, 20), Scalar(10, 255, 255), mask1);
inRange(hsv, Scalar(170, 100, 20), Scalar(180, 255, 255), mask2);
Mat1b mask = mask1 | mask2;
Mat1b kernel = getStructuringElement(MORPH_ELLIPSE, Size(7,7));
morphologyEx(mask, mask, MORPH_OPEN, kernel);
vector<vector<Point>> contours;
findContours(mask.clone(), contours, CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE);
Mat3b res = img.clone();
for(int i=0; i<contours.size(); ++i)
{
drawContours(res, contours, i, Scalar(0,255,0));
RotatedRect r = minAreaRect(contours[i]);
Point2f pts[4];
r.points(pts);
for (int j = 0; j < 4; ++j)
{
line(res, pts[j], pts[(j + 1) % 4], Scalar(0,0,255));
}
Rect box = boundingRect(contours[i]);
rectangle(res, box, Scalar(255,0,0));
}
imshow("Original", img);
imshow("Segmented", res);
waitKey();
return 0;
}
#包括
#包括
#包括“opencv2/opencv.hpp”
使用名称空间std;
使用名称空间cv;
int main()
{
Mat3b img=imread(“路径到图像”);
Mat3b型单纯疱疹病毒;
CVT颜色(img、hsv、彩色);
Mat1b mask1,mask2;
范围(hsv,标量(0,100,20),标量(10,255,255),mask1);
范围(hsv,标量(17010020),标量(180255255),mask2);
Mat1b掩模=mask1 | mask2;
Mat1b内核=getStructuringElement(变形椭圆,大小(7,7));
形态学(遮罩,遮罩,形态开放,内核);
矢量等值线;
findContours(mask.clone()、等高线、CV_RETR_列表、CV_CHAIN_APPROX_SIMPLE);
Mat3b res=img.clone();
对于(int i=0;i 1)
{
对于(int i=0;i
结果:
注意:如果您需要更准确的信息,您应该发布一个新问题,专门询问瞳孔检测。
我将删除一些有用的链接,以防万一:
请包括您正在使用的代码。。。
#include <iostream>
#include <vector>
#include "opencv2/opencv.hpp"
using namespace std;
using namespace cv;
int main()
{
Mat3b img = imread("D:\\SO\\img\\eye.jpg");
Mat3b hsv;
cvtColor(img, hsv, COLOR_BGR2HSV);
Mat1b mask;
inRange(hsv, Scalar(2, 100, 65), Scalar(12, 170, 100), mask);
Mat1b kernel = getStructuringElement(MORPH_ELLIPSE, Size(3, 3));
morphologyEx(mask, mask, MORPH_OPEN, kernel);
vector<vector<Point>> contours;
findContours(mask.clone(), contours, CV_RETR_LIST, CV_CHAIN_APPROX_NONE);
if (contours.empty()) {return -1;}
int idx_largest_blob = 0;
int size_largest_blob = contours[0].size();
if (contours.size() > 1)
{
for (int i = 0; i < contours.size(); ++i)
{
if (size_largest_blob < contours[i].size())
{
size_largest_blob = contours[i].size();
idx_largest_blob = i;
}
}
}
Mat3b res = img.clone();
drawContours(res, contours, idx_largest_blob, Scalar(0, 255, 0));
RotatedRect r = minAreaRect(contours[idx_largest_blob]);
Point2f pts[4];
r.points(pts);
for (int j = 0; j < 4; ++j)
{
line(res, pts[j], pts[(j + 1) % 4], Scalar(0, 0, 255));
}
Rect box = boundingRect(contours[idx_largest_blob]);
rectangle(res, box, Scalar(255, 0, 0));
imshow("Original", img);
imshow("Segmented", res);
waitKey();
return 0;
}