小王007-l 2021-07-28 16:41 采纳率: 50%
浏览 320
已结题

vs2017 opencv 获取图像中十字的中心点

img

工作需要,只有一点编程基础,写到这里不知道怎么往下写了,利用图像中黑色像素点的最大值找到中心点。勿喷,真的基础不咋地,我知道这个很简单,但我就是不会啊。


#include <opencv2/opencv.hpp>
#include<iostream>
#include<math.h>
using namespace std;
using namespace cv;

Mat pho, col, img2;


int main()
{
    //namedWindow("php", CV_WINDOW_AUTOSIZE);
    pho = imread("E://png图片//08.png");
    //imshow("php", pho);

    //灰度图转换
    cvtColor(pho, col, CV_BGR2GRAY);
    //imshow("php2", col);

    //图像二值化
    threshold(col, img2, 100, 255, CV_THRESH_BINARY);
    imshow("phto", img2);

    //遍历行列

    int heigh = img2.rows; //求全部的行数
    int wight = img2.cols;//求全部的列数

    for (int row = 0; row < heigh; row++)
    {
        for (int col = 0; col < wight; col++)
        {
            if (img2.at<uchar>(row, col) == 0)
  • 写回答

2条回答 默认 最新

  • soar3033 2021-07-28 16:52
    关注

    center_x,center_y是中心坐标

    
    
    #include <opencv2/opencv.hpp>
    #include<iostream>
    #include<math.h>
    using namespace std;
    using namespace cv;
    Mat pho, col, img2;
    
    int main()
    {
        //namedWindow("php", CV_WINDOW_AUTOSIZE);
        pho = imread("E://png图片//08.png");
        //imshow("php", pho);
        //灰度图转换
        cvtColor(pho, col, CV_BGR2GRAY);
        //imshow("php2", col);
        //图像二值化
        threshold(col, img2, 100, 255, CV_THRESH_BINARY);
        imshow("phto", img2);
        //遍历行列
        int heigh = img2.rows; //求全部的行数
        int wight = img2.cols;//求全部的列数
        int hl = wight;//hl代表列值最小的非白色像素列值 起始赋值为图像宽度
        int vl = heigh;//vl代表行值最小的非白色像素的行值 起始赋值为图像总高度
        int center_x;//中心点x值
        int center_y;//中心点y值
        for (int row = 0; row < heigh; row++)//遍历行
        {
            for (int col = 0; col < wight; col++)//遍历列
            {
                if (img2.at<uchar>(row, col) != 0) {//如果像素不是空像素
                    if (col<hl)//如果当前列数小于之前存储的非白像素的最小列值
                    {
                        hl = col;//更新最小的非白单元格列值
                        center_y = row;//中心点y值改为当前行值
                    }
                    if (row<vl)//如果当前行小于之前的非白色像素的最小行值
                    {
                        vl = row;//最小行值改为当前行
                        center_x = col;//更新中心点x值为当前列值
                    }
                }
            }
        }
    }
    cout<<“x”<<center_x<<“\n”;//输出x
    cout<<“y“<<center_y<<“\n”;//输出y
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 7月29日
  • 已采纳回答 7月28日
  • 创建了问题 7月28日

悬赏问题

  • ¥15 latex投稿显示click download
  • ¥15 请问读取环境变量文件失败是什么原因?
  • ¥15 在若依框架下实现人脸识别
  • ¥15 网络科学导论,网络控制
  • ¥100 安卓tv程序连接SQLSERVER2008问题
  • ¥15 利用Sentinel-2和Landsat8做一个水库的长时序NDVI的对比,为什么Snetinel-2计算的结果最小值特别小,而Lansat8就很平均
  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?