Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/276.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分析图像中的特定区域_Python_Opencv_Image Processing_Raspberry Pi - Fatal编程技术网

Python分析图像中的特定区域

Python分析图像中的特定区域,python,opencv,image-processing,raspberry-pi,Python,Opencv,Image Processing,Raspberry Pi,这是一条传送带,我感兴趣的领域是橙色带: 我有一个摄像系统,可以拍摄传送带及其产品的照片。每次生产运行的图片数量为500-2000张。我需要确保的是,传送带的橙色条带始终没有任何物体,并且没有阻碍,以便生产顺利进行。这里有更多的上下文,但要知道,在图片中,我需要橙色的条纹是橙色的,如果不是,那就意味着它被遮挡了 所以我需要有一个程序,它可以读取图像并分析图片中的两条橙色条纹,这样当他们检测到任何阻碍条纹的东西时,它就会发送错误消息。我想到的是,该程序将在图像的顶部和底部有矩形,橙色条带所在的位

这是一条传送带,我感兴趣的领域是橙色带:

我有一个摄像系统,可以拍摄传送带及其产品的照片。每次生产运行的图片数量为500-2000张。我需要确保的是,传送带的橙色条带始终没有任何物体,并且没有阻碍,以便生产顺利进行。这里有更多的上下文,但要知道,在图片中,我需要橙色的条纹是橙色的,如果不是,那就意味着它被遮挡了

所以我需要有一个程序,它可以读取图像并分析图片中的两条橙色条纹,这样当他们检测到任何阻碍条纹的东西时,它就会发送错误消息。我想到的是,该程序将在图像的顶部和底部有矩形,橙色条带所在的位置,当它被阻挡时,一条错误消息将显示在图像中

我不知道这个过程叫什么,但目前我正在研究hough变换、模板匹配和颜色检测。我面临的问题是,由于橙色条带是连续的,因此代码在图像上读取大量重复的内容,这些内容会相互重叠。整个图像都是彩色的

我只想让它在顶部和底部的一个矩形条带上进行分析,这个矩形条带距离图像中心有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。具有统计信息的连接组件

  • 使用
    cv2.inRange()
    分割两条橙色条
  • 用于隔离橙色条
  • 使用统计信息(宽度、高度区域)检查是否确实存在两个具有预期宽度/高度比的橙色条(否则,如果没有出现这种情况,并且橙色条中连接的组件多于两个,则可能存在某种阻碍)
您可以使用类似的工具(甚至可以拟合椭圆/矩形、获取角度和其他度量),但请记住,在树莓圆周率上,它会稍微慢一点。连接的组件有点有限,只能使用像素,但速度更快,希望足够好

下面是一个超级基本的示例:

#!/usr/bin/env python
import cv2
import numpy as np

# test image (4 corners, a ring and a + at the centre)
src = np.array([
    [255,255,  0,  0,  0,  0,  0,255,255],
    [255,  0,  0,255,255,255,  0,  0,255],
    [  0,  0,255,  0,  0,  0,255,  0,  0],
    [  0,255,  0,  0,255,  0,  0,255,  0],
    [  0,255,  0,255,255,255,  0,255,  0],
    [  0,255,  0,  0,255,  0,  0,255,  0],
    [  0,  0,255,  0,  0,  0,255,  0,  0],
    [255,  0,  0,255,255,255,  0,  0,255],
    [255,255,  0,  0,  0,  0,  0,255,255]
    ],dtype="uint8")

# connectivity type: 4 (N,E,S,W) or 8 (N,NE,E,SE,S,SW,W,NW)
connectivity = 8
# compute connected components with stats
(labelCount,labels,stats,centroids) = cv2.connectedComponentsWithStats(src, connectivity)
# number of labels
print("total number of labels",labelCount)
# labelled image
print("labels")
print(labels)
#  stats
print("stats")
print(stats)
# centroid matrix
print("centroids")
print(centroids)

# visualise, 42 = (255 // (labelCount-1))
labelsVis = (np.ones((9,9),dtype="uint8") * labels * 42).astype('uint8')
cv2.imshow('labels',labelsVis)
cv2.waitKey(0)

# just connected components (no stats), 4 connectivity
(labelCount,labels) = cv2.connectedComponents(src, 4)
print("labels")
print(labels)
3。使用ROI和图像差分/背景减法

给定静态摄像机/照明,如果你只是对超出范围的东西感兴趣,你可以做如下操作:

  • 传送带的中心区域,仅留下顶部和底部区域,包括可见的橙色带,并存储(在内存或磁盘中)橙色带上没有任何对象的图像
  • 在主处理循环中,在新的遮罩相机帧和先前存储的原始帧之间执行
  • 在这种情况下,任何对象在黑色背景上都是白色的:可以使用
    countNonZero()
    /
    connectedComponentsWithStats()
    /
    findcontours()
    /等设置阈值条件
OpenCV附带了一个相当不错的配置。可能值得一试,但计算成本更高,您需要调整参数(历史大小、忘记背景的速率等),以最适合您的项目


你在正确的轨道上。就我个人而言,我不会为这项任务的模板匹配而烦恼(我会将其保存下来,用于检测桌面上的图标或非常稳定的场景,其中要搜索的模板在图像中出现时在目标图像中总是看起来相同)。我在以下方面没有足够好的经验:您可以尝试,找到正确的参数,并希望测线保持一致。

很清楚您试图实现什么,但不清楚您尝试了什么以及结果是什么。请提供1。代码2。精确输入3。输入所需的输出。也就是说,为什么不通过像素颜色+一些简单的形态学空洞填充来分割橙色区域?只需检查图像中大致为橙色的部分。这还不够吗?为什么不呢?请添加一些带有障碍物的示例图像。@MarkSetchell我添加了一个obstruction@Gulzar基本上我只是从youtube视频和在线网站复制代码。它与此类似,但由于我试图检测的是一条线,黄色框彼此无限重叠。这非常有用,我非常感谢你。这是我第一次处理代码,有点过火,但我正在尽力。我会尝试一下,然后再给你回复我的进度