- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
所以我正在研究一个机器人项目,我们必须识别墙上的图案并相应地定位我们的机器人。我在笔记本电脑上开发了这个图像处理代码,它捕获图像,将其转换为 HSV,应用按位掩码,使用 Canny 边缘检测,并找到轮廓。我想我可以将代码复制并粘贴到树莓派 3 上;然而,由于处理能力下降,fps 小于 1。我一直在尝试将代码分离到线程中,因此我可以有一个捕获图像的线程,一个将图像转换为 HSV 并对其进行过滤的线程,和一根线进行轮廓拟合。为了让它们相互通信,我创建了队列。
这是我最初的愿景代码:
import numpy as np
import cv2
import time
import matplotlib.pyplot as plt
import sys
def onmouse(k, x, y, s, p):
global hsv
if k == 1: # left mouse, print pixel at x,y
print(hsv[y, x])
def distance_to_camera(Kwidth, focalLength, pixelWidth):
return (Kwidth * focalLength) / pixelWidth
def contourArea(contours):
area = []
for i in range(0,len(contours)):
area.append([cv2.contourArea(contours[i]),i])
area.sort()
if(area[len(area) - 1] >= 5 * area[0]):
return area[len(area)-1]
else: return 0
if __name__ == '__main__':
cap = cv2.VideoCapture(0)
"""
cap.set(3, 1920)
cap.set(4, 1080)
cap.set(5, 30)
time.sleep(2)
cap.set(15, -8.0)
"""
KNOWN_WIDTH = 18
# focalLength = focalLength = (rect[1][1] * 74) / 18
focalLength = 341.7075686984592
distance_data = []
counter1 = 0
numFrames = 100
samples = 1
start_time = time.time()
while (samples < numFrames):
# Capture frame-by-frame
ret, img = cap.read()
length1, width1, channels = img.shape
img = cv2.GaussianBlur(img, (5, 5), 0)
hsv = cv2.cvtColor(img.copy(), cv2.COLOR_BGR2HSV)
# lower_green = np.array([75, 200, 170])
# lower_green = np.array([53,180,122])
#lower_green = np.array([70, 120, 120])
lower_green = np.array([70, 50, 120])
upper_green = np.array([120, 200, 255])
#upper_green = np.array([120, 200, 255])
mask = cv2.inRange(hsv, lower_green, upper_green)
res = cv2.bitwise_and(hsv, hsv, mask=mask)
gray = cv2.cvtColor(res, cv2.COLOR_BGR2GRAY)
edged = cv2.Canny(res, 35, 125)
im2, contours, hierarchy = cv2.findContours(edged.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_NONE)
if (len(contours) > 1):
area,place = contourArea(contours)
#print(area)
if(area != 0):
# print("Contxours: %d" % contours.size())
# print("Hierarchy: %d" % hierarchy.size())
c = contours[place]
cv2.drawContours(img, c, -1, (0, 0, 255), 3)
cv2.drawContours(edged,c, -1, (255, 0, 0), 3)
perimeter = cv2.arcLength(c, True)
M = cv2.moments(c)
cx = 0
cy = 0
if (M['m00'] != 0):
cx = int(M['m10'] / M['m00']) # Center of MASS Coordinates
cy = int(M['m01'] / M['m00'])
rect = cv2.minAreaRect(c)
box = cv2.boxPoints(rect)
box = np.int0(box)
cv2.drawContours(img, [box], 0, (255, 0, 0), 2)
cv2.circle(img, (cx, cy), 7, (0, 0, 255), -1)
cv2.line(img, (int(width1 / 2), int(length1 / 2)), (cx, cy), (255, 0, 0), 2)
if(rect[1][1] != 0):
inches = distance_to_camera(KNOWN_WIDTH, focalLength, rect[1][1])
#print(inches)
distance_data.append(inches)
counter1+=1
samples+=1
"""
cv2.namedWindow("Image w Contours")
cv2.setMouseCallback("Image w Contours", onmouse)
cv2.imshow('Image w Contours', img)
cv2.namedWindow("HSV")
cv2.setMouseCallback("HSV", onmouse)
cv2.imshow('HSV', edged)
if cv2.waitKey(1) & 0xFF == ord('x'):
break
"""
# When everything done, release the capture
totTime = time.time() - start_time
print("--- %s seconds ---" % (totTime))
print('----%s fps ----' % (numFrames/totTime))
cap.release()
cv2.destroyAllWindows()
--- 13.469419717788696 seconds ---
----7.42422480665093 fps ----
plt.plot(distance_data)
plt.xlabel('TimeData')
plt.ylabel('Distance to Target(in) ')
plt.title('Distance vs Time From Camera')
plt.show()
这是我的线程代码,它在主线程中抓取帧并在另一个线程中对其进行过滤;我想要另一个线程用于轮廓拟合,但即使使用这两个过程,线程代码也具有与之前的代码几乎相同的 FPS。另外,这些结果来 self 的笔记本电脑,而不是树莓派。
import cv2
import threading
import datetime
import numpy as np
import queue
import time
frame = queue.Queue(0)
canny = queue.Queue(0)
lower_green = np.array([70, 50, 120])
upper_green = np.array([120, 200, 255])
class FilterFrames(threading.Thread):
def __init__(self,threadID,lock):
threading.Thread.__init__(self)
self.lock = lock
self.name = threadID
self.setDaemon(True)
self.start()
def run(self):
while(True):
img1 = frame.get()
img1 = cv2.GaussianBlur(img1, (5, 5), 0)
hsv = cv2.cvtColor(img1.copy(), cv2.COLOR_BGR2HSV)
mask = cv2.inRange(hsv, lower_green, upper_green)
res = cv2.bitwise_and(hsv, hsv, mask=mask)
edged = cv2.Canny(res, 35, 125)
canny.put(edged)
if __name__ == '__main__':
lock = threading.Lock()
numframes = 100
frames = 0
cap = cv2.VideoCapture(0)
filter = FilterFrames(lock=lock, threadID='Filter')
start_time = time.time()
while(frames < numframes):
ret,img = cap.read()
frame.put(img)
frames+=1
totTime = time.time() - start_time
print("--- %s seconds ---" % (totTime))
print('----%s fps ----' % (numframes/totTime))
"""
Results were:
--- 13.590131759643555 seconds ---
----7.358280388197121 fps ----
"""
cap.release()
我想知道我是否做错了什么,队列的访问是否会减慢代码速度,以及我是否应该使用多处理模块而不是该应用程序的线程。
最佳答案
您可以使用cProfile
模块分析代码。它会告诉您程序的哪一部分是瓶颈。
CPython 实现中的 Python 具有全局解释器锁 (GIL)。这意味着即使您的应用程序是多线程的,它也只会使用您的其中一个 CPU。您可以尝试multiprocessing
模块。尽管 Jython 和 IronPython 没有 GIL,但它们没有或没有稳定的 Python3 支持。
在您的代码中,从未使用过 self.lock
。使用带有 pylint 的优秀 IDE 来捕获此类错误。队列维护自己的锁。
threading.Thread.__init__(self)
是来自 Python2 的过时语法。使用 super().__init__()
关于Python 图像处理线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39823742/
我只是想知道需要什么样的计算/编程语言/框架来生成图像,例如 http://www.erdas.com/ 中的图像。 ? 以编程方式,如何生成一般空间分析图像? ps:我大部分时间都在使用java。
我尝试在我的 grails 项目(Mac OS X 上的 1.1.1)中使用一些图像处理插件或 java 库:imageTools 插件、imageJ、awt 库等。每次我从路径打开/获取图像以启动进
我有一个项目,我必须以多种方式处理图像。我陷入了像素化的困境。 对于像素化,我必须采用一组 10x10 像素并返回一个单独平均 RGB 颜色的单元格。目前我在运行程序中得到的只是一个红色图像。谢谢您的
就目前情况而言,这个问题不太适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、民意调查或扩展讨论。如果您觉得这个问题可以改进并可能重新开放,visit
这是一项作业,因为我是Python编程新手,所以我付出了很大的努力: 我正在运行以下函数,它接受图像和短语(空格将被删除,因此只有文本)作为参数,我已经获得了所有导入和预处理代码,我只需要实现这个函数
关闭。这个问题需要更多focused .它目前不接受答案。 想改进这个问题吗? 更新问题,使其只关注一个问题 editing this post . 关闭 8 年前。 Improve this qu
我需要一种简单易学且快速的方法来从背景图像、文本生成图像,然后保存为 JPEG 格式。 您有什么建议?有关于此的任何图书馆或教程吗?重要的标准是简单。 最佳答案 在 .Net 3.5/4 中,您还可以
我正在构建一个夜视应用程序,但我没有找到任何有用的算法可以应用于黑暗图像以使其清晰。任何人请给我一些好的算法。 提前致谢 最佳答案 由于 iphone 镜头和传感器的尺寸,无论您做什么,都会有很多噪音
所以我为游戏制作了这个程序,需要帮助让它更自动化一些。 程序接收图像然后显示它。我正在对 OpenGL 中的纹理执行此操作。当我截取游戏截图时,它通常约为 700x400。我将高度和宽度输入到我的程序
我想更改图像中像素的值,为此我需要将图像存储为矩阵。我怎样才能完成这项工作?请指导。 最佳答案 BufferedImage image = ImageIO.read(..); image.setRGB
概述: 我正在做一个视频创作项目。我使用的技术有:imageMagick、php、ffmpeg。 当前状态: 目前,该项目能够使用图像和文本以及很少的基本过渡来创建视频。我这样做的方式是使用 imag
我正在创建 facebook 应用程序,其中我将用户图像作为背景图像,并有一个用户可以四处移动的默认大写图像。用户将叠加图像(一顶帽子)放在正确的位置后,他点击保存 这就是我感到震惊的地方,我想知道如
我正在尝试编写一个 JavaScript 程序,通过在图像上放置三个垂直条纹来修改图像。左边三分之一是红色条纹,中间是绿色条纹,右边三分之一是蓝色条纹。 这是我试图实现的算法:1. 从您要更改的图像开
目前,我正在尝试通过图像分割方法将面部和头发修剪在一起,然后将所有非彩色像素设置为透明,然后尝试使用Binary Threshold技术和Adaptive Threshold。但是我得到了不希望的结果
我必须使此图像Book Image To Process的页面标题为:“单元3:主动学习的秘诀”,使其成为图像中的唯一页面 为此,我需要删除也在图像中的其他页面的一部分 我需要编写一个通用代码,可以对
我正在研究一个问题,其中我缩小图像的尺寸,在缩小图像中找到类似于二进制图像的有趣点。现在我只想放大在缩小图像中找到的有趣点(即白色像素点),而不是放大整个图像然后找到有趣的点。哪种技术可以最好地用于此
Closed. This question needs debugging details。它当前不接受答案。 想改善这个问题吗?更新问题,以便将其作为on-topic用于堆栈溢出。 上个月关闭。 I
嗨,我需要编写一个程序,从灰度图像中删除分界(图像中带有文本) 我阅读了有关阈值和模糊的信息,但我仍然不知道该怎么做。 我的图像是这样的希伯来文本图像: 我需要删除分界线(假设分界线是图像中的最小元素
我正在做一个带有深度图像的项目。但是我的深度相机有噪音和像素读取失败的问题。有一些点和轮廓(尤其是边缘)的值为零。如何忽略这个零值并将其与周围的值混合? 我试过 dilation和 erosion (
我正在做一个大学项目,我需要在java中处理图像。前段时间我在数学实验室工作,这很容易,所以我想知道是否存在任何可以让我玩像素值、颜色(按像素)、RGB 模型、灰度图像等的 java 库。 最佳答案
我是一名优秀的程序员,十分优秀!