简体   繁体   English

OpenCV - 使用C ++从图像中裁剪非矩形区域

[英]OpenCV - Cropping non rectangular region from image using C++

How can I crop a non rectangular region from image? 如何从图像中裁剪非矩形区域?

Imagine I have four points and I want to crop it, this shape wouldn't be a triangle somehow! 想象一下,我有四个点,我想裁剪它,这个形状不会是一个三角形!

For example I have the following image : 例如,我有以下图像:

在此输入图像描述

and I want to crop this from image : 我想从图像中裁剪出来:

在此输入图像描述

How can I do this? 我怎样才能做到这一点? regards.. 问候..

The procedure for cropping an arbitrary quadrilateral (or any polygon for that matter) part of an image is summed us as: 裁剪图像的任意四边形(或任何多边形)部分的过程总结为:

  • Generate a "mask". 生成“面具”。 The mask is black where you want to keep the image, and white where you don't want to keep it 面具是黑色的,你想要保留图像,而白色则是你不想保留它的地方
  • Compute the "bitwise_and" between your input image and the mask 计算输入图像和蒙版之间的“bitwise_and”

So, lets assume you have an image. 所以,假设你有一个图像。 Throughout this I'll use an image size of 30x30 for simplicity, you can change this to suit your use case. 在整个过程中,为了简单起见,我将使用30x30的图像大小,您可以根据自己的使用情况进行更改。

cv::Mat source_image = cv::imread("filename.txt");

And you have four points you want to use as the corners: 你有4个点想要用作角落:

cv::Point corners[1][4];
corners[0][0] = Point( 10, 10 );
corners[0][1] = Point( 20, 20 );
corners[0][2] = Point( 30, 10 );
corners[0][3] = Point( 20, 10 );
const Point* corner_list[1] = { corners[0] };

You can use the function cv::fillPoly to draw this shape on a mask: 您可以使用函数cv::fillPoly在蒙版上绘制此形状:

int num_points = 4;
int num_polygons = 1;
int line_type = 8;
cv::Mat mask(30,30,CV_8UC3, cv::Scalar(0,0,0));
cv::fillPoly( mask, corner_list, &num_points, num_polygons, cv::Scalar( 255, 255, 255 ),  line_type);

Then simply compute the bitwise_and of the image and mask: 然后简单地计算图像的bitwise_and和掩码:

cv::Mat result;
cv::bitwise_and(source_image, mask, result);

result now has the cropped image in it. result现在有裁剪的图像。 If you want the edges to end up white instead of black you could instead do: 如果你想让边缘最终变成白色而不是黑色,你可以改为:

cv::Mat result_white(30,30,CV_8UC3, cv::Scalar(255,255,255));
cv::bitwise_and(source_image, mask, result_white, mask);

In this case we use bitwise_and 's mask parameter to only do the bitwise_and inside the mask. 在这种情况下,我们使用bitwise_and的mask参数来仅执行掩码内的bitwise_and。 See this tutorial for more information and links to all the functions I mentioned. 有关我提到的所有功能的更多信息和链接,请参阅本教程

You may use cv::Mat::copyTo() like this: 您可以像这样使用cv :: Mat :: copyTo():

cv::Mat img = cv::imread("image.jpeg");
// note mask may be single channel, even if img is multichannel
cv::Mat mask = cv::Mat::zeros(img.rows, img.cols, CV_8UC1);
// fill mask with nonzero values, e.g. as Tim suggests
// cv::fillPoly(...)
cv::Mat result(img.size(), img.type(), cv::Scalar(255, 255, 255));
img.copyTo(result, mask);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM