bli*_*lim 3 python opencv image image-processing
我正在编写一个opencv程序,我使用USB摄像头跟踪对象的位置.为了确保我使用我正在使用的相机获得尽可能高的帧速率,我做了一个线程处理,从相机读取图像.图像处理在另一个循环中完成,该循环还将对象位置写入文件.
现在我想要一种避免多次处理同一帧的方法.所以我想我可以将刚刚处理过的图像与视频流线程中的图像进行比较.
首先我尝试使用if frame1 == frame2,但得到错误消息"具有多个元素的数组的真值是不明确的.使用a.any()或a.all()." 经过一些谷歌搜索后,我找到了cv2.compare和标志CMP_EQ.制作了示例代码,并使其以某种方式工作.但是,我的问题是.怎么能以更容易或更好的方式完成?
import cv2
cv2.namedWindow('image1', cv2.WINDOW_NORMAL)
cv2.namedWindow('image2', cv2.WINDOW_NORMAL)
frame1 = cv2.imread("sample1.png")
frame2 = frame1
frame3 = cv2.imread("sample2.png")
compare1 = cv2.compare(frame1,frame2,0)
compare2 = cv2.compare(frame1,frame3,0)
cv2.imshow('image1', compare1)
cv2.imshow('image2', compare2)
if compare1.all():
print "equal"
else:
print "not equal"
if compare2.all():
print "equal"
else:
print "not equal"
cv2.waitKey(0)
cv2.destroyAllWindows()
Run Code Online (Sandbox Code Playgroud)
Jor*_*ley 22
open("image1.jpg","rb").read() == open("image2.jpg","rb").read()
Run Code Online (Sandbox Code Playgroud)
应该告诉你它们是否完全相同......
我做的事情接近你在做的事;我正在努力获得不同。我使用了减法功能。它可能会帮助您。
更新:
import cv2
import numpy as np
a = cv2.imread("sample1.png")
b = cv2.imread("sample2.png")
difference = cv2.subtract(a, b)
result = not np.any(difference)
if result is True:
print "Pictures are the same"
else:
cv2.imwrite("ed.jpg", difference )
print "Pictures are different, the difference is stored as ed.jpg"
Run Code Online (Sandbox Code Playgroud)
给你的图片一个索引怎么样?
伪代码:
class Frame
{
cvImage img;
uint idx;
}
Run Code Online (Sandbox Code Playgroud)
而不是简单地检查当前索引是否大于您处理的最后一个索引。它比任何基于图像处理的方法都很简单并且绝对更快。
| 归档时间: |
|
| 查看次数: |
15632 次 |
| 最近记录: |