Примеры с cv2.imshow требуют сборки OpenCV с GUI. В ноутбуке с headless-версией используйте вывод изображений через Matplotlib.
Information Flow Alpha Matting
This project was part of Google Summer of Code 2019.
Student: Muskaan Kularia
Mentor: Sunita Nayak
Alphamatting is the problem of extracting the foreground with soft boundaries from a background image. The extracted foreground can be used for further operations like changing the background in an image.
Given an input image and its corresponding trimap, we try to extract the foreground from the background. Following is an example:
Input Image:
Input image should be preferably a RGB image.
Input Trimap:
The trimap image is a greyscale image that contains information about the foreground(white pixels), background(black pixels) and unknown(grey) pixels.
Output alpha Matte:
The computed alpha matte is saved as a greyscale image where the pixel values indicate the opacity of the extracted foreground object. These opacity values can be used to blend the foreground object into a diffferent backgound, as shown below:
Following are some more results.
The first column is input RGB image, the second column is input trimap, third column is the extracted alpha matte and the last two columns show the foreground object blended on new backgrounds.
This project is implementation of [9] . It also required implementation of parts of other papers [2,3,4].
This module uses the Eigen package.
Build the sample code of the alphamat module using the following two cmake commands run inside the build folder:
// This file is part of OpenCV project.// It is subject to the license terms in the LICENSE file found in the top-level directory// of this distribution and at http://opencv.org/license.html.// Include relevant headers
#include <iostream>
#include"opencv2/highgui.hpp"
#include <opencv2/core.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/alphamat.hpp>
// Set namespacesusingnamespacestd;
usingnamespacecv;
usingnamespacecv::alphamat;
// Set the usage parameter namesconstchar* keys =
"{img || input image name}""{tri || input trimap image name}""{out || output image name}""{help h || print help message}"
;
intmain(intargc, char* argv[])
{
CommandLineParserparser(argc, argv, keys);
parser.about("This sample demonstrates Information Flow Alpha Matting");
if (parser.has("help"))
{
parser.printMessage();
return0;
}
// Read the paths to the input image, input trimap and the location of the output image.stringimg_path = parser.get<std::string>("img");
stringtrimap_path = parser.get<std::string>("tri");
stringresult_path = parser.get<std::string>("out");
// Make sure the user inputs paths to the input image and trimapif (!parser.check()
|| img_path.empty() || trimap_path.empty())
{
parser.printMessage();
parser.printErrors();
return1;
}
Matimage, tmap;
// Read the input imageimage = imread(img_path, IMREAD_COLOR);
if (image.empty())
{
printf("Cannot read image file: '%s'\n", img_path.c_str());
return1;
}
// Read the trimaptmap = imread(trimap_path, IMREAD_GRAYSCALE);
if (tmap.empty())
{
printf("Cannot read trimap file: '%s'\n", trimap_path.c_str());
return1;
}
Matresult;
// Perform information flow alpha mattinginfoFlow(image, tmap, result);
if (result_path.empty())
{
// Show the alpha matte if a result filepath is not provided.namedWindow("result alpha matte", WINDOW_NORMAL);
imshow("result alpha matte", result);
waitKey(0);
}
else
{
// Save the alphamatteimwrite(result_path, result);
printf("Result saved: '%s'\n", result_path.c_str());
}
return0;
}