- added cam calib
git-svn-id: http://moon:8086/svn/software/trunk/projects/opencv@319 b431acfa-c32f-4a4a-93f1-934dc6c82436
This commit is contained in:
+61
-7
@@ -3,6 +3,7 @@ from __future__ import print_function
|
||||
|
||||
from imutils.video import FPS
|
||||
|
||||
import sys
|
||||
import numpy as np
|
||||
import argparse
|
||||
import time
|
||||
@@ -83,7 +84,7 @@ class FindObjects():
|
||||
perimeter = cv2.arcLength(contours[member['id']], True)
|
||||
pi = 3.14159265359
|
||||
Q = 0
|
||||
if (perimeter > 0):
|
||||
if (area > 10):
|
||||
Q = 4*pi*area/(perimeter*perimeter)
|
||||
|
||||
if Q > 0.7:
|
||||
@@ -124,6 +125,8 @@ ap.add_argument("-r", "--framerate", type=int, default=30,
|
||||
help="# of frames to loop over for FPS test")
|
||||
ap.add_argument("-f", "--filename", type=str, default='piCamera',
|
||||
help="Whether or not frames should be displayed")
|
||||
ap.add_argument("-c", "--calibrate", type=str, default='off',
|
||||
help="Whether or not frames should be displayed")
|
||||
args = vars(ap.parse_args())
|
||||
|
||||
videoFile = args["filename"]
|
||||
@@ -135,7 +138,7 @@ if videoFile == "piCamera":
|
||||
from PiVideoStream import PiVideoStream
|
||||
from picamera.array import PiRGBArray
|
||||
from picamera import PiCamera
|
||||
vs = PiVideoStream(resolution=(width,height), framerate=framerate).start()
|
||||
vs = PiVideoStream(resolution=(2*width,2*height), framerate=framerate).start()
|
||||
time.sleep(5.0)
|
||||
else:
|
||||
vs = cv2.VideoCapture(videoFile)
|
||||
@@ -150,13 +153,62 @@ findObjects = FindObjects()
|
||||
beadColors = []
|
||||
numColorClasses = 0
|
||||
|
||||
# loop over some frames...this time using the threaded stream
|
||||
while fps._numFrames < args["num_frames"]:
|
||||
# grab the frame from the threaded video stream
|
||||
if args["calibrate"] == 'on':
|
||||
lens_corr = np.ones((height, width, 3), np.float32)
|
||||
if videoFile == "piCamera":
|
||||
frame = vs.read()
|
||||
else:
|
||||
ret, frame = vs.read()
|
||||
|
||||
Z = np.float32(frame)
|
||||
print (frame.dtype)
|
||||
print (frame.shape)
|
||||
print (lens_corr.dtype)
|
||||
print (lens_corr.shape)
|
||||
print (Z.dtype)
|
||||
print (Z.shape)
|
||||
ref_color = Z[height/2, width/2, :]
|
||||
print (ref_color)
|
||||
|
||||
for c in range (0, 3):
|
||||
lens_corr[:,:,c] = cv2.divide(ref_color[c], Z[:,:,c])
|
||||
|
||||
params = list()
|
||||
params.append(cv2.IMWRITE_PNG_COMPRESSION)
|
||||
params.append(0)
|
||||
|
||||
for c in range (0, 3):
|
||||
lens_corr[:,:,c] = cv2.multiply(128, lens_corr[:,:,c])
|
||||
cv2.imwrite("lens_corr.png", np.uint8(lens_corr), params)
|
||||
|
||||
sys.exit()
|
||||
|
||||
|
||||
frame = np.ones((height, width, 3), np.float32)
|
||||
lens_corr = np.float32(cv2.imread("lens_corr.png"))
|
||||
|
||||
# loop over some frames...this time using the threaded stream
|
||||
while fps._numFrames < args["num_frames"]:
|
||||
# grab the frame from the threaded video stream
|
||||
|
||||
if videoFile == "piCamera":
|
||||
frame8 = vs.read()[int(height/2):int(height/2+height), int(width/2):int(width/2)+width]
|
||||
else:
|
||||
ret, frame8 = vs.read()
|
||||
|
||||
if 0:
|
||||
frame32 = np.float32(frame8)
|
||||
print (frame.dtype)
|
||||
print (frame.shape)
|
||||
print (lens_corr.dtype)
|
||||
print (lens_corr.shape)
|
||||
|
||||
for c in range (0, 3):
|
||||
frame32[:,:,c] = cv2.multiply(lens_corr[:,:,c], frame32[:,:,c], 1.0/255)
|
||||
|
||||
frame = np.uint8(frame32)
|
||||
else:
|
||||
frame = frame8
|
||||
|
||||
kernel = np.ones((3,3),np.uint8)
|
||||
frame_dilated = cv2.dilate(frame,kernel,iterations = 1)
|
||||
@@ -231,7 +283,9 @@ while fps._numFrames < args["num_frames"]:
|
||||
numColorClasses = len(colorClasses)
|
||||
print("Found " + str(numColorClasses) + " color classes")
|
||||
|
||||
# cv2.imshow('Thresholded',img1_thr)
|
||||
|
||||
cv2.imshow('Raw',frame8)
|
||||
cv2.imshow('Corrected', frame)
|
||||
cv2.imshow('Contours', frame_dilated)
|
||||
cv2.imshow('Colors', img1_colors)
|
||||
cv2.imshow('Canny',img1_canny)
|
||||
@@ -279,7 +333,7 @@ Z = np.float32(plotColors)
|
||||
|
||||
# Define criteria = ( type, max_iter, epsilon )
|
||||
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 100, 0.1)
|
||||
ret,label,center=cv2.kmeans(Z,10,None,criteria,10,cv2.KMEANS_PP_CENTERS)
|
||||
ret,label,center=cv2.kmeans(Z,numColorClasses,None,criteria,10,cv2.KMEANS_PP_CENTERS)
|
||||
|
||||
print (center)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user