When click on run in the program I want:
- my camera to initialize
- take a picture of the object in front of it
- detect the edges in that picture and save that picture
- compare the current picture with an already existing picture I have in the database
- calculate the difference in percentage between two pictures
Here is my code for the first 3 steps:
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()
and here is the code to get the difference between two edge images:
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
Now my question is ... How do I integrate these two programs, and how do I write this entire process in one program? Can someone please help me out?