qq_36908843 2018-04-08 04:01 采纳率: 0%
浏览 5253
已结题

如何使用opencv提取下面这幅图片的十字线中心坐标

图片说明

  • 写回答

2条回答 默认 最新

  • tylerzhangdi 2018-04-08 05:55
    关注
     #include "opencv2/highgui/highgui.hpp"
    #include "opencv2/imgproc/imgproc.hpp"
    #include "opencv2/imgproc/imgproc_c.h"
    using namespace std;
    using namespace cv;
    Mat img,gray,bw;
    vector<Vec4i> hierarchy;
    vector<vector<Point> > contours;
    int threshval=50;
    Rect r;
    Rect maxrect,brect;
    int idx,n;
    int main() {
        cvNamedWindow("img",1);
        img=imread("cross.jpg",1);
        cvtColor(img,gray,CV_BGR2GRAY);
        bw=threshval>128?(gray<threshval):(gray>threshval);
        findContours(bw,contours,hierarchy,RETR_EXTERNAL,CHAIN_APPROX_SIMPLE);
        if (!contours.empty()&&!hierarchy.empty()) {
            idx=0;
            n=0;
            for (;idx>=0;idx=hierarchy[idx][0]) {
                n++;
                if (1==n) {
                    maxrect=boundingRect(Mat(contours[idx]));
                } else {
                    brect=boundingRect(Mat(contours[idx]));
                    CvRect mr(maxrect),br(brect);
                    maxrect=cvMaxRect(&mr,&br);
                }
            }
            circle(img,Point(maxrect.x+maxrect.width/2,maxrect.y+maxrect.height/2),2,Scalar(0,0,255));
        }
        imshow("img",img);
        waitKey(0);
        cvDestroyWindow("img");
        return 0;
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题