从图像中删除黑色背景。

时间:2016-12-08 10:16:57

标签: c++ image opencv

我是图像处理和开发的新手。我使用过opencv,我需要从给定的图像中提取圆圈。给出x,y坐标的那个圆是奥德的(半径)来做我用下面的代码。但我的问题是我必须采取黑色矩形。因此图像补丁具有不需要的黑色像素。如何只保存圈子?

我的代码

double save_key_points(Mat3b img, double x, double y, double radius, string 

    filename, string foldername)
    {

        // print image height and width first and check.
        Vec3f circ(x, y, radius);

// Draw the mask: white circle on black background
Mat1b mask(img.size(), uchar(0));

circle(mask, Point(circ[0], circ[1]), circ[2], Scalar(255), CV_FILLED);


// Compute the bounding box
Rect bbox(circ[0] - circ[2], circ[1] - circ[2], 2 * circ[2], 2 * circ[2]);

// Create a black image
Mat3b res(img.size(), Vec3b(0, 0, 0));

// Copy only the image under the white circle to black image
img.copyTo(res, mask);

// Crop according to the roi
res = res(bbox);

//remove black but doesn't work.
Mat tmp, alpha;
threshold(res, alpha, 100, 255, THRESH_BINARY);
        // Save the image
        string path = "C:\\Users\\bb\\Desktop\\test_results\\test_case8\\" + foldername + filename + ".png";

        imwrite(path, res);
        Mat keypointimg = imread(path, CV_LOAD_IMAGE_GRAYSCALE);

        //print the cordinate of one patch.
        cordinate_print(keypointimg, radius);

    }  

(这里我想要没有黑色背景) Out put Patch

1 个答案:

答案 0 :(得分:1)

如果我理解你的问题,你可以从图像中删除黑色,你可以使用面具。面具可以突出显示某种颜色的任何东西,或者在您的情况下突出显示黑色的阴影。查看此实现的链接,看看它是否是您的目标。它是在python中,但可以很容易地适应。

Image Filtering