检测方形 OpenCV 但检测到的方形比应有的多
detecting square opencv but square detected more than it should be
我想检测图片的正方形,但是在计算现有正方形的数量时遇到问题。图片中有 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);
我的观点是:
- 为什么我的代码检测到的方块比它应该的多?
- 我该如何解决这个问题?
- 我发现有些人在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 *, ?)'
-
由于内核大小为
medianBlur()
为 9 像素。 -
将内核大小减小到 3 或 5。
-
请参阅 mixChannels() 文档
从正方形.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);
必须预先分配 dst gray0
。在每次迭代中,c
指定的通道将从 timg 复制到 gray0
中。
- 使用CMake检测支持的C++标准
- FFmpeg:制作一个应用程序比直接使用ffmepg更好吗
- 当套接字连接断开时检测C/C++Unix
- C/C++预处理器是否可以检测一些编译器选项
- WMI检测进程创建事件-c++
- 为什么constexpr的性能比正常表达式差
- 基于树莓pi的tensorflow lite量化ssd目标检测
- 下面是我为检测链接列表中的循环而制作的代码
- 落砂模拟碰撞检测C++和SFML
- 为什么std::condition_variable notify_all的工作速度比notify_one快(对于随机请
- 我可以检测和更改 gcc/g++ 中结构的当前数据对齐设置吗?
- 为什么我可以使用比分配的内存更多的内存
- 当使用比格式支持的精度更高的精度来显示数字时,会写出什么数据
- 为什么std::vector比数组慢
- 为什么C++编译器没有检测到正确声明的类?
- 有没有比在库中添加一个并非由所有派生类实现的新虚拟函数更好的设计实践
- 为什么新的随机库比std::rand()更好
- 检测win32服务创建和删除的最佳方法
- 圆形与方形碰撞检测以及需要响应C++ |OpenGL。我的问题就是这个"I need to keep the circle outside the square"
- 检测方形 OpenCV 但检测到的方形比应有的多