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

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日

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效