【发布时间】:2014-04-27 23:13:20
【问题描述】:
我正在尝试沿接缝拆分两个图像,然后将它们混合在一起。在这个过程中,我需要通过应用蒙版沿接缝剪切每个图像。我该如何敷面膜?我尝试了bitwise_and 和multiplying 蒙版和图像,但都没有成功。
int pano_width = left_template_width + right_template_width - roi_width;
// add zeros to the right of the left template
Mat full_left = Mat::zeros(roi_height, pano_width, CV_32FC3);
Mat tmp_l = full_left(Rect(0,0, left_template_width, roi_height));
imshow("Scene mask", mask0f3);
imshow("Cropped scene", cropped_scene);
Mat left_masked;
//bitwise_and(cropped_scene, mask0f3, left_masked); // full_left looks all black
multiply(cropped_scene, mask0f3, left_masked); // full_left looks like the scene mask, but with an extra black rectangle on the right side
left_masked.copyTo(tmp_l);
imshow("Full left", full_left);
我采用了一种非常高效但有效的破解方法:
void apply_mask(Mat& img, Mat mask) {
CV_Assert(img.rows == mask.rows);
CV_Assert(img.cols == mask.cols);
print_mat_type(img);
print_mat_type(mask);
for (int r = 0; r < mask.rows; r++) {
for (int c = 0; c < mask.cols; c++) {
if (mask.at<uchar>(r, c) == 0) {
img.at<Vec3f>(r, c) = Vec3f(0, 0, 0);
}
}
}
}
【问题讨论】:
-
使用Mat::copyTo 使用上面的掩码,另请参阅答案here 可能会有所帮助。
-
你可能会觉得this answer很有趣。