【发布时间】:2020-08-18 08:15:49
【问题描述】:
这是一条传送带,我感兴趣的区域是橙色带:
我有一个摄像系统,可以拍摄带有产品的传送带的照片。每次生产运行的图片数量为 500-2000 张图片。我需要确保的是,传送带的橙色条带始终没有任何物体,不受阻碍,以便生产顺利进行。这有更多的上下文,但只知道在图片中我需要橙色条带为橙色,如果不是,则意味着它被阻塞了。
所以我需要一个程序,它可以读取图像并分析图片中的两个橙色条带,因此当它们检测到任何阻碍条带的东西时,它会发送一条错误消息。我的想法是,该程序将在图像的顶部和底部有橙色条带所在的矩形,当它被阻挡时,图像中会显示一条错误消息。
我不知道这个过程叫什么,但目前我正在研究霍夫变换、模板匹配和颜色检测。我面临的问题是,由于橙色条是连续的,因此代码会在图像上读取大量重复项,这些重复项会相互重叠。整个图像就会被着色。
我想要的只是让它在顶部和底部条带的矩形中进行分析,这将是距离中心 2/3 长度的图像。请帮助,如果不清楚,请随时询问您需要的详细信息。
已编辑:添加了障碍物示例的图片。白点可能是灰尘或任何杂质。
我只想尽可能分析红色矩形的区域。
这是我正在使用的代码:
import cv2
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm
from matplotlib import colors
import numpy as np
import matplotlib.pyplot as plt
img = cv2.imread('sample02.jpg') # Read image
img = cv2.resize(img, (672, 672)) # Resize image
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
lower_gray = np.array([0, 0, 0], np.uint8)
upper_gray = np.array([0, 0, 45], np.uint8)
mask_gray = cv2.inRange(hsv, lower_gray, upper_gray)
img_res = cv2.bitwise_and(img, img, mask = mask_gray)
cv2.imshow('detected.jpg', mask_gray)
cv2.imwrite('5.jpg',mask_gray)
cv2.waitKey(0)
cv2.destroyAllWindows()
我能够隔离条形并且清楚地显示异物/阻塞
这是我使用的代码:
# importing cv2
import cv2
import numpy as np
# Reading an image in default mode
img = cv2.imread('f06.bmp') # Read image
img = cv2.resize(img, (672, 672)) # Resize image
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
lower_gray = np.array([0, 0, 0], np.uint8)
upper_gray = np.array([0, 0, 45], np.uint8)
mask_gray = cv2.inRange(hsv, lower_gray, upper_gray)
# 1) Start coordinate (Top rectangle)
# represents the top left corner of rectangle
start_point = (0, 0)
# Ending coordinate
# represents the bottom right corner of rectangle
end_point = (672, 200)
# color in BGR
color = (0, 0, 0)
# Line thickness
thickness = -1
# 2) Start coordinate (Mid rectangle)
start_point2 = (0, 240)
# Ending coordinate
# represents the bottom right corner of rectangle
end_point2 = (672, 478)
# color in BGR
color2 = (0, 0, 0)
# Line thickness
thickness2 = -1
# 3) Start coordinate (Bottom rectangle)
start_point3 = (0, 515)
# Ending coordinate
# represents the bottom right corner of rectangle
end_point3 = (672, 672)
# color in BGR
color3 = (0, 0, 0)
# Line thickness
thickness3 = -1
# 4) Start coordinate (Left rectangle)
start_point4 = (0, 180)
# Ending coordinate
# represents the bottom right corner of rectangle
end_point4 = (159, 672)
# color in BGR
color4 = (0, 0, 0)
# Line thickness
thickness4 = -1
# 5) Start coordinate (Right rectangle)
start_point5 = (485, 0)
# Ending coordinate
# represents the bottom right corner of rectangle
end_point5 = (672, 672)
# color in BGR
color5 = (0, 0, 0)
# Line thickness
thickness5 = -1
# Using cv2.rectangle() method
image1 = cv2.rectangle(mask_gray, start_point, end_point, color, thickness)
image2 = cv2.rectangle(mask_gray, start_point2, end_point2, color2, thickness2)
image3 = cv2.rectangle(mask_gray, start_point3, end_point3, color3, thickness3)
image4 = cv2.rectangle(mask_gray, start_point4, end_point4, color4, thickness4)
image5 = cv2.rectangle(mask_gray, start_point5, end_point5, color5, thickness5)
image = image1 + image2 + image3 + image4 + image5
# Displaying the image
cv2.imshow('test.jpg', image)
cv2.imwrite('almost02.jpg',image)
cv2.waitKey(0)
cv2.destroyAllWindows()
这有点严格和漫长,因为我在我不感兴趣的区域手动插入了矩形,因为我已经使用了蒙版,但仍然有很多噪音。所以这是我能想到的最好的了。
现在可以计算白色像素了!
我用来计算白色像素的代码:
img = cv2.imread('almost05.jpg', cv2.IMREAD_GRAYSCALE)
n_white_pix = np.sum(img == 0)
print('Number of white pixels:', n_white_pix)
我将不得不使用膨胀或其他参数来使差异更加明显,以避免误报。
【问题讨论】:
-
很清楚你想要完成什么,但不清楚你尝试了什么以及你的结果是什么。请提供 1. 代码 2. 准确输入 3. 该输入所需的输出。
-
也就是说,为什么不按像素颜色分割橙色区域+一些简单的形态孔填充?只需检查图像中大致橙色的部分。还不够吗?为什么不呢?
-
请添加一些带有障碍物的示例图片。
-
@MarkSetchell 我添加了一个阻塞示例
-
@Gulzar 基本上我只是从 youtube 视频和在线网站复制代码。 pythonprogramming.net/template-matching-python-opencv-tutorial 与此类似,但由于我试图检测的是一条线,因此黄色框彼此无限重叠
标签: python opencv image-processing raspberry-pi