框出白点区域 判断1为ok ,2为ng c++代码

 

你论坛上有人回复了,你可以看一下

您这是开局一张图,内容全靠编?

说说思路也行啊

 

循环判断每一个像素点,如果是白点,则记录该坐标点,不断对比,找到四个角的坐标点。分别是:x最小y最小,x最小y最大,x最大y最小,x最大y最大。

两个区域分别框怎么弄呢

 

 

设置一个阈值,如果超过阈值,则判为第二个框

阈值可以设为2行白点的正常间距

可以写一下代码嘛 c++用的不熟 谢谢

 

#include <opencv2/opencv.hpp>
#include<opencv2/imgproc/types_c.h>//
#include <iostream>
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"

using namespace std;
using namespace cv;//使用命名空间


int main()
{

    Mat img = imread("C:\\Users\\Administrator\\Desktop\\image\\ConsoleApplication1\\x64\\Debug\\1610594031027.png");//    读取图片 这里文件地址写成你想打开的文件的地址或者放在执行目录里就不用写路径直接使用文件名字
    if (img.empty())
    {
        fprintf(stderr, "无法打开图片 \n");
        return -1;
    }
    //imshow("load image", img);   //在窗口中打开图片

    Mat imgThresholded;
    Mat imgBGR = img.clone();;
    

    inRange(imgBGR, Scalar(128, 128, 128), Scalar(255, 255, 255), imgThresholded); //白色

    std::cout << imgThresholded.rows << "\n" << imgThresholded.cols << "\n";

    int nl = imgThresholded.rows;//gao du
    int nc = imgThresholded.cols;//kuan du

    int up[3] = { nl,nl,nl };
    int down[3] = {0};
    int left[3] = { nc,nc,nc };
    int right[3] = {0};
    int lastHang = 0;
    int point = -1;

    //std::cout << up << " ";
    //std::cout << down << " ";
    //std::cout << left << " ";
    //std::cout << right << "\n";

    //遍历图像的每个像素
    for (int j = 20; j < nl-20; j++)//gao du
    {
        for (int i = 20; i < nc-20; i++)//kuandu
        {
            if (imgThresholded.at<uchar>(j, i) == 255)
            {
                if ((j - lastHang) > 23)
                {
                    point++;
                    
                }
                if (point < 0)continue;
                if (i < left[point])
                {
                    left[point] = i;
                }
                if (i>right[point])
                {
                   right[point] =i;
                }
                if (j <up[point])
                {
                    up[point] = j;
                }
                if (j>down[point])
                {
                    down[point] = j;
                }
                lastHang = j;
            }
        }
    }
    //std::cout << up << " ";
    //std::cout << down << " ";
    //std::cout << left << " ";
    //std::cout << right << "\n";

    std::cout << point << "\n";

    //for (int i = 0; i < point + 1; i++)
    //{
    //    cv::rectangle(imgThresholded, cvPoint(left[point], up[point]), cvPoint(right[point], down[point]), Scalar(255, 255, 255), 1, 1, 0);
    //}
    
    cv::rectangle(imgThresholded, cvPoint(left[0], up[0]), cvPoint(right[0], down[0]), Scalar(255, 255, 255), 1, 1, 0);
    cv::rectangle(imgThresholded, cvPoint(left[1], up[1]), cvPoint(right[1], down[1]), Scalar(255, 255, 255), 1, 1, 0);

    imshow("out", imgThresholded);

    waitKey(100000);//等待时间,这里等待时间最好别填零,要不打开的窗口秒退


    return 0;
}