如何使用openCV使用四个角点裁剪图像?

时间:2014-01-31 10:45:36

标签: ios iphone opencv

我使用此code获取图像的四个角:

cv::Mat CVSquares::detectedSquaresInImage (cv::Mat image, float tol, int threshold, int levels, int acc)
{
    vector<vector<Point> > squares;

    if( image.empty() )
        {
        cout << "CVSquares.m: Couldn't load " << endl;
        }

    tolerance = tol;
    thresh = threshold;
    N = levels;
    accuracy = acc;
    findSquares(image, squares);
    //drawSquares(image, squares);

    // The largest of them probably represents the paper
    vector<Point> largest_square;
    find_largest_square(squares, largest_square);
    drawSquares(image, largest_square);

    // Print the x,y coordinates of the square
    cout << "Point 1: " << largest_square[0] << endl;
    cout << "Point 2: " << largest_square[1] << endl;
    cout << "Point 3: " << largest_square[2] << endl;
    cout << "Point 4: " << largest_square[3] << endl;

    return image;
}

它给了我四分,但现在我想把图像裁剪到那一点。有人可以帮我这个吗?

我尝试使用this answer

中的代码

但它的显示错误如下。

duplicate symbol __Z9getCenterNSt3__16vectorIN2cv6Point_IiEENS_9allocatorIS3_EEEE in:
    /Users/user/Library/Developer/Xcode/DerivedData/OpenCVSquares-gbnzjrefuxhjlchibreeqqdoaiqq/Build/Intermediates/OpenCVSquares.build/Debug-iphonesimulator/OpenCVSquares.build/Objects-normal/i386/UIImage+OpenCV.o
    /Users/user/Library/Developer/Xcode/DerivedData/OpenCVSquares-gbnzjrefuxhjlchibreeqqdoaiqq/Build/Intermediates/OpenCVSquares.build/Debug-iphonesimulator/OpenCVSquares.build/Objects-normal/i386/CVSquares.o
duplicate symbol __Z25sortSquarePointsClockwiseNSt3__16vectorIN2cv6Point_IiEENS_9allocatorIS3_EEEE in:
    /Users/user/Library/Developer/Xcode/DerivedData/OpenCVSquares-gbnzjrefuxhjlchibreeqqdoaiqq/Build/Intermediates/OpenCVSquares.build/Debug-iphonesimulator/OpenCVSquares.build/Objects-normal/i386/UIImage+OpenCV.o
    /Users/user/Library/Developer/Xcode/DerivedData/OpenCVSquares-gbnzjrefuxhjlchibreeqqdoaiqq/Build/Intermediates/OpenCVSquares.build/Debug-iphonesimulator/OpenCVSquares.build/Objects-normal/i386/CVSquares.o
ld: 2 duplicate symbols for architecture i386
clang: error: linker command failed with exit code 1 (use -v to see invocation)

3 个答案:

答案 0 :(得分:0)

假设您的四个终点是x1,y1,x2,y2

然后使用代码

src.copyTo(dst(Rect(x1,y1,x1+x2,y1+y2));

答案 1 :(得分:0)

使用copyTo加上ROI rect是可以的,示例代码如下:

Mat srcImg = imread('sample.jpg'), dstImg;
Rect rect = Rect( x, y, width, height ); // ROI rect in srcImg
// In your case, x=x1, y=y1, width=x2-x1, height=y2-y1; 
// (x1, y1) is the up-left corner, (x2, y2) is the bottom-right corner
srcImg(rect).copyTo(dstImg);  

希望这会有所帮助。

答案 2 :(得分:0)

$ c++filt __Z9getCenterNSt3__16vectorIN2cv6Point_IiEENS_9allocatorIS3_EEEE

getCenter(std :: __ 1 :: vector,std :: __ 1 :: allocator&gt;&gt;)

$ c++filt __Z25sortSquarePointsClockwiseNSt3__16vectorIN2cv6Point_IiEENS_9allocatorIS3_EEEE

sortSquarePointsClockwise(std :: __ 1 :: vector,std :: __ 1 :: allocator&gt;&gt;)

这是一个链接错误。它建议您定义上述每个函数两次。