我发现了很多关于使用openCV等人在图像中找到“东西”的问题.在

Python中,但到目前为止,我一直无法将它们拼凑在一起,以便为我的问题提供可靠的解决方案.

我正在尝试使用计算机视觉来帮助计算微小的表面贴装电子部件.我的想法是将零件转移到一张纯色纸上,拍下一张照片,并让软件告诉我其中有多少件物品.

“事物”从一张图片到另一张图片不同,但在任何一张图片中总是相同的.我似乎能够手动调整特定部件的色调/饱和度等参数,但每次更换新部件时都需要调整.

我目前的半功能代码发布如下:

import imutils

import numpy

import cv2

import sys

def part_area(contours, round=10):

"""Finds the mode of the contour area. The idea is that most of the parts in an image will be separated and that

finding the most common area in the list of areas should provide a reasonable value to approximate by. The areas

are rounded to the nearest multiple of 200 to reduce the list of options."""

# Start with a list of all of the areas for the provided contours.

areas = [cv2.contourArea(contour) for contour in contours]

# Determine a threshold for the minimum amount of area as 1% of the overall range.

threshold = (max(areas) - min(areas)) / 100

# Trim the list of areas down to only those that exceed the threshold.

thresholded = [area for area in areas if area > threshold]

# Round the areas to the nearest value set by the round argument.

rounded = [int((area + (round / 2)) / round) * round for area in thresholded]

# Remove any areas that rounded down to zero.

cleaned = [area for area in rounded if area != 0]

# Count the areas with the same values.

counts = {}

for area in cleaned:

if area not in counts:

counts[area] = 0

counts[area] += 1

# Reduce the areas down to only those that are in groups of three or more with the same area.

above = []

for area, count in counts.iteritems():

if count > 2:

for _ in range(count):

above.append(area)

# Take the mean of the areas as the average part size.

average = sum(above) / len(above)

return average

def find_hue_mode(hsv):

"""Given an HSV image as an input, compute the mode of the list of hue values to find the most common hue in the

image. This is used to determine the center for the background color filter."""

pixels = {}

for row in hsv:

for pixel in row:

hue = pixel[0]

if hue not in pixels:

pixels[hue] = 0

pixels[hue] += 1

counts = sorted(pixels.keys(), key=lambda key: pixels[key], reverse=True)

return counts[0]

if __name__ == "__main__":

# load the image and resize it to a smaller factor so that the shapes can be approximated better

image = cv2.imread(sys.argv[1])

# define range of blue color in HSV

hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)

center = find_hue_mode(hsv)

print 'Center Hue:', center

lower = numpy.array([center - 10, 50, 50])

upper = numpy.array([center + 10, 255, 255])

# Threshold the HSV image to get only blue colors

mask = cv2.inRange(hsv, lower, upper)

inverted = cv2.bitwise_not(mask)

blurred = cv2.GaussianBlur(inverted, (5, 5), 0)

edged = cv2.Canny(blurred, 50, 100)

dilated = cv2.dilate(edged, None, iterations=1)

eroded = cv2.erode(dilated, None, iterations=1)

# find contours in the thresholded image and initialize the shape detector

