weixin_56337147 2022-05-07 09:39 采纳率: 29.4%
浏览 338
已结题

C++opencv获取矩形角点的像素坐标

问题遇到的现象和发生背景

类似这种墙面上的相框,怎么通过C++opencv获取矩形相框角点的像素坐标

img

我的解答思路和尝试过的方法

ORB::create()存在的问题:在矩形角上选了很多个特征点,另外识别的是整个画面的特征点,找不到哪个坐标是期望矩形的角点

我想要达到的结果
  • 写回答

1条回答 默认 最新

  • 赵4老师 2022-05-07 18:08
    关注

    仅供参考:

    #include "opencv2/highgui/highgui.hpp"
    #include "opencv2/imgproc/imgproc.hpp"
    #include "opencv2/imgproc/imgproc_c.h"
    #include "opencv2\highgui\highgui_c.h"
    using namespace std;
    using namespace cv;
    Mat img,smallImg,gray,bw;
    vector<Vec4i> hierarchy;
    vector<vector<Point> > contours;
    int threshval=128;
    Rect r;
    Rect maxrect,brect;
    int idx,n;
    const static Scalar colors[15]={
        CV_RGB(  0,  0,128),
        CV_RGB(  0,128,  0),
        CV_RGB(  0,128,128),
        CV_RGB(128,  0,  0),
        CV_RGB(128,  0,128),
        CV_RGB(128,128,  0),
        CV_RGB(128,128,128),
        CV_RGB(160,160,160),
        CV_RGB(  0,  0,255),
        CV_RGB(  0,255,  0),
        CV_RGB(  0,255,255),
        CV_RGB(255,  0,  0),
        CV_RGB(255,  0,255),
        CV_RGB(255,255,  0),
        CV_RGB(255,255,255),
    };
    Scalar color;
    void gamma_correct(Mat& img, Mat& dst, double gamma) {
        Mat tmp;
    
        img.convertTo(tmp, CV_32FC1, 1.0/255.0, 0.0);
        pow(tmp, gamma, tmp);
        tmp.convertTo(dst , CV_8UC1,     255.0, 0.0);
    }
    int main() {
        cvNamedWindow("display",1);
        img=imread("image.jpg",1);
        r.x     =img.cols*1/10;
        r.y     =img.rows*1/5;
        r.width =img.cols*8/10;
        r.height=img.rows*3/5;
        smallImg=img(r);
        cvtColor(smallImg,gray,CV_BGR2GRAY);
    //  medianBlur(gray,gray,5);
        equalizeHist(gray,gray);
        gamma_correct(gray,gray,4.0);
        imshow("display",gray);
        waitKey(0);
    
        bw=(gray>threshval);
        imshow("display",bw);
        waitKey(0);
    
    //  Mat Structure1=getStructuringElement(MORPH_RECT   ,Size(6,6));
        Mat Structure1=getStructuringElement(MORPH_ELLIPSE,Size(6,6));
        dilate(bw,bw,Structure1, Point(-1,-1));
        imshow("display",bw);
        waitKey(0);
    
        Mat Structure0=getStructuringElement(MORPH_RECT   ,Size(3,3));
        Mat Structure0=getStructuringElement(MORPH_ELLIPSE,Size(3,3));
        erode(bw,bw,Structure0,Point(-1,-1));
        imshow("display",bw);
        waitKey(0);
    
        findContours(bw,contours,hierarchy,RETR_EXTERNAL,CHAIN_APPROX_SIMPLE);
        if (!contours.empty()&&!hierarchy.empty()) {
            idx=0;
            n=0;
            vector<Point> approx;
            for (;idx>=0;idx=hierarchy[idx][0]) {
                color=colors[idx%15];
    //          drawContours(smallImg,contours,idx,color,1,8,hierarchy);
                approxPolyDP(Mat(contours[idx]), approx, arcLength(Mat(contours[idx]), true)*0.005, true);//0.005为将毛边拉直的系数
                const Point* p = &approx[0];
                int m=(int)approx.size();
                polylines(smallImg, &p, &m, 1, true, color);
                circle(smallImg,Point(p[0].x,p[0].y),3,color);
                circle(smallImg,Point(p[1].x,p[1].y),2,color);
                for (int i=2;i<m;i++) circle(smallImg,Point(p[i].x,p[i].y),1,color);
                n++;
                if (1==n) {
                    maxrect=boundingRect(Mat(contours[idx]));
                } else {
                    brect=boundingRect(Mat(contours[idx]));
                    CvRect mr;
                    mr.x=maxrect.x;
                    mr.y=maxrect.y;
                    mr.width=maxrect.width;
                    mr.height=maxrect.height;
                    CvRect br;
                    br.x=brect.x;
                    br.y=brect.y;
                    br.width=brect.width;
                    br.height=brect.height;
                    maxrect=cvMaxRect(&mr,&br);
                }
            }
            circle(smallImg,Point(maxrect.x+maxrect.width/2,maxrect.y+maxrect.height/2),2,CV_RGB(255,0,0));
        }
        imshow("display",smallImg);
        waitKey(0);
        cvDestroyWindow("display");
        return 0;
    }
    
    
    
    评论

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 6月18日
  • 创建了问题 5月7日

悬赏问题

  • ¥15 关于#单片机#的问题:以ATMEGA128或相近型号单片机为控制器设计直流电机调速的闭环控制系统(相关搜索:设计报告|软件设计|流程图)
  • ¥15 打开软件提示错误:failed to get wglChoosePixelFormatARB
  • ¥30 电脑误删了手机的照片怎么恢复?
  • ¥15 (标签-python|关键词-char)
  • ¥15 python+selenium,在新增时弹出了一个输入框
  • ¥15 苹果验机结果的api接口哪里有??单次调用1毛钱及以下。
  • ¥20 学生成绩管理系统设计
  • ¥15 来一个cc穿盾脚本开发者
  • ¥15 CST2023安装报错
  • ¥15 使用diffusionbert生成文字 结果是PAD和UNK怎么办