进行方检测。问题出在辐射地板上。你可以看到照片。 解决这个问题的任何想法?
谢谢。源图片:
输出:
源代码:
void EdgeDetection :: find_squares(const cv :: Mat& image, 向量>&广场,CV ::垫和放大器; outputFrame){
unsigned long imageSize = (long) (image.rows * image.cols) / 1000; if (imageSize > 1200) RESIZE = 9; else if (imageSize > 600) RESIZE = 5; else if (imageSize > 300) RESIZE = 3; else RESIZE = 1; Mat src(Size(image.cols / RESIZE, image.rows / RESIZE),CV_YUV420sp2BGR); // Resize src to img size resize(image, src, src.size() ,0.5, 0.5, INTER_LINEAR);
Mat imgeorj = image; const int N = 10; // 11; Mat pyr,timg,gray0(src.size(),CV_8U),灰色;
// down-scale and upscale the image to filter out the noise pyrDown(src, pyr, Size(src.cols / 2, src.rows / 2)); pyrUp(pyr, timg, src.size());
#ifdef blured
Mat blurred(src); medianBlur(src, blurred, 15);
#ENDIF
vector<vector<Point> > contours; // find squares in every color plane of the image for ( int c = 0; c < 3; ++c) { int ch[] = {c, 0}; mixChannels(&timg, 1, &gray0, 1, ch, 1); // try several threshold levels for ( int l = 0; l < N; ++l) { // hack: use Canny instead of zero threshold level. // Canny helps to catch squares with gradient shading if (l == 0) { // apply Canny. Take the upper threshold from slider // and set the lower to 0 (which forces edges merging) // Canny(gray0, gray, 0, thresh, 5); // Canny(gray0, gray, (10+l), (10+l)*3, 3); Canny(gray0, gray,50, 200, 3 ); // dilate canny output to remove potential // holes between edge segments dilate(gray, gray, Mat(), Point(-1, -1)); //erode(gray, gray, Mat(), Point(-1, -1), 1); } else { // apply threshold if l!=0: // tgray(x,y) = gray(x,y) < (l+1)*255/N ? 255 : 0 gray = gray0 >= (l + 1) * 255 / N; } // find contours and store them all as a list findContours(gray, contours, CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE); vector<Point> approx; // test each contour for (size_t i = 0; i < contours.size(); ++i) { // approximate contour with accuracy proportional // to the contour perimeter approxPolyDP(Mat(contours[i]), approx, arcLength(Mat(contours[i]), true) * 0.02, true); if (approx.size() == 4 && fabs(contourArea(Mat(approx))) > 5000 && isContourConvex(Mat(approx))) { float maxCosine = 0; for (register int j = 2; j < 5; ++j) { // find the maximum cosine of the angle between joint edges float cosine = fabs(angle(approx[j%4], approx[j-2], approx[j-1])); maxCosine = MAX(maxCosine, cosine); } // if cosines of all angles are small // (all angles are ~90 degree) then write quandrange // vertices to resultant sequence if (maxCosine < 0.3) { squares.push_back(approx); } } } } } debugSquares(squares, imgeorj,outputFrame); }
答案 0 :(得分:0)
您可以尝试使用Hough变换检测直边,并使用它们构建正方形。