gpt4 book ai didi

algorithm - 将图像对象分成N个相等像素的部分(方法)

转载 作者:行者123 更新时间:2023-12-02 16:22:56 25 4
gpt4 key购买 nike

预先抱歉,这更多是算法问题,而不是编码问题,但是我不确定该放在哪里。为了简单起见,假设您有一个二进制图像(白色背景,前景中为纯黑色对象)
例:
sample input
我想将此对象(仅表示黑色像素)划分为N个部分,所有部分均具有相同数量的像素(因此每个部分应包含(1 / N)*(黑色像素总数)。
使用我当前使用的算法,我(1)找到黑色像素的总数,然后(2)除以N。然后我(3)逐行扫描图像,标记所有黑色像素。结果看起来像这样:
current output sketch
问题是最后一个(黄色)部分,它不是连续的。我想以一种更有意义的方式来划分图像,如下所示:
ideal output
基本上,我希望各部分之间的边界尽可能短。
我已经为此感到困惑了一段时间,但是我的旧代码只是不再使用它了。我只需要一种识别部分的方法,最终将每个部分输出为单独的图像,并输入图像的灰度副本,其中每个像素的值对应于其部分编号(这些我不需要帮助。与)。有任何想法吗?

最佳答案

I only need an approach to identifying the sections


据此,我尝试了几种方法,这些方法可能有助于指导原则:
  • 查找图像的轮廓
  • 找到轮廓的moments并检测重心。
  • 对于外角,您可以简单地使用convex hull
  • 找到最接近质量中心的轮廓点(将是内角)
  • 然后您可以通过使用这些重要点将其分离到所需区域

  • 这是结果和代码:
    enter image description here
    #include "opencv2/imgcodecs.hpp"
    #include "opencv2/highgui.hpp"
    #include "opencv2/imgproc.hpp"
    #include <iostream>
    using namespace cv;
    using namespace std;

    vector<Point>innerCorners;
    bool isClose(Point test);
    int main()
    {
    Mat src_gray;
    int thresh = 100;
    Mat src = imread("image/dir/star.png");
    cvtColor( src, src_gray, COLOR_BGR2GRAY );
    namedWindow( "Source",WINDOW_NORMAL );

    Mat canny_output;
    Canny( src_gray, canny_output, thresh, thresh*2 );
    vector<vector<Point> > contours;
    findContours( canny_output, contours, RETR_TREE, CHAIN_APPROX_SIMPLE );
    vector<Vec4i> hierarchy;
    vector<vector<Point> >hull( contours.size() );

    vector<Moments> mu(contours.size() );
    for( int i = 0; i <(int)contours.size(); i++ )
    { mu[i] = moments( contours[i], false ); }

    for( size_t i = 0; i < contours.size(); i++ )
    {
    if(contours[i].size()>20)
    convexHull( contours[i], hull[i] );
    }

    vector<Point2f> mc( contours.size() );
    for( int i = 0; i <(int)contours.size(); i++ )
    { mc[i] = Point2f( mu[i].m10/mu[i].m00 , mu[i].m01/mu[i].m00 ); }

    Mat drawing = Mat::zeros( canny_output.size(), CV_8UC3 );
    int onlyOne = 1;
    for( size_t i = 0; i< contours.size(); i++ )
    {
    if(contours[i].size()>20 && onlyOne)
    {
    circle( src, mc[i], 4, Scalar(0,255,255), -1, 8, 0 );
    Scalar color = Scalar(255,0,0);
    drawContours( drawing, contours, (int)i, color );
    drawContours( src, hull, (int)i, color,5 );

    Point centerMass = mc[i];
    for(int a=0; a<(int)contours[i].size();a++)
    {
    if(cv::norm(cv::Mat(contours[i][a]),Mat(centerMass))<200 && isClose(contours[i][a]))
    {
    circle(src,contours[i][a],5,Scalar(0,0,255),10);
    innerCorners.push_back(contours[i][a]);
    line(src,contours[i][a],centerMass,Scalar(0,255,255),5);
    }
    }

    onlyOne = 0;
    }
    }
    namedWindow( "Hull demo",WINDOW_NORMAL );
    imshow( "Hull demo", drawing );
    imshow("Source", src );


    waitKey();
    return 0;
    }

    bool isClose(Point test){
    if(innerCorners.size()==0)
    return 1;

    for(Point a:innerCorners)
    if((cv::norm(cv::Mat(a),cv::Mat(test)))<70)
    return 0;
    return 1;
    }

    关于algorithm - 将图像对象分成N个相等像素的部分(方法),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63732274/

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