Python OpenCV:如何对角分割图像

问题描述 投票:0回答:1

我有如下图像。我想将它们对角地拆分,可能使用图像的质心,或者获取边界矩形/椭圆的角度并沿其切割。我可以使用列表切片水平或垂直剪切图像:

eg img_split=img[:50%,:]

我该如何在OpenCV Python / Numpy中对角线化?

图像:

enter image description here

enter image description here

拆分样本:

enter image description here

enter image description here

python numpy opencv contour
1个回答
0
投票

[如果只想剪切,可以使用fillPoly功能来帮助剪切任何多边形。在下面的代码中,我自己给出了要点。

注:如果要让程序检测到这些点,则对于第一张图像来说很容易,因为对象具有不同的颜色。但是对于第二张图片,我不知道该怎么办。

[[Note:我的环境基于C ++,这就是为什么我使用它,但是它应该很容易转换。

结果:

enter image description here

代码:

#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgcodecs.hpp"
#include "opencv2/imgproc/imgproc.hpp"

using namespace cv;

int main()
{


    cv::Mat img = cv::imread("/ur/image/directory/image.png");
    imshow("original",img);

    cv::Point corners[1][4];
    corners[0][0] = Point( 0, 0 );
    corners[0][1] = Point( img.cols/2, img.rows/2 );
    corners[0][2] = Point( img.cols, img.rows );
    corners[0][3] = Point( img.cols, 0 );

    const Point* corner_list[1] = { corners[0] };

    int num_points = 4;
    int num_polygons = 1;
    int line_type = 8;

    cv::Mat mask = cv::Mat::zeros(img.rows, img.cols, CV_8UC1);
    cv::fillPoly( mask, corner_list, &num_points, num_polygons, cv::Scalar( 255, 255, 255 ),  line_type);
    cv::Mat result(img.size(), img.type(), cv::Scalar(255, 255, 255));
    img.copyTo(result, mask);

    imshow("Output",result);
    cv::waitKey(0);
    return 0;
}
© www.soinside.com 2019 - 2024. All rights reserved.