我在我的测试应用程序中成功实现了OpenCV平方检测示例,但现在需要过滤输出,因为它非常混乱 - 或者我的代码是错误的?
我对本文的四个角点感兴趣,以减少偏斜(如此)和进一步处理......
原始图片:
码:
double angle( cv::Point pt1, cv::Point pt2, cv::Point pt0 ) {
double dx1 = pt1.x - pt0.x;
double dy1 = pt1.y - pt0.y;
double dx2 = pt2.x - pt0.x;
double dy2 = pt2.y - pt0.y;
return (dx1*dx2 + dy1*dy2)/sqrt((dx1*dx1 + dy1*dy1)*(dx2*dx2 + dy2*dy2) + 1e-10);
}
- (std::vector<std::vector<cv::Point> >)findSquaresInImage:(cv::Mat)_image
{
std::vector<std::vector<cv::Point> > squares;
cv::Mat pyr, timg, gray0(_image.size(), CV_8U), gray;
int thresh = 50, N = 11;
cv::pyrDown(_image, pyr, cv::Size(_image.cols/2, _image.rows/2));
cv::pyrUp(pyr, …Run Code Online (Sandbox Code Playgroud) 前一段时间我问了一个关于方形检测的问题,karlphillip提出了一个不错的结果.
现在我想更进一步,找到边缘不完全可见的方块.看看这个例子:
有任何想法吗?我正在使用karlphillips代码:
void find_squares(Mat& image, vector<vector<Point> >& squares)
{
// blur will enhance edge detection
Mat blurred(image);
medianBlur(image, blurred, 9);
Mat gray0(blurred.size(), CV_8U), gray;
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(&blurred, 1, &gray0, 1, ch, 1);
// try several threshold levels
const int threshold_level = 2;
for (int l = 0; …Run Code Online (Sandbox Code Playgroud) 我使用的程序squares.c的OpenCV库的样本中找到.它适用于每个图像,但我真的无法弄清楚为什么它不能识别该图像中绘制的正方形
http://desmond.imageshack.us/Himg12/scaled.php?server=12&filename=26725680.jpg&res=medium
CANNY之后:http: //img847.imageshack.us/img847/6787/canny.jpg
在DILATE之后:http: //img593.imageshack.us/img593/3010/dilate.jpg
在RESULT图像(红色) http://img267.imageshack.us/img267/8016/resultuq.jpg
如您所见,未检测到方形.
在检测之后,我需要提取广场中包含的区域......没有投资回报率怎么可能?
我想提取图像的轮廓,表示为一系列点坐标.
随着Canny我能够产生包含图像的只有边缘的二值图像.然后,我试图findContours用来提取轮廓.但结果并不好.
对于每个边缘,我经常得到2条线,就像它被认为是一个非常薄的区域.我想简化我的轮廓,以便我可以将它们画成单行.或者可以用不同的函数提取它们,直接产生正确的结果会更好.
我看了一下OpenCV的文档,但我找不到任何有用的东西,但我想我不是第一个遇到类似问题的人.我可以使用任何功能或方法吗?
这是我到目前为止编写的Python代码:
def main():
img = cv2.imread("lena-mono.png", 0)
if img is None:
raise Exception("Error while loading the image")
canny_img = cv2.Canny(img, 80, 150)
contours, hierarchy = cv2.findContours(canny_img, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
contours_img = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
scale = 10
contours_img = cv2.resize(contours_img, (0, 0), fx=scale, fy=scale)
for cnt in contours:
color = np.random.randint(0, 255, (3)).tolist()
cv2.drawContours(contours_img,[cnt*scale], 0, color, 1)
cv2.imwrite("canny.png", canny_img)
cv2.imwrite("contours.png", contours_img)
Run Code Online (Sandbox Code Playgroud)
比例因子用于突出轮廓的双线.以下是图片的链接:
任何建议将不胜感激.
opencv feature-extraction contour computer-vision edge-detection