如何使用opencv找到凸性缺陷?

发布于 2025-01-02 23:34:39 字数 2042 浏览 2 评论 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,255,255);
    vector<vector<Point> >  hullPoints(cont.size());
    for(  i = 0; i < cont.size(); i++ )
    convexHull(cont[i],hullPoints[i],false);

    for(i = 0 ;i >= 0; i = hierarchy[i][0] )
    {
        drawContours( ycbcr, cont, i, color,1, CV_AA, hierarchy );//for drawing contours
        drawContours( ycbcr, hullPoints, i, color,2, CV_AA, hierarchy );//for drawing convex hull
    }

    flip(ycbcr,ycbcr,1);
    imshow("current",ycbcr);

    if(waitKey(33)=='q')
        break;

    img1=img2.clone();
}

}

如何找到凸包中的凸性缺陷..cvConvexityDefects() 需要 const cvArr * 作为参数。但是我有来自凸包的向量点类型结果..那么如何类型转换..?

I have 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,255,255);
    vector<vector<Point> >  hullPoints(cont.size());
    for(  i = 0; i < cont.size(); i++ )
    convexHull(cont[i],hullPoints[i],false);

    for(i = 0 ;i >= 0; i = hierarchy[i][0] )
    {
        drawContours( ycbcr, cont, i, color,1, CV_AA, hierarchy );//for drawing contours
        drawContours( ycbcr, hullPoints, i, color,2, CV_AA, hierarchy );//for drawing convex hull
    }

    flip(ycbcr,ycbcr,1);
    imshow("current",ycbcr);

    if(waitKey(33)=='q')
        break;

    img1=img2.clone();
}

}

How to find the convexity defects in this convexHull..cvConvexityDefects() require const cvArr * as arguments.But i have vector point type result from convexHull..So how to type cast ..?

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

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

发布评论

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

评论(1

℉服软 2025-01-09 23:34:39

您应该使用 vector> 类型进行 convexHull 计算:

convexHull(Mat(contours), hullsI, false); // false means it will return the indices at which the hulls are found and not the point of the convex hull

然后调用:

convexityDefects(contours, hullsI, defects);

类似于 凸性缺陷 C++ OpenCv
和许多其他问题:)希望有帮助!

You should be using the vector<vector<int>> type for your convexHull calculations:

convexHull(Mat(contours), hullsI, false); // false means it will return the indices at which the hulls are found and not the point of the convex hull

Then call:

convexityDefects(contours, hullsI, defects);

Similar to Convexity defects C++ OpenCv
and many other SO questions :) Hope that helps!

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