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)

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

img
红色框中心就是十字中心,计算出来坐标(310,364)