def show_image(): global Target global Grid global Display_Grid global Display_Original global Img global Display global Display_Data if Display_Original: Display = cv.CloneImage(Img) else: Display = cv.CloneImage(Target) if Blank_Image: Display = cv.CloneImage(Blank) if Display_Grid: cv.Or(Display, Grid, Display) if Display_Peephole: cv.And(Display, Peephole, Display) if Display_Data: show_data() cv.Or(Display, Hex, Display) cv.ShowImage("rompar %s" % sys.argv[1], Display)
def cvAnd(A, B, dst): width1 = cv.GetImageROI(A)[2] height1 = cv.GetImageROI(A)[3] width2 = cv.GetImageROI(B)[2] height2 = cv.GetImageROI(B)[3] width3 = cv.GetImageROI(dst)[2] height3 = cv.GetImageROI(dst)[3] if not (width1 == width3 and height1 == height3 and A.depth == dst.depth): print 'cvAnd argument error: size, type of src1, dst' print (width1, height1, A.depth), (width3, height3, dst.depth) raise RuntimeError cv.And(A,B,dst)
def do_loop(self): # image processing if self.config.threshold: cv.Threshold(self.img_original, self.img_target, self.config.pix_thresh_min, 0xff, cv.CV_THRESH_BINARY) cv.And(self.img_target, self.img_mask, self.img_target) if self.config.dilate: cv.Dilate(self.img_target, self.img_target, iterations=self.config.dilate) if self.config.erode: cv.Erode(self.img_target, self.img_target, iterations=self.config.erode) show_image(self) sys.stdout.write('> ') sys.stdout.flush() # keystroke processing ki = cv.WaitKey(0) # Simple character value, if applicable kc = None # Char if a common char, otherwise the integer code k = ki if 0 <= ki < 256: kc = chr(ki) k = kc elif 65506 < ki < 66000 and ki != 65535: ki2 = ki - 65506 - 30 # modifier keys if ki2 >= 0: kc = chr(ki2) k = kc if kc: print '%d (%s)\n' % (ki, kc) else: print '%d\n' % ki if ki > 66000: return if ki < 0: print "Exiting on closed window" self.running = False return on_key(self, k)
def show_image(self): if self.config.img_display_original: self.img_display = cv.CloneImage(self.img_original) else: self.img_display = cv.CloneImage(self.img_target) if self.config.img_display_blank_image: self.img_display = cv.CloneImage(self.img_blank) if self.config.img_display_grid: cv.Or(self.img_display, self.img_grid, self.img_display) if self.config.img_display_peephole: cv.And(self.img_display, self.img_peephole, self.img_display) if self.config.img_display_data: show_data(self) cv.Or(self.img_display, self.img_hex, self.img_display) self.img_display_viewport = self.img_display[ self.config.view.y:self.config.view.y + self.config.view.h, self.config.view.x:self.config.view.x + self.config.view.w] cv.ShowImage(self.title, self.img_display_viewport)
dilated = cv.CloneImage(dst_32f) cv.Dilate( dst_32f, dilated ) # By this way we are sure that pixel with local max value will not be changed, and all the others will localMax = cv.CreateMat(dst_32f.height, dst_32f.width, cv.CV_8U) cv.Cmp( dst_32f, dilated, localMax, cv.CV_CMP_EQ ) #compare allow to keep only non modified pixel which are local maximum values which are corners. threshold = 0.01 * maxv cv.Threshold(dst_32f, dst_32f, threshold, 255, cv.CV_THRESH_BINARY) cornerMap = cv.CreateMat(dst_32f.height, dst_32f.width, cv.CV_8U) cv.Convert(dst_32f, cornerMap) #Convert to make the and cv.And(cornerMap, localMax, cornerMap) #Delete all modified pixels radius = 3 thickness = 2 l = [] for x in range( cornerMap.height ): #Create the list of point take all pixel that are not 0 (so not black) for y in range(cornerMap.width): if cornerMap[x, y]: l.append((y, x)) for center in l: cv.Circle(im, center, radius, (255, 255, 255), thickness)
if max(ds3, ds4) / min(ds3, ds4) > 1.4: tracking = 0 if ds1 < 10 or ds2 < 10 or ds3 < 10 or ds4 < 10: tracking = 0 if tracking == 0: detected = 0 #detection mode if tracking == 0: detected = 0 cv.Smooth(grey, dst2, cv.CV_GAUSSIAN, 3) cv.Laplace(dst2, d) cv.CmpS(d, 8, d2, cv.CV_CMP_GT) if onlyBlackCubes: #can also detect on black lines for improved robustness cv.CmpS(grey, 100, b, cv.CV_CMP_LT) cv.And(b, d2, d2) #these weights should be adaptive. We should always detect 100 lines if lastdetected > dects: THR = THR + 1 if lastdetected < dects: THR = max(2, THR - 1) li = cv.HoughLines2(d2, cv.CreateMemStorage(), cv.CV_HOUGH_PROBABILISTIC, 1, 3.1415926 / 45, THR, 10, 5) #store angles for later angs = [] for (p1, p2) in li: #cv.Line(sg,p1,p2,(0,255,0)) a = atan2(p2[1] - p1[1], p2[0] - p1[0]) if a < 0: a += pi angs.append(a)
cv.Add( im, im2, res ) #Add every pixels together (black is 0 so low change and white overload anyway) cv.ShowImage("Add", res) cv.AbsDiff(im, im2, res) # Like minus for each pixel im(i) - im2(i) cv.ShowImage("AbsDiff", res) cv.Mul(im, im2, res) #Multiplie each pixels (almost white) cv.ShowImage("Mult", res) cv.Div(im, im2, res) #Values will be low so the image will likely to be almost black cv.ShowImage("Div", res) cv.And(im, im2, res) #Bit and for every pixels cv.ShowImage("And", res) cv.Or(im, im2, res) # Bit or for every pixels cv.ShowImage("Or", res) cv.Not(im, res) # Bit not of an image cv.ShowImage("Not", res) cv.Xor(im, im2, res) #Bit Xor cv.ShowImage("Xor", res) cv.Pow(im, res, 2) #Pow the each pixel with the given value cv.ShowImage("Pow", res) cv.Max(im, im2, res) #Maximum between two pixels
param1=5, param2=0, param3=0, param4=0) cv.SaveImage("Grey image.png", GrayImg) cv.SaveImage("GraySmooth image.png", SmoothGrayImg) #edge detection EdgeDetection_Img = cv.CreateImage(cv.GetSize(SmoothGrayImg), cv.IPL_DEPTH_16S, cv.CV_BLUR_NO_SCALE) cv.Laplace(SmoothGrayImg, EdgeDetection_Img) cv.SaveImage(" EdgeDetection image.png", EdgeDetection_Img) # set threshold Thresholding = cv.CreateImage(cv.GetSize(EdgeDetection_Img), cv.IPL_DEPTH_16S, cv.CV_BLUR_NO_SCALE) cv.Threshold(EdgeDetection_Img, Thresholding, 20, 400, cv.CV_THRESH_BINARY_INV) cv.SaveImage("Thresholding.png", Thresholding) #Output from bilateral filter im = cv.LoadImageM("cat.jpg") BilateralImg = cv.CreateImage(cv.GetSize(im), cv.IPL_DEPTH_8U, 3) cv.CvtColor(im, BilateralImg, cv.CV_RGB2Lab) cv.Smooth(im, BilateralImg, cv.CV_BILATERAL, 100, 100, 100, 100) cv.SaveImage("bilateral.png", BilateralImg) finalImg = cv.CreateImage(cv.GetSize(GrayImg), cv.IPL_DEPTH_8U, 3) Sketch = cv.LoadImageM("Thresholding.png") Paint = cv.LoadImageM("bilateral.png") cv.And(Sketch, Paint, finalImg) cv.SaveImage("final.png", finalImg)
return get_data(x, y) # main loop Target = cv.CloneImage(Img) while True: # image processing if Dilate: cv.Dilate(Target, Target, iterations=Dilate) Dilate = 0 if Erode: cv.Erode(Target, Target, iterations=Erode) Erode = 0 if Threshold: cv.Threshold(Img, Target, Threshold_Min, 0xff, cv.CV_THRESH_BINARY) cv.And(Target, Mask, Target) show_image() # keystroke processing k = cv.WaitKey(0) print k if k > 66000: continue if k < 256: k = chr(k) else: if k > 65506 and k != 65535: k -= 65506 k = chr(k - 30) if k == 65288 and Edit_x >= 0: # BS
cv.NamedWindow('left') cv.NamedWindow('right') cv.SetMouseCallback("cam", on_mouse) on_mouse(cv.CV_EVENT_LBUTTONDOWN, centerX, centerY, None, None) font = cv.InitFont(cv.CV_FONT_HERSHEY_PLAIN, 1.0, 1.0) M = 60 while True: img = cv.QueryFrame(capture) cv.CvtColor(img, im, cv.CV_RGB2GRAY) #cv.LogPolar(img, polar, (centerX, centerY), M+1, cv.CV_INTER_NN + cv.CV_WARP_FILL_OUTLIERS ) cv.And(im, lm, lt) leftBrightness = cv.Avg(im, mask=lm) cv.Rectangle(lt, (0, 0), (32, 32), leftBrightness, thickness=-1) cv.PutText(lt, "%3.0f" % leftBrightness[0], (3,20), font, cv.RealScalar(0)) cv.And(im, rm, rt) rightBrightness = cv.Avg(im, mask=rm) cv.Rectangle(rt, (0, 0), (32, 32), rightBrightness, thickness=-1) cv.PutText(rt, "%3.0f" % rightBrightness[0], (3,20), font, cv.RealScalar(0)) cv.ShowImage('cam', im) cv.ShowImage('left', lt) cv.ShowImage('right', rt) key = cv.WaitKey(10) #print "key ",key
dilated = cv.CloneImage(dst_32f) cv.Dilate( dst_32f, dilated ) #nos aseguramos que el pixel local su maximo valor no cambia y todos los otros pixeles si localMax = cv.CreateMat(dst_32f.height, dst_32f.width, cv.CV_8U) cv.Cmp( dst_32f, dilated, localMax, cv.CV_CMP_EQ ) #comparamos y guardamos solo los pixeles modificados los que son los maximos valores locales que son esquinas threshold = 0.01 * maxv cv.Threshold(dst_32f, dst_32f, threshold, 255, cv.CV_THRESH_BINARY) cornerMap = cv.CreateMat(dst_32f.height, dst_32f.width, cv.CV_8U) cv.Convert(dst_32f, cornerMap) #convertir y utilizar la operacion logica AND cv.And(cornerMap, localMax, cornerMap) #Borrar todos los pixeles modificados radius = 3 thickness = 2 l = [] for x in range(cornerMap.height ): # crea la lista de todos los pixeles que no son 0 (no negro) for y in range(cornerMap.width): if cornerMap[x, y]: l.append((y, x)) for center in l: cv.Circle(img, center, radius, (255, 255, 255), thickness)