contours = cv2.findContours(eroded.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

contours = contours[0] if imutils.is_cv2() else contours[1]

# Compute the area for a single part to use when setting the threshold and calculating the number of parts within

# a contour area.

part_area = part_area(contours)

# The threshold for a part's area - can't be too much smaller than the part itself.

threshold = part_area * 0.5

part_count = 0

for contour in contours:

if cv2.contourArea(contour) < threshold:

continue

# Sometimes parts are close enough together that they become one in the image. To battle this, the total area

# of the contour is divided by the area of a part (derived earlier).

part_count += int((cv2.contourArea(contour) / part_area) + 0.1) # this 0.1 "rounds up" slightly and was determined empirically

# Draw an approximate contour around each detected part to give the user an idea of what the tool has computed.

epsilon = 0.1 * cv2.arcLength(contour, True)

approx = cv2.approxPolyDP(contour, epsilon, True)

cv2.drawContours(image, [approx], -1, (0, 255, 0), 2)

# Print the part count and show off the processed image.

print 'Part Count:', part_count

cv2.imshow("Image", image)

cv2.waitKey(0)

这是我正在使用的输入图像类型的示例:

或这个:

我目前正在得到这样的结果:

结果清楚地表明,剧本在识别某些部件时遇到了麻烦,而且当部件彼此接触时,其真正的跟腱似乎就是这样.

所以我的问题/挑战是,我该怎么做才能提高这个脚本的可靠性?

该脚本将集成到现有的Python工具中,因此我正在使用Python搜索解决方案.解决方案不需要是纯Python,因为我愿意安装可能需要的第三方库.

python图像检测_如何用Python检测图像中的矩形项相关推荐

  1. 用python画耳朵_如何用python画猪头

    用python画猪头的方法:首先设置画布和画笔,代码为[a.screensize(400,300)a.setup(width=400,height=300)]:然后画脸型,代码为[.goto(-100 ...

  2. python怎么读_如何用Python读写文件

    前面我们已经介绍了很多Python相关的基础知识,大家是不是对Python已经有了进一步认识了呢?作为人工智能时代的热门编程语言,开始接触并学习Python的孩子越来越多,家长们都不想让自己的孩子落于 ...

  3. python旅游推荐系统_如何用Python搭建一个简单的推荐系统?

    推荐系统的相关知识我们已在前文中提到,在这篇文章中,我们会介绍如何用Python来搭建一个简单的推荐系统. 本文使用的数据集是MovieLens数据集,该数据集由明尼苏达大学的Grouplens研究小 ...

  4. 用python开启相机_如何用Python打开realsenseD435相机并获取相机参数

    如何用Python打开realsenseD435相机 import pyrealsense2 as rs import numpy as np import cv2 if __name__ == &q ...

  5. python读取二进制文件_如何用python读取二进制文件?

    我发现用Python读取二进制文件特别困难.你能帮我一把吗?我需要读取这个文件,在Fortran 90中,该文件很容易被int*4 n_particles, n_groups real*4 group ...

  6. python md5加密_如何用python“优雅”的调用有道翻译?

    前言 其实在以前就盯上有道翻译了的,但是由于时间问题一直没有研究(我的骚操作还在后面,记得关注),本文主要讲解如何用python调用有道翻译,讲解这个爬虫与有道翻译的js"斗争"的 ...

  7. python求平均值_如何用python求平均值

    学习了Python相关数据类型,函数的知识后,利用字符串的分割实现了输入任意多个数据,并计算其平均值的小程序.思路是接收输入的字符串,以空格为分隔符,将分割的数据存入列表(lst1)中,将lst1中的 ...

  8. python小助手_如何用python写个人专属群聊提醒小助手?

    前言 大家还记得教会父母玩微信是什么时候吗?父母学会后,我们的生活就发生了「质」的变化,父母也许会吐槽你的微信头像不好,要你换一个头像. 最近 pk哥 又被母后大人吐槽了,原因是亲戚微信群里某个亲戚生 ...

  9. 如何用python计算圆周率_如何用python计算圆周率?

    如何用python计算圆周率? python计算圆周率的方法: 圆周率没有精确的计算公式,所以只能用近似的方式计算它的近似值. 我们运用蒙特卡罗方法,思路很简单,在下面图形中随机抛置大量的点, 计算落 ...

  10. python做网站开发_如何用Python做网站开发

    很多人想要用Python做网站开发,但是又不知从何入手,接下来小编介绍用Python做网站开发需要学习哪些知识,又应该以什么样的顺序来学. 第一步:HTML(超文本标记语言)是网页的核心,学好HTML ...

最新文章

  1. 干货:Wireshark使用技巧-显示规则
  2. python 动态规划例子
  3. WebRTC VideoEngine超详细教程(三)——集成X264编码和ffmpeg解码
  4. Linux服务器集群系统(二)--转
  5. 区块链BaaS云服务(8)京东 智臻链
  6. J2EE的经常使用十三规范——java菜鸟成长记
  7. AtCoder Regular Contest 061
  8. 类似ajax封装函数,JS 封装一个async式的AJAX函数
  9. oracle 微信公众号,关于微信公众号贴代码的方法
  10. 爬虫技术前置准备工作 http url 请求方法 状态码 等等
  11. 集合的get方法中参数从多少开始_源码分析CopyOnWriteArrayList 中的隐藏知识,你Get了吗?...
  12. 郑大网教育计算机2017,2017郑大计算机操作系统(计算机).docx
  13. 阶段5 3.微服务项目【学成在线】_day01 搭建环境 CMS服务端开发_10-CMS服务端工程搭建-导入基础工程...
  14. Lauterbach trace32与 jlink
  15. 如何获得微信小游戏源码
  16. 2022CUDA夏季训练营Day5实践
  17. 用C#获取系统中的临时文件夹或windows安装文件夹
  18. 计算机哪些方向发展前景,计算机就业的几个方向
  19. php 处理eml,php读取eml范例、php解析eml、eml解析成网页
  20. 43. 盘点那些必问的数据结构算法题之二叉树基础

热门文章

  1. tomcat问题记录-线程池引发的问题
  2. 转:MFC 的程序中GetAt()的理解
  3. BZOJ 4300: 绝世好题( dp )
  4. 操作系统中的内存分配
  5. Shell脚本调试工具set
  6. php mysql.x86 64_centos7.2yum安装php70w.x86_64
  7. python语言支持苹果系统吗_Mac系统上的一款Python编程平台
  8. php ajax jquery 表单重复提交,jQuery的 $.ajax防止重复提交的两种方法(推荐)
  9. c++ char*转string_CC++|输入输出与内存、缓存、设备文件
  10. mysql主从复制gtid_详解MySQL主从复制实战 - 基于GTID的复制