我是图像处理和开发的新手。我在黑色背景中有一个三角形。我想将该三角形保存为没有黑色像素[0]的Mat对象。为了做到这一点,我尝试如下。
我的代码请在下面找到。
Mat finalImage = imread("test.png, CV_LOAD_IMAGE_GRAYSCALE);
img.copyTo(finalImage, mask);
Mat canny_output;
vector<vector<Point> > contours;
vector<Vec4i> hierarchy;
int thresh = 100;
int max_thresh = 255;
RNG rng(12345);
/// Detect edges using canny
Canny(finalImage, canny_output, thresh, thresh * 2, 3);
/// Find contours
findContours(canny_output, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0)); //find this method
/// Draw contours
Mat drawing = Mat::zeros(canny_output.size(), CV_8UC1);
for (int i = 0; i< contours.size(); i++)
{
Scalar color = Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255));
drawContours(drawing, contours, i, color, 2, 8, hierarchy, 0, Point()); // find this method.
}
我有轮廓点但是通过使用轮廓点我不知道如何仅裁剪输入图像中的 。
答案 0 :(得分:0)
在绘制轮廓的同时,可以获得各种轮廓的边界矩形。因此,在迭代轮廓的Your循环中,您可以使用cv::boundingRect()
来获取相应轮廓的边界矩形:
/// Draw contours
Mat drawing = Mat::zeros(canny_output.size(), CV_8UC1);
for (int i = 0; i< contours.size(); i++)
{
Scalar color = Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255));
drawContours(drawing, contours, i, color, 2, 8, hierarchy, 0, Point()); // find this method.
cv::Rect boundingRect = cv::boundingRect(contours[i]);
}