将轮廓点映射到原始图像

时间:2017-07-10 21:25:33

标签: python c++ opencv

我已经执行了一些基本的图像操作,并设法隔离了我感兴趣的对象。

结果,我最终得到了一个容器; get<0>(results)持有对象的相应(x, y)。出于视觉目的,我在名为contourFrame

的不同框架上绘制了这些点

我的问题是,如何将这些点映射回原始图像?

我已经研究过findHomography()warpPerspective的组合,但为了找到单应矩阵,我需要提供各自的目标点,这是我首先要寻找的。< / p>

remap()似乎正在寻找我正在寻找的东西,但我认为这是我对API的误解,但在尝试之后,它会返回一个空白的白框。

remap()甚至可以解决这个问题吗?如果有,怎么样?如果不是,我可以使用哪种方法将轮廓点映射到另一个图像?

Python解决方案/建议也适用于我。

编辑我

对于当前场景,其1-1映射回原始图像。但是如果我旋转原始图像怎么办?或者前后移动相机,即将其移近或远离?如何将坐标映射回新定向的原始图像?

原始图片

iPad

结果

Results

左上角: Canny Edge 右上:扩张 左下角:受到侵蚀 右下角:具有所需对象/轮廓的框架

tuple<vector<vector<Point>>, Mat, Mat> removeStupidIcons(Mat& edges)
{
    Mat dilated, eroded;
    vector<vector<Point>> contours, filteredContours;

    dilate(edges, dilated, Mat(), Point(-1, -1), 5);
    erode(dilated, eroded, Mat(), Point(-1, -1), 5);

    findContours(eroded, contours, CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE);

    for(vector<Point>contour: contours)
        if(contourArea(contour) < 200)
            filteredContours.push_back(contour);

    return make_tuple(filteredContours, dilated, eroded);
}

Mat mapPoints(Mat& origin, Mat& destination)
{
    Mat remapWindow, mapX, mapY;

    mapX.create(origin.size(), CV_32FC1);
    mapY.create(origin.size(), CV_32FC1);

    remap(origin, destination, mapX, mapY, CV_INTER_LINEAR);

    return destination;
}

int main(int argc, const char * argv[])
{
    string image_path = "ipad.jpg";

    original = imread(image_path);

    blur(original, originalBlur, Size(15, 15));
    cvtColor(originalBlur, originalGrey, CV_RGB2GRAY);

    Canny(originalGrey, cannyEdges, 50, 130, 3);
    cannyEdges.convertTo(cannyFrame, CV_8U);

    tuple<vector<vector<Point>>, Mat, Mat> results = removeStupidIcons(cannyEdges);
//  
//  get<0>(results) -> contours
//  get<1>(results) -> matrix after dilation
//  get<2>(results) -> matrix after erosion

    Mat contourFrame = Mat::zeros(original.size(), CV_8UC3);

    Scalar colour = Scalar(rand()&255, rand()&255, rand()&255);

    drawContours(contourFrame, get<0>(results), -1, colour, 3);

    Mat contourCopy, originalCopy;

    original.copyTo(originalCopy); contourFrame.copyTo(contourCopy);

    // a white background is returned.
    Mat mappedFrame = mapPoints(originalCopy, contourCopy);

    imshow("Canny", cannyFrame);
    imshow("Original", original);
    imshow("Contour", contourFrame);
    imshow("Eroded", get<2>(results));
    imshow("Dilated", get<1>(results));
    imshow("Original Grey", originalGrey);
    imshow("Mapping Result", contourCopy);

    waitKey(0);

    return 0;
}

0 个答案:

没有答案
相关问题