1
兩個方案時,請單擊上運行的程序我想:Itegration在python
- 我的相機初始化
- 拍攝物體的圖片在它前面
- 檢測的邊緣圖片並保存圖片
- 比較已經存在的圖片我已經在數據庫
- 當前圖片計算兩個圖片 之間的百分比差異0
這是我的第3個步驟的代碼:
import numpy as np
import cv2
from matplotlib import pyplot as plt
camera_port = 0
ramp_frames = 30
cap = cv2.VideoCapture(camera_port)
def get_image():
retval, im = cap.read()
return im
for i in xrange(ramp_frames):
temp = get_image()
print("Taking image...")
# Take the actual image we want to keep
camera_capture = get_image()
file = "/Users/Me/Documents/python programs/New/test_image7.jpg"
# A nice feature of the imwrite method is that it will automatically choose the
# correct format based on the file extension you provide. Convenient!
cv2.imwrite(file, camera_capture)
# You'll want to release the camera, otherwise you won't be able to create a new
# capture object until your script exits
del(cap)
img1=cv2.imread('/Users/Me/Documents/python programs/New/test_image7.jpg',0)
#height, width, channels = img1.shape
#res = cv2.resize(img1,(width/2, height/2), interpolation = cv2.INTER_CUBIC)
#cv2.imshow('image',res)
edges = cv2.Canny(img1,100,200)
#plt.subplot(121),plt.imshow(img1,cmap = 'gray')
#plt.title('Original Image'), plt.xticks([]), plt.yticks([])
plt.subplot(122),plt.imshow(edges,cmap = 'gray')
plt.title('Edge Image'), plt.xticks([]), plt.yticks([])
plt.show()
#plt.save('/Users/Me/Documents/python programs/New/test_image2.jpg',img1)
cv2.waitKey(0)
cv2.destroyAllWindows()
,這裏是代碼得到兩個邊緣圖像之間的差異:
from itertools import izip
from PIL import Image
i1 = Image.open("pencil.png")
i2 = Image.open("eraser2.png")
assert i1.mode == i2.mode, "Different kinds of images."
assert i1.size == i2.size, "Different sizes."
pairs = izip(i1.getdata(), i2.getdata())
if len(i1.getbands()) == 1:
# for gray-scale jpegs
dif = sum(abs(p1-p2) for p1,p2 in pairs)
else:
dif = sum(abs(c1-c2) for p1,p2 in pairs for c1,c2 in zip(p1,p2))
ncomponents = i1.size[0] * i1.size[1] * 3
print "Difference (percentage):", (dif/255.0 * 100)/ncomponents
現在我的問題是...我如何整合這兩個程序,以及如何在一個程序中編寫整個過程?有人可以幫我嗎?
非常感謝您抽出寶貴時間回答我的問題,....這個解決方案非常完美......我還有一些關於這個的問題..我可以有您的郵件ID嗎?我應該郵寄給你嗎? –
我修改了你給的代碼,它的工作......現在我需要知道如何比較當前圖像與數據庫中的其他10個圖像,並給我的文件與最小的差異...我試過使用這個循環,但它的不工作.. for no_file1範圍(10,100): template = cv2.imread('numbers1/{no_file} .jpg'。格式(no_file = no_file1),0) –
「不工作」非常模糊,但如果您有10張圖像,爲什麼不使用範圍(10)而不是範圍(10,100)? –