openCV中求轮廓和凸包的问题

发布于 2025-01-03 04:55:48 字数 1856 浏览 3 评论 0原文

我写了下面的

#include"opencv2/opencv.hpp"

 #include<iostream>
 #include<math.h>
 using namespace std;
 using namespace cv;

main()
{
Mat img1,img2,sub,gray1,gray2,lab,ycbcr;
int v[3];

int row,col,i,j,t;
VideoCapture cap(0);

namedWindow("current");

cap>>img1;
sub=img1;

row=img1.rows;
col=img1.cols;

cvtColor(img1,gray1,CV_BGR2GRAY);


vector<vector<Point> > cont;

vector<Vec4i> hierarchy;

while (1) {

    cap>>img2;

    cvtColor(img2,gray2,CV_BGR2GRAY);


    for(i=0;i<row;++i)
    {
        for (j=0; j<col; ++j)
        {


            if(abs(gray1.at<uchar>(i,j) - gray2.at<uchar>(i,j))>10)
            {
                sub.at<Vec3b>(i,j)[0] = img2.at<Vec3b>(i,j)[0];
                sub.at<Vec3b>(i,j)[1] = img2.at<Vec3b>(i,j)[1];
                sub.at<Vec3b>(i,j)[2] = img2.at<Vec3b>(i,j)[2];

            }
            else
            {

                sub.at<Vec3b>(i,j)[0]=0;
                sub.at<Vec3b>(i,j)[1]=0;
                sub.at<Vec3b>(i,j)[2]=0;
            }

        }


    }


    cvtColor(sub,ycbcr,CV_BGR2YCrCb);

    inRange(ycbcr,Scalar(7,133,106),Scalar(255,178,129),ycbcr);
    findContours(ycbcr,cont,hierarchy,CV_RETR_LIST,CV_CHAIN_APPROX_SIMPLE);
    Scalar color = CV_RGB(255,0,0);
    for(int i1 = 0 ;i1 >= 0; i1 = hierarchy[i1][0] )
    drawContours( ycbcr, cont, i1, color,2, CV_AA, hierarchy );



    vector<Point2f > hullPoints;
  // convexHull(Mat(cont),hullPoints,false);
    imshow("current",ycbcr);

代码 if(waitKey(33)=='q') 休息; img1=img2.clone(); 1.

)尽管我通过 CV_RGB(255,0,0) 指定了轮廓,

但为什么轮廓没有以红色显示。 2.)当我取消注释该行时 凸包(Mat(续),hullPoints,假); ,程序显示运行时错误。为什么会发生这种情况。有人可以告诉我凸包()的确切格式吗 及其论点的意义

I have written the following code

#include"opencv2/opencv.hpp"

 #include<iostream>
 #include<math.h>
 using namespace std;
 using namespace cv;

main()
{
Mat img1,img2,sub,gray1,gray2,lab,ycbcr;
int v[3];

int row,col,i,j,t;
VideoCapture cap(0);

namedWindow("current");

cap>>img1;
sub=img1;

row=img1.rows;
col=img1.cols;

cvtColor(img1,gray1,CV_BGR2GRAY);


vector<vector<Point> > cont;

vector<Vec4i> hierarchy;

while (1) {

    cap>>img2;

    cvtColor(img2,gray2,CV_BGR2GRAY);


    for(i=0;i<row;++i)
    {
        for (j=0; j<col; ++j)
        {


            if(abs(gray1.at<uchar>(i,j) - gray2.at<uchar>(i,j))>10)
            {
                sub.at<Vec3b>(i,j)[0] = img2.at<Vec3b>(i,j)[0];
                sub.at<Vec3b>(i,j)[1] = img2.at<Vec3b>(i,j)[1];
                sub.at<Vec3b>(i,j)[2] = img2.at<Vec3b>(i,j)[2];

            }
            else
            {

                sub.at<Vec3b>(i,j)[0]=0;
                sub.at<Vec3b>(i,j)[1]=0;
                sub.at<Vec3b>(i,j)[2]=0;
            }

        }


    }


    cvtColor(sub,ycbcr,CV_BGR2YCrCb);

    inRange(ycbcr,Scalar(7,133,106),Scalar(255,178,129),ycbcr);
    findContours(ycbcr,cont,hierarchy,CV_RETR_LIST,CV_CHAIN_APPROX_SIMPLE);
    Scalar color = CV_RGB(255,0,0);
    for(int i1 = 0 ;i1 >= 0; i1 = hierarchy[i1][0] )
    drawContours( ycbcr, cont, i1, color,2, CV_AA, hierarchy );



    vector<Point2f > hullPoints;
  // convexHull(Mat(cont),hullPoints,false);
    imshow("current",ycbcr);

a
if(waitKey(33)=='q')
break;
img1=img2.clone();
}

}

1.)Why the contours are not displaying in red color although i specified it through CV_RGB(255,0,0).
2.)When i uncomment the line
convexHull(Mat(cont),hullPoints,false);
,the program shows runtime error.Why is it happening.Can anybody tell me the exact format of convexHull()
and the meaning of its arguments

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

与往事干杯 2025-01-10 04:55:48

1) 尝试 (0,0,255) 获取红色。 OpenCV 使用 BGR 格式。

2)要查找凸包,请尝试 OpenCV 教程。

还可以在 Convexhull 上尝试与您类似的问题。 如何使用openCV函数计算凸包面积?

1) Try (0,0,255) for red color. OpenCV uses BGR format.

2) For finding convex hull, try the code in OpenCV tutorial.

Also try similar question as yours on Convexhull. How to calculate convex hull area using openCV functions?

小猫一只 2025-01-10 04:55:48

您的 hullPoints 向量为空。如果您检查向量的大小,则不会出现错误。

if(hullPoints.size() > 2) {
  convexHull(....);
 }

Your hullPoints vector is empty. If you check the size of vector,you don't get an error.

if(hullPoints.size() > 2) {
  convexHull(....);
 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文