如何使用openCV使用四个角点裁剪图像?
Posted
技术标签:
【中文标题】如何使用openCV使用四个角点裁剪图像?【英文标题】:How to crop image using four corner point using openCV? 【发布时间】:2014-01-31 10:45:35 【问题描述】:我正在使用 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)
【问题讨论】:
嘿,你怎么能得到这些正方形的CGPoint,这样我们就可以绘制uiview pointfro裁剪 【参考方案1】:假设你的四个端点是 x1,y1,x2,y2
然后使用代码
src.copyTo(dst(Rect(x1,y1,x1+x2,y1+y2));
【讨论】:
【参考方案2】:使用 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);
希望这会有所帮助。
【讨论】:
【参考方案3】:$ c++filt __Z9getCenterNSt3__16vectorIN2cv6Point_IiEENS_9allocatorIS3_EEEE
getCenter(std::__1::vector, std::__1::allocator >>)
$ c++filt __Z25sortSquarePointsClockwiseNSt3__16vectorIN2cv6Point_IiEENS_9allocatorIS3_EEEE
sortSquarePointsClockwise(std::__1::vector, std::__1::allocator > >)
这是一个链接错误。它建议您将上述每个函数定义两次。
【讨论】:
以上是关于如何使用openCV使用四个角点裁剪图像?的主要内容,如果未能解决你的问题,请参考以下文章