【发布时间】:2017-04-29 06:32:53
【问题描述】:
我是图像处理和开发的新手。我有一个黑色背景的三角形。我想将该三角形保存为没有黑色像素 [0] 的 Mat 对象。为了做到这一点,我尝试如下。
- 设置阈值
- 寻找轮廓
- 将轮廓 [0] 识别为三角形 // 有 2 个轮廓,一个是三角形,另一个是背面像素。
- 保存轮廓点
- 裁剪图像。
我的代码请在下面找到。
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.
}
我有轮廓点,但通过使用轮廓点,我不知道如何仅裁剪 输入图像中的三角形。
【问题讨论】:
-
这个问题你已经问过几次了。 1) 图像必须是矩形,因此您不能拥有三角形图像,有什么不清楚的地方? 2)在矩形图像中,可以使用alpha通道作为遮罩,例如设置透明三角形外的所有像素?
-
三木是对的,图片不能是三角形的。请详细说明您到底需要什么。