def redraw(): global draging global has_roi global roi_x0 global roi_y0 global cur_mouse_x global cur_mouse_y #Redraw ROI selection image2 = cv.CloneImage(current_image) # redraw old rect pen_width = 4 if rect_table.has_key(current_img_file_name): rects_in_table = rect_table[current_img_file_name] for r in rects_in_table: cv.Rectangle(image2, (r[0], r[1]), (r[0] + r[2], r[1] + r[3]), cv.CV_RGB(0, 255, 0), pen_width) # redraw new rect if has_roi: cv.Rectangle(image2, (roi_x0, roi_y0), (cur_mouse_x, cur_mouse_y), cv.CV_RGB(255, 0, 255), pen_width) # draw background if current_img_file_name in background_files: cv.Line(image2, (0, 0), (image2.width, image2.height), cv.CV_RGB(255, 0, 0)) cv.Line(image2, (0, image2.height), (image2.width, 0), cv.CV_RGB(255, 0, 0)) cv.ShowImage(window_name, image2)
def getIris(frame): iris = [] copyImg = cv.CloneImage(frame) resImg = cv.CloneImage(frame) grayImg = cv.CreateImage(cv.GetSize(frame), 8, 1) mask = cv.CreateImage(cv.GetSize(frame), 8, 1) storage = cv.CreateMat(frame.width, 1, cv.CV_32FC3) cv.CvtColor(frame,grayImg,cv.CV_BGR2GRAY) cv.Canny(grayImg, grayImg, 5, 70, 3) cv.Smooth(grayImg,grayImg,cv.CV_GAUSSIAN, 7, 7) circles = getCircles(grayImg) iris.append(resImg) for circle in circles: rad = int(circle[0][2]) global radius radius = rad cv.Circle(mask, centroid, rad, cv.CV_RGB(255,255,255), cv.CV_FILLED) cv.Not(mask,mask) cv.Sub(frame,copyImg,resImg,mask) x = int(centroid[0] - rad) y = int(centroid[1] - rad) w = int(rad * 2) h = w cv.SetImageROI(resImg, (x,y,w,h)) cropImg = cv.CreateImage((w,h), 8, 3) cv.Copy(resImg,cropImg) cv.ResetImageROI(resImg) return(cropImg) return (resImg)
#---- Standard ---- color_dst_standard = cv.CreateImage(cv.GetSize(im), 8, 3) cv.CvtColor(im, color_dst_standard, cv.CV_GRAY2BGR) #Create output image in RGB to put red lines lines = cv.HoughLines2(dst, cv.CreateMemStorage(0), cv.CV_HOUGH_STANDARD, 1, pi / 180, 100, 0, 0) for (rho, theta) in lines[:100]: a = math.cos(theta) #Calculate orientation in order to print them b = math.sin(theta) x0 = a * rho y0 = b * rho pt1 = (cv.Round(x0 + 1000 * (-b)), cv.Round(y0 + 1000 * (a))) pt2 = (cv.Round(x0 - 1000 * (-b)), cv.Round(y0 - 1000 * (a))) cv.Line(color_dst_standard, pt1, pt2, cv.CV_RGB(255, 0, 0), 2, 4) #Draw the line #---- Probabilistic ---- color_dst_proba = cv.CreateImage(cv.GetSize(im), 8, 3) cv.CvtColor(im, color_dst_proba, cv.CV_GRAY2BGR) # idem rho = 1 theta = pi / 180 thresh = 50 minLength = 120 # Values can be changed approximately to fit your image edges maxGap = 20 lines = cv.HoughLines2(dst, cv.CreateMemStorage(0), cv.CV_HOUGH_PROBABILISTIC, rho, theta, thresh, minLength, maxGap) for line in lines:
def add_keypoints(self, track_box): # Look for any new keypoints around the current keypoints # Begin with a mask of all black pixels mask = np.zeros_like(self.grey) # Get the coordinates and dimensions of the current track box try: ((x, y), (w, h), a) = track_box except: try: x, y, w, h = track_box x = x + w / 2 y = y + h / 2 a = 0 except: rospy.loginfo("Track box has shrunk to zero...") return x = int(x) y = int(y) # Expand the track box to look for new keypoints w_new = int(self.expand_roi * w) h_new = int(self.expand_roi * h) pt1 = (x - int(w_new / 2), y - int(h_new / 2)) pt2 = (x + int(w_new / 2), y + int(h_new / 2)) mask_box = ((x, y), (w_new, h_new), a) # Display the expanded ROI with a yellow rectangle if self.show_add_drop: cv2.rectangle(self.marker_image, pt1, pt2, cv.RGB(255, 255, 0)) # Create a filled white ellipse within the track_box to define the ROI cv2.ellipse(mask, mask_box, cv.CV_RGB(255, 255, 255), cv.CV_FILLED) if self.keypoints is not None: # Mask the current keypoints for x, y in [np.int32(p) for p in self.keypoints]: cv2.circle(mask, (x, y), 5, 0, -1) new_keypoints = cv2.goodFeaturesToTrack(self.grey, mask=mask, **self.gf_params) # Append new keypoints to the current list if they are not # too far from the current cluster if new_keypoints is not None: for x, y in np.float32(new_keypoints).reshape(-1, 2): distance = self.distance_to_cluster((x, y), self.keypoints) if distance > self.add_keypoint_distance: self.keypoints.append((x, y)) # Briefly display a blue disc where the new point is added if self.show_add_drop: cv2.circle(self.marker_image, (x, y), 3, (255, 255, 0, 0), cv.CV_FILLED, 2, 0) # Remove duplicate keypoints self.keypoints = list(set(self.keypoints))
trainloss, _ = sess.run( [loss, optimizer], feed_dict={pt_in: tpoints.astype('float32')}) trainloss, trainmatch = sess.run( [loss, match], feed_dict={pt_in: tpoints.astype('float32')}) #trainmatch=trainmatch.transpose((0,2,1)) show = np.zeros((400, 400, 3), dtype='uint8') ^ 255 trainmypoints = sess.run(mypoints) for i in range(len(tpoints[0])): u = np.random.choice(range(len(trainmypoints[0])), p=trainmatch[0].T[i]) cv2.line(show, (int(tpoints[0][i, 1] * 100 + 200), int(tpoints[0][i, 0] * 100 + 200)), (int(trainmypoints[0][u, 1] * 100 + 200), int(trainmypoints[0][u, 0] * 100 + 200)), cv2.CV_RGB(0, 255, 0)) for x, y, z in tpoints[0]: cv2.circle(show, (int(y * 100 + 200), int(x * 100 + 200)), 2, cv2.cv.CV_RGB(255, 0, 0)) for x, y, z in trainmypoints[0]: cv2.circle(show, (int(y * 100 + 200), int(x * 100 + 200)), 3, cv2.cv.CV_RGB(0, 0, 255)) cost = ((tpoints[0][:, None, :] - np.repeat( trainmypoints[0][None, :, :], 4, axis=1))**2).sum(axis=2)**0.5 #trueloss=bestmatch.bestmatch(cost)[0] print(trainloss) #,trueloss cv2.imshow('show', show) cmd = cv2.waitKey(10) % 256 if cmd == ord('q'): break
def process_image(self, slider_pos): global cimg, source_image1, ellipse_size, maxf, maxs, eoc, lastcx,lastcy,lastr """ This function finds contours, draws them and their approximation by ellipses. """ stor = cv.CreateMemStorage() # Create the destination images cimg = cv.CloneImage(self.source_image) cv.Zero(cimg) image02 = cv.CloneImage(self.source_image) cv.Zero(image02) image04 = cv.CreateImage(cv.GetSize(self.source_image), cv.IPL_DEPTH_8U, 3) cv.Zero(image04) # Threshold the source image. This needful for cv.FindContours(). cv.Threshold(self.source_image, image02, slider_pos, 255, cv.CV_THRESH_BINARY) # Find all contours. cont = cv.FindContours(image02, stor, cv.CV_RETR_LIST, cv.CV_CHAIN_APPROX_NONE, (0, 0)) maxf = 0 maxs = 0 size1 = 0 for c in contour_iterator(cont): if len(c) > ellipse_size: PointArray2D32f = cv.CreateMat(1, len(c), cv.CV_32FC2) for (i, (x, y)) in enumerate(c): PointArray2D32f[0, i] = (x, y) # Draw the current contour in gray gray = cv.CV_RGB(100, 100, 100) cv.DrawContours(image04, c, gray, gray,0,1,8,(0,0)) if iter == 0: strng = segF + '/' + 'contour1.png' cv.SaveImage(strng,image04) color = (255,255,255) (center, size, angle) = cv.FitEllipse2(PointArray2D32f) # Convert ellipse data from float to integer representation. center = (cv.Round(center[0]), cv.Round(center[1])) size = (cv.Round(size[0] * 0.5), cv.Round(size[1] * 0.5)) if iter == 1: if size[0] > size[1]: size2 = size[0] else: size2 = size[1] if size2 > size1: size1 = size2 size3 = size # Fits ellipse to current contour. if eoc == 0 and iter == 2: rand_val = abs((lastr - ((size[0]+size[1])/2))) if rand_val > 20 and float(max(size[0],size[1]))/float(min(size[0],size[1])) < 1.5: lastcx = center[0] lastcy = center[1] lastr = (size[0]+size[1])/2 if rand_val > 20 and float(max(size[0],size[1]))/float(min(size[0],size[1])) < 1.4: cv.Ellipse(cimg, center, size, angle, 0, 360, color,2, cv.CV_AA, 0) cv.Ellipse(source_image1, center, size, angle, 0, 360, color,2, cv.CV_AA, 0) elif eoc == 1 and iter == 2: (int,cntr,rad) = cv.MinEnclosingCircle(PointArray2D32f) cntr = (cv.Round(cntr[0]), cv.Round(cntr[1])) rad = (cv.Round(rad)) if maxf == 0 and maxs == 0: cv.Circle(cimg, cntr, rad, color, 1, cv.CV_AA, shift=0) cv.Circle(source_image1, cntr, rad, color, 2, cv.CV_AA, shift=0) maxf = rad elif (maxf > 0 and maxs == 0) and abs(rad - maxf) > 30: cv.Circle(cimg, cntr, rad, color, 2, cv.CV_AA, shift=0) cv.Circle(source_image1, cntr, rad, color, 2, cv.CV_AA, shift=0) maxs = len(c) if iter == 1: temp3 = 2*abs(size3[1] - size3[0]) if (temp3 > 40): eoc = 1