0

我想检测图片的正方形,但是在计算现有正方形的数量时遇到了问题。图中有 6 块方格,但有 8 块被检测到,其中 3 块被检测到两次。

这是图像检测的结果 http://i698.photobucket.com/albums/vv347/holybring18/Screenshot_2013-11-01-07-37-17.png

这是预处理 http://i698.photobucket.com/albums/vv347/holybring18/Screenshot_2013-11-01-07-37-49.png

这是我的代码,我从 squares.cpp 示例代码中获得。我在我的 Android 中使用本机语言对此进行了编码,因此有关正方形检测的所有内容都由我的本机代码处理。但是我做了一点修改,因为在 mixChannels() 有一个恼人的错误,我不知道如何修复它。

// jlong from java convert to references to Mat
// for image source and image result
Mat& matsrc = *(Mat*)alamatMatSrc;
Mat& matres = *(Mat*)alamatMatRes;

// needed matrix
Mat gray;
Mat blur;
Mat bw;
Mat dil;
Mat er;

// tempat menyimpan kontur dan estimasi sudut
vector<vector<Point> > squares;
vector<vector<Point> > contours;
vector<Point> approx;

// convert to grayscale
cvtColor(matsrc, gray, CV_BGR2GRAY);

// blur for reducing noise
medianBlur(gray, blur, 9);

// edge detection with Canny
Canny(blur, bw, 0, 50);

// dilate to ensure there is no cut off lines
dilate(bw, dil, Mat(), Point(-1,-1));

// find all contours
findContours(dil, contours, CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE);

// loop to find the squares
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);

    // Note: absolute value of an area is used because
    // area may be positive or negative - in accordance with the
    // contour orientation
    if (approx.size() == 4 && fabs(contourArea(Mat(approx))) > 1000 && isContourConvex(Mat(approx))) {
        double maxCosine = 0;

        for (int j = 2; j < 5; j++) {
            Point pt1 = approx[j%4];
            Point pt2 = approx[j-2];
            Point pt3 = approx[j-1];
            double cosine = fabs(sudut(pt1, pt2, pt3));
            maxCosine = MAX(maxCosine, cosine);
            }
        if (maxCosine < 0.1) squares.push_back(approx);
    }
}

这是调用本机时的我的java代码

// provide Mat for the source and for the result
mymatsrc = new Mat(mybmp.getWidth(), mybmp.getHeight(), CvType.CV_8UC1);
mymatres = new Mat(mybmp.getWidth(), mybmp.getHeight(), CvType.CV_8UC1);

// convert bitmap to Mat, then pass the Mat adress to native, process, and convert it again to bitmap
Utils.bitmapToMat(mybmp, mymatsrc);
Utils.bitmapToMat(mybmp, mymatres);
preProcess(mymatsrc.getNativeObjAddr(), mymatres.getNativeObjAddr());
Utils.matToBitmap(mymatres, mybmp);

// show the processed picture
imageView.setImageBitmap(mybmp);

我的观点是:

  1. 为什么我的代码检测到的平方比它应该的多?
  2. 我该如何解决?
  3. 我发现一些人在 mixChannel () 上遇到错误,我找不到解决方案,有谁知道如何解决这个问题,这是错误消息。

无效参数 ' 候选者是: void mixChannels(const cv::Mat *, ?, cv::Mat *, ?, const int *, ?) void mixChannels(const cv::_InputArray &, const cv::_InputArray &, const std::vector> &) void mixChannels(const std::vector> &, std::vector> &, const int *, ?) '

4

1 回答 1

0
  1. 因为内核大小为medianBlur()9 像素。

  2. 将内核大小减小到 3 或 5。

  3. 请参阅mixChannels() 文档

来自 squares.cpp

// 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);

dstgray0必须预先分配。在每次迭代中,由 指定的通道c从 timg 复制到gray0.

于 2013-11-01T06:53:42.900 回答