def update_mhi(img, dst, diff_threshold): global last global mhi global storage global mask global orient global segmask timestamp = time.clock() / CLOCKS_PER_SEC # get current time in seconds size = cv.GetSize(img) # get current frame size idx1 = last if not mhi or cv.GetSize(mhi) != size: for i in range(N): buf[i] = cv.CreateImage(size, cv.IPL_DEPTH_8U, 1) cv.Zero(buf[i]) mhi = cv.CreateImage(size,cv. IPL_DEPTH_32F, 1) cv.Zero(mhi) # clear MHI at the beginning orient = cv.CreateImage(size,cv. IPL_DEPTH_32F, 1) segmask = cv.CreateImage(size,cv. IPL_DEPTH_32F, 1) mask = cv.CreateImage(size,cv. IPL_DEPTH_8U, 1) cv.CvtColor(img, buf[last], cv.CV_BGR2GRAY) # convert frame to grayscale idx2 = (last + 1) % N # index of (last - (N-1))th frame last = idx2 silh = buf[idx2] cv.AbsDiff(buf[idx1], buf[idx2], silh) # get difference between frames cv.Threshold(silh, silh, diff_threshold, 1, cv.CV_THRESH_BINARY) # and threshold it cv.UpdateMotionHistory(silh, mhi, timestamp, MHI_DURATION) # update MHI cv.CvtScale(mhi, mask, 255./MHI_DURATION, (MHI_DURATION - timestamp)*255./MHI_DURATION) cv.Zero(dst) cv.Merge(mask, None, None, None, dst) cv.CalcMotionGradient(mhi, mask, orient, MAX_TIME_DELTA, MIN_TIME_DELTA, 3) if not storage: storage = cv.CreateMemStorage(0) seq = cv.SegmentMotion(mhi, segmask, storage, timestamp, MAX_TIME_DELTA) for (area, value, comp_rect) in seq: if comp_rect[2] + comp_rect[3] > 100: # reject very small components color = cv.CV_RGB(255, 0,0) silh_roi = cv.GetSubRect(silh, comp_rect) mhi_roi = cv.GetSubRect(mhi, comp_rect) orient_roi = cv.GetSubRect(orient, comp_rect) mask_roi = cv.GetSubRect(mask, comp_rect) angle = 360 - cv.CalcGlobalOrientation(orient_roi, mask_roi, mhi_roi, timestamp, MHI_DURATION) count = cv.Norm(silh_roi, None, cv.CV_L1, None) # calculate number of points within silhouette ROI if count < (comp_rect[2] * comp_rect[3] * 0.05): continue magnitude = 30. center = ((comp_rect[0] + comp_rect[2] / 2), (comp_rect[1] + comp_rect[3] / 2)) cv.Circle(dst, center, cv.Round(magnitude*1.2), color, 3, cv.CV_AA, 0) cv.Line(dst, center, (cv.Round(center[0] + magnitude * cos(angle * cv.CV_PI / 180)), cv.Round(center[1] - magnitude * sin(angle * cv.CV_PI / 180))), color, 3, cv.CV_AA, 0)
def process_image(self, slider_pos): """ This function finds contours, draws them and their approximation by ellipses. """ stor = cv.CreateMemStorage() # Create the destination images 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)) for c in contour_iterator(cont): # Number of points must be more than or equal to 6 for cv.FitEllipse2 if len(c) >= 6: # Copy the contour into an array of (x,y)s 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)) # Fits ellipse to current contour. (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)) # Draw ellipse in random color color = cv.CV_RGB(random.randrange(256), random.randrange(256), random.randrange(256)) cv.Ellipse(image04, center, size, angle, 0, 360, color, 2, cv.CV_AA, 0) # Show image. HighGUI use. cv.ShowImage("Result", image04)
def _mixImageAlphaMask(self, wipeSettings, level, image1, image2, image2mask, mixMat): if(level < 0.99): wipeMode, wipePostMix, wipeConfig = wipeSettings if((wipeMode == WipeMode.Fade) or (wipeMode == WipeMode.Default)): valueCalc = int(256 * (1.0 - level)) rgbColor = cv.CV_RGB(valueCalc, valueCalc, valueCalc) whiteColor = cv.CV_RGB(255, 255, 255) cv.Set(mixMat, whiteColor) cv.Set(mixMat, rgbColor, image2mask) cv.Mul(image1, mixMat, image1, 0.004) valueCalc = int(256 * level) rgbColor = cv.CV_RGB(valueCalc, valueCalc, valueCalc) cv.Zero(mixMat) cv.Set(mixMat, rgbColor, image2mask) cv.Mul(image2, mixMat, image2, 0.004) cv.Add(image1, image2, image1) return image1 else: if(wipePostMix == False): image2, image2mask = self._wipeImage(wipeMode, wipeConfig, level, image2, image2mask, mixMat, False) cv.Copy(image2, image1, image2mask) return image1 else: cv.Copy(image1, mixMat) cv.Copy(image2, mixMat, image2mask) return self._wipeMix(wipeMode, wipeConfig, level, image1, mixMat, image2) cv.Copy(image2, image1, image2mask) return image1
def rectif(self, image, cadreIn, cadreOut): prev_image = cv.CloneImage(image) cv.Zero(image) mmat = cv.CreateMat(3, 3, cv.CV_32FC1) print("mmat= %s" % repr(mmat)) cv.GetPerspectiveTransform(cadreIn, cadreOut, mmat) cv.WarpPerspective(prev_image, image, mmat) #, flags=cv.CV_WARP_INVERSE_MAP )
def draw_color_contours(self, color_contours, color=None): '''Takes in list of (contour, color) tuples where contour is iterable for (x, y) tuples''' gray_img = cv.CreateImage(cv.GetSize(self.image), 8, 1) #cv.CvtColor( self.image, gray_img, cv.CV_BGR2GRAY ) cv.Zero(gray_img) for (contour, color) in color_contours: cv.PolyLine(gray_img, [contour], True, color) cv.ShowImage("Contours", gray_img) cv.WaitKey()
def get_working(self): (width, height) = cv.GetSize(self.image) dest = cv.CreateMat(height, width, cv.CV_8UC3) mask8x1 = cv.CreateImage(cv.GetSize(self.image), 8, 1) cv.Zero(mask8x1) cv.FillConvexPoly(mask8x1, self.cur_contour, cv.ScalarAll(255)) # Could 8x3 mask copy but histogram mask will take care of it cv.Copy(self.image, dest) return (dest, mask8x1)
def gen_mask(frec, tpoly): mask = cv.CreateMat(int(np.round(frec.height())), int(np.round(frec.width())), cv.CV_8UC1) cv.Zero(mask) pts = tuple((int(np.round(x - frec.left)), int(np.round(y - frec.top))) for (x, y) in tpoly.contour(0)) cv.FillPoly(mask, [pts], cv.Scalar(1), lineType=8, shift=0) return np.asarray(mask)
def minarea_seq(img, count, storage): points = [(randint(img.width / 4, img.width * 3 / 4), randint(img.height / 4, img.height * 3 / 4)) for i in range(count)] cv.Zero(img) for p in points: cv.Circle(img, roundxy(p), 2, cv.CV_RGB(255, 0, 0), cv.CV_FILLED, cv.CV_AA, 0) draw_common(points)
def test_2686307(self): lena = cv.LoadImage(find_sample("lena.jpg"), 1) dst = cv.CreateImage((512,512), 8, 3) cv.Set(dst, (128,192,255)) mask = cv.CreateImage((512,512), 8, 1) cv.Zero(mask) cv.Rectangle(mask, (10,10), (300,100), 255, -1) cv.Copy(lena, dst, mask) self.snapL([lena, dst, mask]) m = cv.CreateMat(480, 640, cv.CV_8UC1) print "ji", m print m.rows, m.cols, m.type, m.step
def compute_ref(self): '''Compute a reference histogram that matched regions should approximate''' (image, polygon) = self.get_ref() (width, height) = cv.GetSize(image) # (rows, cols,...) dest = cv.CreateMat(height, width, cv.CV_8UC3) mask8x1 = cv.CreateImage(cv.GetSize(image), 8, 1) cv.Zero(mask8x1) cv.FillConvexPoly(mask8x1, polygon, cv.ScalarAll(255)) cv.Copy(image, dest) self.ref_hist = hs_histogram(dest, mask8x1)
def minarea_array(img, count): pointMat = cv.CreateMat(count, 1, cv.CV_32SC2) for i in range(count): pointMat[i, 0] = (randint(img.width / 4, img.width * 3 / 4), randint(img.height / 4, img.height * 3 / 4)) cv.Zero(img) for i in range(count): cv.Circle(img, roundxy(pointMat[i, 0]), 2, cv.CV_RGB(255, 0, 0), cv.CV_FILLED, cv.CV_AA, 0) draw_common(pointMat)
def undistort(self, img): # intrinsic parameters fx = self.C[0, 0] fy = self.C[1, 1] cx = self.C[0, 2] cy = self.C[1, 2] # radial distortion coefficients k1, k2 = self.dist[0:2] # †angential distortion coefficients p1, p2 = self.dist[2:4] intrinsics = cv.CreateMat(3, 3, cv.CV_64FC1) cv.Zero(intrinsics) intrinsics[0, 0] = float(fx) intrinsics[1, 1] = float(fy) intrinsics[2, 2] = 1.0 intrinsics[0, 2] = float(cx) intrinsics[1, 2] = float(cy) dist_coeffs = cv.CreateMat(1, 4, cv.CV_64FC1) cv.Zero(dist_coeffs) dist_coeffs[0, 0] = float(k1) dist_coeffs[0, 1] = float(k2) dist_coeffs[0, 2] = float(p1) dist_coeffs[0, 3] = float(p2) #src = cv.LoadImage(src) src = cv.fromarray(img) dst = img.copy() dst = cv.fromarray(dst) #dst = cv.CreateImage(cv.GetSize(src), src.type, src.channels) mapx = cv.CreateImage(cv.GetSize(src), cv.IPL_DEPTH_32F, 1) mapy = cv.CreateImage(cv.GetSize(src), cv.IPL_DEPTH_32F, 1) cv.InitUndistortMap(intrinsics, dist_coeffs, mapx, mapy) cv.Remap(src, dst, mapx, mapy, cv.CV_INTER_LINEAR + cv.CV_WARP_FILL_OUTLIERS, cv.ScalarAll(0)) # cv.Undistort2(src,dst, intrinsics, dist_coeffs) return np.array(dst)
def draw_contour(self, contour, color=None): gray_img = cv.CreateImage(cv.GetSize(self.image), 8, 1) #cv.CvtColor( self.image, gray_img, cv.CV_BGR2GRAY ) cv.Zero(gray_img) # DrawContours(img, contour, external_color, hole_color, max_level [, thickness [, lineType [, offset]]]) -> None # in my small example external didn't get value but internal did # void cvPolyLine(CvArr* img, # CvPoint** pts, int* npts, # int contours, int is_closed, CvScalar color, # int thickness=1, int lineType=8, int shift=0) if color is None: # White for default black background #color = cv.CV_RGB(255, 255, 255) color = cv.ScalarAll(255) cv.PolyLine(gray_img, [contour], True, color) cv.ShowImage("Contours", gray_img) cv.WaitKey()
def Process(image, pos_var, pos_w, pos_phase, pos_psi): global kernel_size if kernel_size % 2 == 0: kernel_size += 1 kernel = cv.CreateMat(kernel_size, kernel_size, cv.CV_32FC1) # kernelimg = cv.CreateImage((kernel_size,kernel_size),cv.IPL_DEPTH_32F,1) # big_kernelimg = cv.CreateImage((kernel_size*20,kernel_size*20),cv.IPL_DEPTH_32F,1) src = cv.CreateImage((image.width, image.height), cv.IPL_DEPTH_8U, 1) src_f = cv.CreateImage((image.width, image.height), cv.IPL_DEPTH_32F, 1) # src = image #cv.CvtColor(image,src,cv.CV_BGR2GRAY) #no conversion is needed if cv.GetElemType(image) == cv.CV_8UC3: cv.CvtColor(image, src, cv.CV_BGR2GRAY) else: src = image cv.ConvertScale(src, src_f, 1.0 / 255, 0) dest = cv.CloneImage(src_f) dest_mag = cv.CloneImage(src_f) var = pos_var / 10.0 w = pos_w / 10.0 phase = pos_phase * cv.CV_PI / 180.0 psi = cv.CV_PI * pos_psi / 180.0 cv.Zero(kernel) for x in range(-kernel_size / 2 + 1, kernel_size / 2 + 1): for y in range(-kernel_size / 2 + 1, kernel_size / 2 + 1): kernel_val = math.exp(-( (x * x) + (y * y)) / (2 * var)) * math.cos(w * x * math.cos(phase) + w * y * math.sin(phase) + psi) cv.Set2D(kernel, y + kernel_size / 2, x + kernel_size / 2, cv.Scalar(kernel_val)) # cv.Set2D(kernelimg,y+kernel_size/2,x+kernel_size/2,cv.Scalar(kernel_val/2+0.5)) cv.Filter2D(src_f, dest, kernel, (-1, -1)) # cv.Resize(kernelimg,big_kernelimg) cv.Pow(dest, dest_mag, 2) # return (dest_mag, big_kernelimg, dest) return (dest_mag, dest) # cv.ShowImage("Mag",dest_mag) # cv.ShowImage("Kernel",big_kernelimg) # cv.ShowImage("Process window",dest)
def findImage(img): #Set up storage for images frame_size = cv.GetSize(img) img2 = cv.CreateImage(frame_size,8,3) tmp = cv.CreateImage(frame_size,8,cv.CV_8U) h = cv.CreateImage(frame_size,8,1) #copy original image to do work on cv.Copy(img,img2) #altering the image a bit for smoother processing cv.Smooth(img2,img2,cv.CV_BLUR,3) cv.CvtColor(img2,img2,cv.CV_BGR2HSV) #make sure temp is empty cv.Zero(tmp) #detection based on HSV value #30,100,90 lower limit on pic 41,255,255 on pic #cv.InRangeS(img2,cv.Scalar(25,100,87),cv.Scalar(50,255,255),tmp) #Range for green plate dot in my Living room #cv.InRangeS(img2,cv.Scalar(55,80,60),cv.Scalar(65,95,90),tmp) #classroom #cv.InRangeS(img2,cv.Scalar(55,80,60),cv.Scalar(70,110,70),tmp) #Kutztowns Gym cv.InRangeS(img2,cv.Scalar(65,100,112),cv.Scalar(85,107,143),tmp) elmt_shape=cv.CV_SHAPE_ELLIPSE pos = 3 element = cv.CreateStructuringElementEx(pos*2+1, pos*2+1, pos, pos, elmt_shape) cv.Dilate(tmp,tmp,element,6) cv.Erode(tmp,tmp,element,2) cv.Split(tmp,h,None,None,None) storage = cv.CreateMemStorage() scan = sc.FindContours(h,storage) xyImage=drawCircles(scan,img) if xyImage != None: return (xyImage,tmp) else: return None
def crack(tocrack,withContourImage=False): #Function that intent to release all characters on the image so that the ocr can detect them #We just apply 4 filters but with multiples rounds resized = resizeImage(tocrack, (tocrack.width*6, tocrack.height*6)) dilateImage(resized, 4) erodeImage(resized, 4) thresholdImage(resized, 200, cv.CV_THRESH_BINARY) if withContourImage: #If we want the image made only with contours contours = getContours(resized, 5) contourimage = cv.CreateImage(cv.GetSize(resized), 8, 3) cv.Zero(contourimage) cv.DrawContours(contourimage, contours, cv.Scalar(255), cv.Scalar(255), 2, cv.CV_FILLED) contourimage = resizeImage(contourimage, cv.GetSize(tocrack)) resized = resizeImage(resized, cv.GetSize(tocrack)) return resized, contourimage resized = resizeImage(resized, cv.GetSize(tocrack)) return resized
def test_2542670(self): xys = [(94, 121), (94, 122), (93, 123), (92, 123), (91, 124), (91, 125), (91, 126), (92, 127), (92, 128), (92, 129), (92, 130), (92, 131), (91, 132), (90, 131), (90, 130), (90, 131), (91, 132), (92, 133), (92, 134), (93, 135), (94, 136), (94, 137), (94, 138), (95, 139), (96, 140), (96, 141), (96, 142), (96, 143), (97, 144), (97, 145), (98, 146), (99, 146), (100, 146), (101, 146), (102, 146), (103, 146), (104, 146), (105, 146), (106, 146), (107, 146), (108, 146), (109, 146), (110, 146), (111, 146), (112, 146), (113, 146), (114, 146), (115, 146), (116, 146), (117, 146), (118, 146), (119, 146), (120, 146), (121, 146), (122, 146), (123, 146), (124, 146), (125, 146), (126, 146), (126, 145), (126, 144), (126, 143), (126, 142), (126, 141), (126, 140), (127, 139), (127, 138), (127, 137), (127, 136), (127, 135), (127, 134), (127, 133), (128, 132), (129, 132), (130, 131), (131, 130), (131, 129), (131, 128), (132, 127), (133, 126), (134, 125), (134, 124), (135, 123), (136, 122), (136, 121), (135, 121), (134, 121), (133, 121), (132, 121), (131, 121), (130, 121), (129, 121), (128, 121), (127, 121), (126, 121), (125, 121), (124, 121), (123, 121), (122, 121), (121, 121), (120, 121), (119, 121), (118, 121), (117, 121), (116, 121), (115, 121), (114, 121), (113, 121), (112, 121), (111, 121), (110, 121), (109, 121), (108, 121), (107, 121), (106, 121), (105, 121), (104, 121), (103, 121), (102, 121), (101, 121), (100, 121), (99, 121), (98, 121), (97, 121), (96, 121), (95, 121)] #xys = xys[:12] + xys[16:] pts = cv.CreateMat(len(xys), 1, cv.CV_32SC2) for i,(x,y) in enumerate(xys): pts[i,0] = (x, y) storage = cv.CreateMemStorage() hull = cv.ConvexHull2(pts, storage) hullp = cv.ConvexHull2(pts, storage, return_points = 1) defects = cv.ConvexityDefects(pts, hull, storage) vis = cv.CreateImage((1000,1000), 8, 3) x0 = min([x for (x,y) in xys]) - 10 x1 = max([x for (x,y) in xys]) + 10 y0 = min([y for (y,y) in xys]) - 10 y1 = max([y for (y,y) in xys]) + 10 def xform(pt): x,y = pt return (1000 * (x - x0) / (x1 - x0), 1000 * (y - y0) / (y1 - y0)) for d in defects[:2]: cv.Zero(vis) # First draw the defect as a red triangle cv.FillConvexPoly(vis, [xform(p) for p in d[:3]], cv.RGB(255,0,0)) # Draw the convex hull as a thick green line for a,b in zip(hullp, hullp[1:]): cv.Line(vis, xform(a), xform(b), cv.RGB(0,128,0), 3) # Draw the original contour as a white line for a,b in zip(xys, xys[1:]): cv.Line(vis, xform(a), xform(b), (255,255,255)) self.snap(vis)
c = cv.WaitKey(0) % 0x100 if c == 27: print("Exiting ...") sys.exit(0) elif c == ord('c'): if (is_color): print("Grayscale mode is set") cv.CvtColor(color_img, gray_img, cv.CV_BGR2GRAY) is_color = 0 else: print("Color mode is set") cv.Copy(im, color_img, None) cv.Zero(mask) is_color = 1 elif c == ord('m'): if (is_mask): cv.DestroyWindow("mask") is_mask = 0 else: cv.NamedWindow("mask", 0) cv.Zero(mask) cv.ShowImage("mask", mask) is_mask = 1 elif c == ord('r'): print("Original image is restored")
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
import cv2.cv as cv from math import cos, sin, sqrt import sys if __name__ == "__main__": A = [[1, 1], [0, 1]] img = cv.CreateImage((500, 500), 8, 3) kalman = cv.CreateKalman(2, 1, 0) state = cv.CreateMat(2, 1, cv.CV_32FC1) # (phi, delta_phi) process_noise = cv.CreateMat(2, 1, cv.CV_32FC1) measurement = cv.CreateMat(1, 1, cv.CV_32FC1) rng = cv.RNG(-1) code = -1L cv.Zero(measurement) cv.NamedWindow("Kalman", 1) while True: cv.RandArr(rng, state, cv.CV_RAND_NORMAL, cv.RealScalar(0), cv.RealScalar(0.1)) kalman.transition_matrix[0, 0] = 1 kalman.transition_matrix[0, 1] = 1 kalman.transition_matrix[1, 0] = 0 kalman.transition_matrix[1, 1] = 1 cv.SetIdentity(kalman.measurement_matrix, cv.RealScalar(1)) cv.SetIdentity(kalman.process_noise_cov, cv.RealScalar(1e-5)) cv.SetIdentity(kalman.measurement_noise_cov, cv.RealScalar(1e-1)) cv.SetIdentity(kalman.error_cov_post, cv.RealScalar(1))
img0 = cv.DecodeImage(imagefiledata, cv.CV_LOAD_IMAGE_COLOR) print "Hot keys:" print "\tESC - quit the program" print "\tr - restore the original image" print "\ti or ENTER - run inpainting algorithm" print "\t\t(before running it, paint something on the image)" cv.NamedWindow("image", 1) cv.NamedWindow("inpainted image", 1) img = cv.CloneImage(img0) inpainted = cv.CloneImage(img0) inpaint_mask = cv.CreateImage(cv.GetSize(img), 8, 1) cv.Zero(inpaint_mask) cv.Zero(inpainted) cv.ShowImage("image", img) cv.ShowImage("inpainted image", inpainted) sk = Sketcher("image", [img, inpaint_mask]) while True: c = cv.WaitKey(0) % 0x100 if c == 27 or c == ord('q'): break if c == ord('r'): cv.Zero(inpaint_mask) cv.Copy(img0, img) cv.ShowImage("image", img)
# # Works with OpenCV 2.3.1 cv.NamedWindow("imgOriginal", 1) cv.NamedWindow("imgMarkers", 1) cv.NamedWindow("imgWatershed", 1) imgOriginal = cv.LoadImage("TouchingCircles.png") imgMarkers = cv.CreateImage(cv.GetSize(imgOriginal), cv.IPL_DEPTH_8U, 1) imgWatershed = cv.CreateImage(cv.GetSize(imgOriginal), cv.IPL_DEPTH_32S, 1) cv.Zero(imgMarkers) cv.Zero(imgWatershed) # create markers for the background, upper circle, and lower circle # markers are differentiated by colour # background marker, identified by colour (80, 80, 80) cv.Circle(imgMarkers, (60, 60), 3, (80, 80, 80), -1) # upper circle marker, identified by colour (160, 160, 160) cv.Circle(imgMarkers, (180, 180), 3, (160, 160, 160), -1) # lower circle marker, identified by colour (240, 240, 240) cv.Circle(imgMarkers, (320, 320), 3, (240, 240, 240), -1)
print "\tw - run watershed algorithm" print "\t (before that, roughly outline several markers on the image)" cv.NamedWindow("image", 1) cv.NamedWindow("watershed transform", 1) img = cv.CloneImage(img0) img_gray = cv.CloneImage(img0) wshed = cv.CloneImage(img0) marker_mask = cv.CreateImage(cv.GetSize(img), 8, 1) markers = cv.CreateImage(cv.GetSize(img), cv.IPL_DEPTH_32S, 1) cv.CvtColor(img, marker_mask, cv.CV_BGR2GRAY) cv.CvtColor(marker_mask, img_gray, cv.CV_GRAY2BGR) cv.Zero(marker_mask) cv.Zero(wshed) cv.ShowImage("image", img) cv.ShowImage("watershed transform", wshed) sk = Sketcher("image", [img, marker_mask]) while True: c = cv.WaitKey(0) % 0x100 if c == 27 or c == ord('q'): break if c == ord('r'): cv.Zero(marker_mask) cv.Copy(img0, img) cv.ShowImage("image", img)
orig = cv.LoadImage("robin2.png") #Convert in black and white res = cv.CreateImage(cv.GetSize(orig), 8, 1) cv.CvtColor(orig, res, cv.CV_BGR2GRAY) #Operations on the image openCloseImage(res) dilateImage(res, 2) erodeImage(res, 2) smoothImage(res, 5) thresholdImage(res, 150, cv.CV_THRESH_BINARY_INV) #Get contours approximated contourLow = getContours(res, 3) #Draw them on an empty image final = cv.CreateImage(cv.GetSize(res), 8, 1) cv.Zero(final) cv.DrawContours(final, contourLow, cv.Scalar(255), cv.Scalar(255), 2, cv.CV_FILLED) cv.ShowImage("orig", orig) cv.ShowImage("image", res) cv.SaveImage("modified.png", res) cv.ShowImage("contour", final) cv.SaveImage("contour.png", final) cv.WaitKey(0)
if __name__ == "__main__": motion = 0 capture = 0 if len(sys.argv)==1: capture = cv.CreateCameraCapture(0) elif len(sys.argv)==2 and sys.argv[1].isdigit(): capture = cv.CreateCameraCapture(int(sys.argv[1])) elif len(sys.argv)==2: capture = cv.CreateFileCapture(sys.argv[1]) if not capture: print "Could not initialize capturing..." sys.exit(-1) cv.NamedWindow("Motion", 1) while True: image = cv.QueryFrame(capture) if(image): if(not motion): motion = cv.CreateImage((image.width, image.height), 8, 3) cv.Zero(motion) #motion.origin = image.origin update_mhi(image, motion, 30) cv.ShowImage("Motion", motion) if(cv.WaitKey(10) != -1): break else: break cv.DestroyWindow("Motion")
def _wipeImage(self, wipeMode, wipeConfig, level, image, imageMask, mixMat, whiteMode = False): if((wipeMode == WipeMode.Push)): wipeDirection = wipeConfig if(whiteMode == True): cv.Set(mixMat, (255,255,255)) else: cv.SetZero(mixMat) if(wipeDirection < 0.25): wipePosX = int(self._internalResolutionX * level) sourceLeft = self._internalResolutionX-wipePosX sourceTop = 0 sourceWidth = wipePosX sourceHeight = self._internalResolutionY destLeft = 0 destTop = 0 elif(wipeDirection < 0.5): wipePosX = self._internalResolutionX - int(self._internalResolutionX * level) sourceLeft = 0 sourceTop = 0 sourceWidth = self._internalResolutionX-wipePosX sourceHeight = self._internalResolutionY destLeft = self._internalResolutionX-(self._internalResolutionX-wipePosX) destTop = 0 elif(wipeDirection < 0.75): wipePosY = int(self._internalResolutionY * level) sourceLeft = 0 sourceTop = self._internalResolutionY-wipePosY sourceWidth = self._internalResolutionX sourceHeight = wipePosY destLeft = 0 destTop = 0 else: wipePosY = self._internalResolutionY - int(self._internalResolutionY * level) sourceLeft = 0 sourceTop = 0 sourceWidth = self._internalResolutionX sourceHeight = self._internalResolutionY-wipePosY destLeft = 0 destTop = self._internalResolutionY-(self._internalResolutionY-wipePosY) destWidth = sourceWidth destHeight = sourceHeight src_region = cv.GetSubRect(image, (sourceLeft, sourceTop, sourceWidth, sourceHeight)) dst_region = cv.GetSubRect(mixMat, (destLeft, destTop, destWidth, destHeight)) cv.Copy(src_region, dst_region) cv.Zero(self._mixMixMask1) dst_region = cv.GetSubRect(self._mixMixMask1, (destLeft, destTop, destWidth, destHeight)) cv.Set(dst_region, 255) return mixMat, self._mixMixMask1 if(wipeMode == WipeMode.Noize): scaleArg = wipeConfig cv.Copy(image, mixMat) noizeMask = getNoizeMask(level, self._internalResolutionX, self._internalResolutionY, 1.0 + (19.0 * scaleArg)) if(whiteMode == True): cv.Set(image, (255,255,255)) else: cv.Set(image, (0,0,0)) cv.Copy(mixMat, image, noizeMask) return image, noizeMask if(wipeMode == WipeMode.Zoom): xMove, yMove = wipeConfig xSize = int(self._internalResolutionX * level) ySize = int(self._internalResolutionY * level) xPos = int((self._internalResolutionX - xSize) * xMove) yPos = int((self._internalResolutionY - ySize) * (1.0 - yMove)) if(whiteMode == True): cv.Set(mixMat, (255,255,255)) else: cv.SetZero(mixMat) dst_region = cv.GetSubRect(mixMat, (xPos, yPos, xSize, ySize)) cv.Resize(image, dst_region,cv.CV_INTER_CUBIC) cv.Copy(mixMat, image) cv.SetZero(self._mixMixMask1) if(imageMask == None): dst_region = cv.GetSubRect(self._mixMixMask1, (xPos, yPos, xSize, ySize)) cv.Set(dst_region, 255) else: dst_region = cv.GetSubRect(self._mixMixMask1, (xPos, yPos, xSize, ySize)) cv.Resize(imageMask, dst_region,cv.CV_INTER_CUBIC) return image, self._mixMixMask1 if(wipeMode == WipeMode.Flip): flipRotation = wipeConfig rotation = 1.0 - level srcPoints = ((0.0, 0.0),(0.0,self._internalResolutionY),(self._internalResolutionX, 0.0)) destPoint1 = (0.0, 0.0) destPoint2 = (0.0, self._internalResolutionY) destPoint3 = (self._internalResolutionX, 0.0) flipAngle = rotation / 4 destPoint1 = rotatePoint(flipRotation, destPoint1[0], destPoint1[1], self._halfResolutionX, self._halfResolutionY, flipAngle) destPoint2 = rotatePoint(flipRotation, destPoint2[0], destPoint2[1], self._halfResolutionX, self._halfResolutionY, flipAngle) destPoint3 = rotatePoint(flipRotation, destPoint3[0], destPoint3[1], self._halfResolutionX, self._halfResolutionY, flipAngle) dstPoints = ((destPoint1[0], destPoint1[1]),(destPoint2[0], destPoint2[1]),(destPoint3[0],destPoint3[1])) zoomMatrix = cv.CreateMat(2,3,cv.CV_32F) # print "DEBUG pcn: trasform points source: " + str(srcPoints) + " dest: " + str(dstPoints) cv.GetAffineTransform(srcPoints, dstPoints, zoomMatrix) cv.WarpAffine(image, mixMat, zoomMatrix) cv.Set(self._mixMixMask2, (255,255,255)) cv.WarpAffine(self._mixMixMask2, self._mixMixMask1, zoomMatrix) return mixMat, self._mixMixMask1 calcMaskValue = 255 - (255* level) cv.Set(self._mixMixMask1, (calcMaskValue, calcMaskValue, calcMaskValue)) return image, self._mixMixMask1
import cv2.cv as cv im = cv.LoadImage("../img/lena.jpg", 3) cv.SetImageROI(im, (50, 50, 150, 150)) cv.Zero(im) #cv.Set(im, cv.RGB(100, 100, 100)) cv.ResetImageROI(im) cv.ShowImage("Image", im) cv.WaitKey(0)
def gen_sweep_plane(src_Hs, src_frames): max_poly = None # the bound of the blended image bldIm = None # the blended plane image cost_info = None # An array structure to collect pixel colors of # blended frames nframes = len(src_frames) o = np.array([[0], [0], [1]]) # origin = zero # Get max polygon for k in range(nframes): cpoly = Rect(Point(0, 0), Point(src_frames[k].shape[1], src_frames[k].shape[0])).get_trans_regpoly( src_Hs[k], 10) if (k == 0): max_poly = cpoly else: max_poly = max_poly | cpoly #end if #end for (xmin, xmax, ymin, ymax) = max_poly.boundingBox(0) max_rec = Rect(Point(xmin, ymin), Point(xmax, ymax)) #print "Max: " , max_rec, "W: ", max_rec.width(), " H: ", max_rec.height() #alocate costs and count matrixes cost_info = np.zeros( [int(max_rec.height()), int(max_rec.width()), nframes, 3], dtype=np.int) counts = np.zeros( [int(max_rec.height()), int(max_rec.width())], dtype=np.int) bldIm = cv.CreateMat(int(np.round(max_rec.height())), int(np.round(max_rec.width())), cv.CV_32FC3) cv.Zero(bldIm) for k in range(nframes): cur_H = src_Hs[k] cur_o = np.dot(cur_H, o) # translate the warped frame to origin from topleft corner disp = np.array([[1, 0, (0 - cur_o[0, 0]) / cur_o[2, 0]], [0, 1, (0 - cur_o[1, 0]) / cur_o[2, 0]], [0, 0, 1]]) o_H = np.dot(disp, cur_H) tpoly = Rect(Point(0, 0), Point(src_frames[k].shape[1], src_frames[k].shape[0])).get_trans_poly(cur_H) cpoly = Rect(Point(0, 0), Point(src_frames[k].shape[1], src_frames[k].shape[0])).get_trans_regpoly( cur_H, 10) (xmin, xmax, ymin, ymax) = cpoly.boundingBox(0) frec = Rect(Point(xmin, ymin), Point(xmax, ymax)) mask = gen_mask(frec, tpoly) #print "Rec: ", frec, "W: ", frec.width(), " H: ", frec.height() if k == 0: fwarp = cv2.warpPerspective( src_frames[k], o_H, (int(frec.width()), int(frec.height()))) # get blended image blend_views(bldIm, fwarp, mask, frec, max_rec) # determine costs collect_costs_info(cost_info, counts, fwarp, mask, frec, max_rec, k) else: fwarp = cv2.warpPerspective( src_frames[k], o_H, (int(frec.width()), int(frec.height()))) # get blended image blend_views(bldIm, fwarp, mask, frec, max_rec) # determine costs collect_costs_info(cost_info, counts, fwarp, mask, frec, max_rec, k) #end if #end for bldIm = np.asarray(bldIm) # Scale blended image to 8U bldIm8U = scaleTo8U(bldIm, counts) ## Measure Cost costs = None costs = calculate_costs(cost_info, counts) ##return blended image and costs return (bldIm8U, costs, max_rec)
moments = cv.Moments(cv.GetMat(imgYellowTresh), 1) moment10 = cv.GetSpatialMoment(moments, 1, 0) moment01 = cv.GetSpatialMoment(moments, 0, 1) area = cv.GetCentralMoment(moments, 0, 0) #Get the center lastx = posx lasty = posy if area == 0: posx = 0 posy = 0 else: posx = moment10 / area posy = moment01 / area if lastx > 0 and lasty > 0 and posx > 0 and posy > 0: #Mean we have received coordinates to print #Draw the line cv.Line(imgScribble, (int(posx), int(posy)), (int(lastx), int(lasty)), cv.Scalar(0, 255, 255), 3, 1) #Add the frame and the line image to see lines on the webcam frame cv.Add(frame, imgScribble, frame) cv.ShowImage("video", frame) cv.ShowImage("thresh", imgYellowTresh) c = cv.WaitKey(1) if c == 27 or c == 1048603: #Break if user enters 'Esc'. break elif c == 1048690: # 'r' for reset cv.Zero(imgScribble)
if k != cluster_count: last = (k + 1) * sample_count / cluster_count point_chunk = cv.GetRows(points, first, last) cv.RandArr(rng, point_chunk, cv.CV_RAND_NORMAL, cv.Scalar(center[0], center[1], 0, 0), cv.Scalar(img.width * 0.1, img.height * 0.1, 0, 0)) # shuffle samples cv.RandShuffle(points, rng) cv.KMeans2(points, cluster_count, clusters, (cv.CV_TERMCRIT_EPS + cv.CV_TERMCRIT_ITER, 10, 1.0)) cv.Zero(img) for i in range(sample_count): cluster_idx = int(clusters[i, 0]) pt = (cv.Round(points[i, 0][0]), cv.Round(points[i, 0][1])) cv.Circle(img, pt, 2, color_tab[cluster_idx], cv.CV_FILLED, cv.CV_AA, 0) cv.ShowImage("clusters", img) key = cv.WaitKey(0) % 0x100 if key in [27, ord('q'), ord('Q')]: break cv.DestroyWindow("clusters")