gpt4 book ai didi

c++ - Opencv - 如何获取图像中存在的垂直线数(线数)

转载 作者:行者123 更新时间:2023-12-04 07:29:50 25 4
gpt4 key购买 nike

首先,我将 OpenCV 框架集成到 XCode 中,所有 OpenCV 代码都在 ObjectiveC 上,我在 Swift 中使用桥接头。我是 OpenCV 框架的新手,并试图从图像中获得垂直线的数量。
这是我的代码:
首先我将图像转换为灰度

 + (UIImage *)convertToGrayscale:(UIImage *)image {
cv::Mat mat;
UIImageToMat(image, mat);
cv::Mat gray;
cv::cvtColor(mat, gray, CV_RGB2GRAY);
UIImage *grayscale = MatToUIImage(gray);
return grayscale;
}
然后,我正在检测边缘,以便我可以找到灰色线
+ (UIImage *)detectEdgesInRGBImage:(UIImage *)image {
cv::Mat mat;
UIImageToMat(image, mat);

//Prepare the image for findContours
cv::threshold(mat, mat, 128, 255, CV_THRESH_BINARY);

//Find the contours. Use the contourOutput Mat so the original image doesn't get overwritten
std::vector<std::vector<cv::Point> > contours;
cv::Mat contourOutput = mat.clone();
cv::findContours( contourOutput, contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE );

NSLog(@"Count =>%lu", contours.size());

//For Blue
/*cv::GaussianBlur(mat, gray, cv::Size(11, 11), 0); */

UIImage *grayscale = MatToUIImage(mat);
return grayscale;
}
这两个函数都是写在Objective C上的
在这里,我调用两个函数 Swift
override func viewDidLoad() {
super.viewDidLoad()
let img = UIImage(named: "imagenamed")
let img1 = Wrapper.convert(toGrayscale: img)
self.capturedImageView.image = Wrapper.detectEdges(inRGBImage: img1)
}
我这样做了几天并找到了一些有用的文件(引用链接)
OpenCV - how to count objects in photo?
How to count number of lines (Hough Trasnform) in OpenCV
OpenCV 文档
https://docs.opencv.org/2.4/modules/imgproc/doc/structural_analysis_and_shape_descriptors.html?#findcontours
基本上,我理解首先我们需要将此图像转换为黑白,然后使用 cvtColor, threshold and findContours我们可以找到颜色或线条。
我附上了我想要获得的垂直线的图像。
原图
Original Image
我得到的输出图像
Output Image that I am getting
我得到的行数 =>10
我无法在这里得到准确的计数。
请指导我。谢谢你!

最佳答案

由于您想检测垂直线的数量,因此我可以为您建议一个非常简单的方法。你已经得到了一个清晰的输出,我在我的代码中使用了这个输出。下面是代码之前的步骤:

  • 预处理 输入图像以获得清晰的线条
  • 检查每一行并检查直到得到值大于100的像素(阈值我选择了)
  • 然后增加专线柜台该行
  • 继续在那条线上,直到得到一个像素值低于 100
  • 从第 3 步重新开始,完成每行的图像
  • 最后,查看 最重复的元素 在您为每行分配行号的数组中。这个数字将是垂直线的数量。

  • 注意:如果步骤难以理解,可以这样思考:

    " I am checking the first row, I found a pixel which is higher than100, now this is a line edge starting, increase the counter for thisrow. Search on this row until get a pixel smaller than 100, and thenresearch a pixel bigger than 100. when row is finished, assign theline number for this row to a big array. Do this for all image. At theend, since some lines looks like two lines at the top and also somenoises can occur, you should take the most repeated element in the bigarray as the number of lines."


    这是C++中的代码部分:
    #include <vector>
    #include <iostream>
    #include <opencv2/opencv.hpp>
    #include <opencv2/highgui/highgui.hpp>


    int main()
    {
    cv::Mat img = cv::imread("/ur/img/dir/img.jpg",cv::IMREAD_GRAYSCALE);

    std::vector<int> numberOfVerticalLinesForEachRow;


    cv::Rect r(0,0,img.cols-10,200);

    img = img(r);

    bool blackCheck = 1;

    for(int i=0; i<img.rows; i++)
    {
    int numberOfLines = 0;

    for(int j=0; j<img.cols; j++)
    {
    if((int)img.at<uchar>(cv::Point(j,i))>100 && blackCheck)
    {
    numberOfLines++;
    blackCheck = 0;
    }
    if((int)img.at<uchar>(cv::Point(j,i))<100)
    blackCheck = 1;
    }

    numberOfVerticalLinesForEachRow.push_back(numberOfLines);
    }

    // In this part you need a simple algorithm to check the most repeated element
    for(int k:numberOfVerticalLinesForEachRow)
    std::cout<<k<<std::endl;





    cv::namedWindow("WinWin",0);

    cv::imshow("WinWin",img);

    cv::waitKey(0);


    }

    关于c++ - Opencv - 如何获取图像中存在的垂直线数(线数),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68007307/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com