【问题标题】:Shape detection in specific area of image open cv python图像opencv python特定区域的形状检测
【发布时间】:2019-04-04 20:11:33
【问题描述】:

我正在拍摄旧的超 8 毫米胶片的静止图像,并且想要检测图像左侧的主轴孔。有时图像中有多个孔。我想用python用opencv检测图像中心的洞。

我玩过标准的 opencv 检测方形 python 脚本,我可以检测到漏洞。但是我很难找到一种方法来限制对图像中心孔的搜索。

我希望能够检测中心孔并根据该孔的位置裁剪图像。

以下是我正在使用的基本脚本。对我可以开始寻求解决此问题的任何帮助表示感谢。


#!/usr/bin/env python

'''
Simple "Square Detector" program.
Loads several images sequentially and tries to find squares in each image.
'''

# Python 2/3 compatibility
from __future__ import print_function
import sys
PY3 = sys.version_info[0] == 3

if PY3:
    xrange = range

import numpy as np
import cv2 as cv


def angle_cos(p0, p1, p2):
    d1, d2 = (p0-p1).astype('float'), (p2-p1).astype('float')
    return abs( np.dot(d1, d2) / np.sqrt( np.dot(d1, d1)*np.dot(d2, d2) ) )

def find_squares(img):
    squares = []
    for gray in cv.split(img):
        for thrs in xrange(0, 255, 26):
            if thrs == 0:
                bin = cv.Canny(gray, 0, 50, apertureSize=5)
                bin = cv.dilate(bin, None)
            else:
                _retval, bin = cv.threshold(gray, thrs, 255, cv.THRESH_BINARY)
            contours, _hierarchy = cv.findContours(bin, cv.RETR_LIST, cv.CHAIN_APPROX_SIMPLE)
            for cnt in contours:
                cnt_len = cv.arcLength(cnt, True)
                cnt = cv.approxPolyDP(cnt, 0.02*cnt_len, True)
                if len(cnt) == 4 and cv.contourArea(cnt) > 1000 and cv.isContourConvex(cnt):
                    cnt = cnt.reshape(-1, 2)
                    max_cos = np.max([angle_cos( cnt[i], cnt[(i+1) % 4], cnt[(i+2) % 4] ) for i in xrange(4)])
                    if max_cos < 0.1:
                        squares.append(cnt)
    return squares

def main():
    from glob import glob
    for fn in glob('/home/trent/Pictures/image0001.jpg'):
        img = cv.imread(fn)
        squares = find_squares(img)
        cv.drawContours( img, squares, -1, (255, 0, 0), 3 )
        cv.imshow('squares', img)
        ch = cv.waitKey()
        if ch == 27:
            break

    print('Done')


if __name__ == '__main__':
    print(__doc__)
    main()
cv.destroyAllWindows()

【问题讨论】:

标签: python algorithm opencv


【解决方案1】:

这是一个粗略的工作然后找到中心 使用 DIY 过滤“return_important_contours”,然后使用 cv2.pointPolygonTest 查找到每个中心的距离。您可能可以一起取消过滤。

return_contour_nearest_center_function(image):
 rows, cols, ch = image.shape
 middle=(rows/2,cols/2)

 selected_contours=return_important_contours(image,50,0.5,cv2.CHAIN_APPROX_NONE)
 min_distance=1000000
 min_distace_tag=0
 for i in range(0,selected_contours.__len__()):
    dist = abs(cv2.pointPolygonTest(selected_contours[i], (cols/2, rows/2), True))
  #  print("each distance"+ str(dist) )
    if dist<min_distance:
        min_distance=dist
        min_distace_tag=i

 if selected_contours.__len__()==0:
    return ;
 return selected_contours[min_distace_tag];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-21
    • 2019-07-27
    相关资源
    最近更新 更多