我試圖沿接縫分割兩個圖像,然後將它們混合在一起。在這個過程中,我需要通過應用一個蒙版來沿着接縫剪下每個圖像。我如何使用面膜?我嘗試了bitwise_and
和multiply
這個蒙版和圖片,但都沒有奏效。在opencv中掩蓋圖像
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](http://docs.opencv.org/modules/core/doc/basic_structures.html#mat-copyto)使用上面的掩碼,也可以看到答案[here](http:/ /stackoverflow.com/questions/22315904/blending-does-not-remove-seams-in-opencv)可能會有所幫助。 – Haris
你可能會發現[這個答案](http://stackoverflow.com/a/20288616/176769)有趣。 – karlphillip