Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python OpenCV中的方向检测有问题_Python_Opencv_Orientation_Object Detection_Canny Operator - Fatal编程技术网

Python OpenCV中的方向检测有问题

Python OpenCV中的方向检测有问题,python,opencv,orientation,object-detection,canny-operator,Python,Opencv,Orientation,Object Detection,Canny Operator,我正在尝试制作一个可以检测物体方向的计算机视觉脚本。它在大多数情况下都有效,但对于某些图像,它似乎无法获得同样的成功 该脚本依靠模糊和精明的边缘检测来找到轮廓 工作示例: 失败的部分: 对于失败的部分,它会为同一形状中的一个创建两行,并完全忽略其他形状中的一个 主要代码: import cv2 from imgops import imutils import CVAlgo z = 'am' path = 'images/pca.jpg' #path = 'images/pca2.jp

我正在尝试制作一个可以检测物体方向的计算机视觉脚本。它在大多数情况下都有效,但对于某些图像,它似乎无法获得同样的成功

该脚本依靠模糊和精明的边缘检测来找到轮廓

工作示例:

失败的部分:

对于失败的部分,它会为同一形状中的一个创建两行,并完全忽略其他形状中的一个

主要代码:

import cv2
from imgops import imutils
import CVAlgo

z = 'am'

path = 'images/pca.jpg'
#path = 'images/pca2.jpg'

img = cv2.imread(path)
imgray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

img = imutils.resize(img, height = 600)
imgray = imutils.resize(img, height = 600)

final = img.copy()

thresh, imgray = CVAlgo.filtering(img, imgray, z)


__ , contours, hierarchy = cv2.findContours(thresh.copy(),cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)

# Iterate through all contours
test = CVAlgo.cnt_gui(final, contours)

#cv2.imwrite('1.jpg', final)

cv2.imshow('thresh', thresh)
cv2.imshow('contours', final)
cv2.waitKey(0)
CVAlgo.py

import cv2
from numpy import *
from pylab import *
from imgops import imutils
import math

def invert_img(img):
    img = (255-img)
    return img


def canny(imgray):
    imgray = cv2.GaussianBlur(imgray, (11,11), 200)
    canny_low = 0
    canny_high = 100

    thresh = cv2.Canny(imgray,canny_low,canny_high)
    return thresh

def cnt_gui(img, contours):
    cnts = sorted(contours, key = cv2.contourArea, reverse = True)

    for i in range(0,len(cnts)):
        sel_cnts = sorted(contours, key = cv2.contourArea, reverse = True)[i]

        area = cv2.contourArea(sel_cnts)

        if area < 1000:
            continue

        # get orientation angle and center coord
        center, axis,angle = cv2.fitEllipse(sel_cnts)

        hyp = 100  # length of the orientation line

        # Find out coordinates of 2nd point if given length of line and center coord 
        linex = int(center[0]) + int(math.sin(math.radians(angle))*hyp)
        liney = int(center[1]) - int(math.cos(math.radians(angle))*hyp)

        # Draw orienation
        cv2.line(img, (int(center[0]),int(center[1])), (linex, liney), (0,0,255),5)             
        cv2.circle(img, (int(center[0]), int(center[1])), 10, (255,0,0), -1)

    return img

def filtering(img, imgray, mode):
    imgray = cv2.medianBlur(imgray, 11)
    thresh = cv2.Canny(imgray,75,200)

    return thresh, imgray
导入cv2
从numpy进口*
从派拉布进口*
从imgops导入IMUTIL
输入数学
def反转img(img):
img=(255 img)
返回img
def canny(灰色):
imgray=cv2.GaussianBlur(imgray,(11,11),200)
canny_低=0
canny_high=100
阈值=cv2.Canny(灰色,Canny低,Canny高)
回程脱粒
def cnt_gui(图像、轮廓):
cnts=已排序(轮廓,关键点=cv2.contourArea,反向=真)
对于范围(0,len(cnts))内的i:
sel_cnts=已排序(轮廓,键=cv2.contourArea,反转=真)[i]
面积=cv2。轮廓面积(选择)
如果面积小于1000:
持续
#获取方向角和中心坐标
中心、轴、角度=cv2.椭圆(选择)
hyp=100#定向线的长度
#如果给定直线长度和中心坐标,找出第二点的坐标
linex=int(中心[0])+int(数学正弦(数学弧度(角度))*hyp)
liney=int(中心[1])-int(数学坐标系(数学弧度(角度))*hyp)
#绘画定向
cv2.直线(img,(int(center[0]),int(center[1]),(linex,liney),(0,0255),5)
cv2.圆(img,(int(center[0]),int(center[1])),10,(255,0,0),-1)
返回img
def过滤(img、imgray、模式):
imgray=cv2.medianBlur(imgray,11)
thresh=cv2.Canny(imgray,75200)
返回阈值,imgray

有人知道问题出在哪里吗?有人知道我如何改进这个脚本吗

未检测到的形状太接近黑色背景,因此其轮廓已与白色对象区域的轮廓合并。在其中一个对象中找到的第二个方向实际上是外轮廓的方向。为了避免这种情况,您可以在使用cv2.Displate函数进行阈值化后,从以下位置放大或关闭二值图像:

  • 我有一个建议。因为您已经提取了中的每个对象 将图像作为轮廓,尝试将椭圆拟合到每个轮廓

  • 然后找到每个椭圆的长轴

  • 现在求出这些长轴的方向角


  • 你介意给出一个示例代码吗?我对实施情况没有把握。