def redraw_monocular(self, drawable): width, height = cv.GetSize(drawable.scrib) display = cv.CreateMat(max(480, height), width + 100, cv.CV_8UC3) cv.Zero(display) cv.Copy(drawable.scrib, cv.GetSubRect(display, (0, 0, width, height))) cv.Set(cv.GetSubRect(display, (width, 0, 100, height)), (255, 255, 255)) self.buttons(display) if not self.c.calibrated: if drawable.params: for i, (label, lo, hi, progress) in enumerate(drawable.params): (w, _), _ = cv.GetTextSize(label, self.font) cv.PutText(display, label, (width + (100 - w) / 2, self.y(i)), self.font, (0, 0, 0)) color = (0, 255, 0) if progress < 1.0: color = (0, int(progress * 255.), 255) cv.Line(display, (int(width + lo * 100), self.y(i) + 20), (int(width + hi * 100), self.y(i) + 20), color, 4) else: cv.PutText(display, "lin.", (width, self.y(0)), self.font, (0, 0, 0)) linerror = drawable.linear_error if linerror < 0: msg = "?" else: msg = "%.2f" % linerror #print "linear", linerror cv.PutText(display, msg, (width, self.y(1)), self.font, (0, 0, 0)) self.show(display)
def onNewFrame(frame):#use this to modify the image before printing frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR) msg = "processed frame" font = cv.InitFont(cv.CV_FONT_HERSHEY_DUPLEX, 1.0, 1.0) tsize, baseline = cv.GetTextSize(msg, font) h,w = frame.shape[:2] tpt = (w - tsize[0]) / 2, (h - tsize[1]) / 2
def getShape(self): ((buttonWidth,buttonHeight),baseline) = cv.GetTextSize(self.getText(),self.getFont()) origin = self.getBottomLeft() xMin = origin.x()-4 xMax = origin.x() + buttonWidth yMin = origin.y() - buttonHeight yMax = origin.y()+4 return Geometry2D.Polygon(Geometry2D.Point(xMin,yMin),Geometry2D.Point(xMin,yMax),Geometry2D.Point(xMax,yMax),Geometry2D.Point(xMax,yMin))
def button(self, dst, label, enable): cv.Set(dst, (255, 255, 255)) size = cv.GetSize(dst) if enable: color = cv.RGB(155, 155, 80) else: color = cv.RGB(224, 224, 224) cv.Circle(dst, (size[0] / 2, size[1] / 2), min(size) / 2, color, -1) ((w, h), _) = cv.GetTextSize(label, self.font) cv.PutText(dst, label, ((size[0] - w) / 2, (size[1] + h) / 2), self.font, (255, 255, 255))
def add_text(image, text, good = True): if good: color = (0, 255, 0) else: color = (0, 0, 255) w = image.cols h = image.rows for i in range(len(text)): font = cv.InitFont(cv.CV_FONT_HERSHEY_SIMPLEX, 0.30, float(w)/350, thickness = 1) ((text_w, text_h), _) = cv.GetTextSize(text[i], font) cv.PutText(image, text[i], (w/2-text_w/2, h/2-text_h/2 + i*text_h*2), font, color) return image
def splash_screen(self, s1, s2): splash_array = numpy.zeros((self.height, self.width, 3), numpy.uint8) font = cv.InitFont(cv.CV_FONT_HERSHEY_SIMPLEX, 3.0, 3.0, 9) ((text_x, text_y), baseline) = cv.GetTextSize(s1, font) org = ((self.width - text_x) / 2, (self.height / 3) + (text_y / 2)) cv2.putText(splash_array, s1, org, cv.CV_FONT_HERSHEY_SIMPLEX, 3.0, \ self.white, thickness = 7) ((text_x, text_y), baseline) = cv.GetTextSize(s2, font) org = ((self.width - text_x) / 2, ((2 * self.height) / 3) + (text_y / 2)) cv2.putText(splash_array, s2, org, cv.CV_FONT_HERSHEY_SIMPLEX, 3.0, \ self.white, thickness = 7) splash_image = cv.fromarray(splash_array) # 3ms wait cv2.waitKey(3) msg = cv_bridge.CvBridge().cv_to_imgmsg(splash_image, encoding="bgr8") self.pub.publish(msg)
def redraw_stereo(self, drawable): width, height = cv.GetSize(drawable.lscrib) display = cv.CreateMat(max(480, height), 2 * width + 100, cv.CV_8UC3) cv.Zero(display) cv.Copy(drawable.lscrib, cv.GetSubRect(display, (0, 0, width, height))) cv.Copy(drawable.rscrib, cv.GetSubRect(display, (width, 0, width, height))) cv.Set(cv.GetSubRect(display, (2 * width, 0, 100, height)), (255, 255, 255)) self.buttons(display) if not self.c.calibrated: if drawable.params: for i, (label, lo, hi, progress) in enumerate(drawable.params): (w, _), _ = cv.GetTextSize(label, self.font) cv.PutText(display, label, (2 * width + (100 - w) / 2, self.y(i)), self.font, (0, 0, 0)) color = (0, 255, 0) if progress < 1.0: color = (0, int(progress * 255.), 255) cv.Line(display, (int(2 * width + lo * 100), self.y(i) + 20), (int(2 * width + hi * 100), self.y(i) + 20), color, 4) else: cv.PutText(display, "epi.", (2 * width, self.y(0)), self.font, (0, 0, 0)) if drawable.epierror == -1: msg = "?" else: msg = "%.2f" % drawable.epierror cv.PutText(display, msg, (2 * width, self.y(1)), self.font, (0, 0, 0)) # TODO dim is never set anywhere. Supposed to be observed chessboard size? if drawable.dim != -1: cv.PutText(display, "dim", (2 * width, self.y(2)), self.font, (0, 0, 0)) cv.PutText(display, "%.3f" % drawable.dim, (2 * width, self.y(3)), self.font, (0, 0, 0)) self.show(display)
def writetext(img, textupper=None, textbottom=None): padding = 2 cvfont = cv.InitFont(cv.CV_FONT_HERSHEY_SIMPLEX, 1, 1) textsize, baseline = cv.GetTextSize(textupper, cvfont) fontheight = textsize[1] fontheight = fontheight + (padding * 2) if textupper: cv.Rectangle(img, (0, 0), (img.width, fontheight), cv.RGB(30, 30, 50), cv.CV_FILLED) cv.PutText(img, textupper, (0, textsize[1] + padding), cvfont, cv.RGB(255, 255, 255)) if textbottom: cv.Rectangle(img, (0, img.height), (img.width, img.height - fontheight), cv.RGB(30, 30, 50), cv.CV_FILLED) cv.PutText(img, textbottom, (0, img.height - padding), cvfont, cv.RGB(255, 255, 255))
def _composite(self, img, pos, imgNum): ''' Internal method to composite the thumbnail of a given image into the correct position, given by (row,col). @param img: The image from which a thumbnail will be composited onto the montage @param pos: A tuple (row,col) for the position in the montage layout @param imgNum: The image number used to draw a text label in the lower left corner of each thumbnail. ''' (row,col) = pos tile = img.resize(self._tileSize) pos_x = col*(self._tileSize[0] + self._gutter) + self._gutter + self._xpad pos_y = row*(self._tileSize[1] + self._gutter) + self._gutter + self._ypad cvImg = self._cvMontageImage cvTile = tile.asOpenCV() cv.SetImageROI(cvImg, (pos_x,pos_y,self._tileSize[0],self._tileSize[1])) depth = cvTile.nChannels if depth==1: cvTileBGR = cv.CreateImage(self._tileSize, cv.IPL_DEPTH_8U, 3) cv.CvtColor(cvTile, cvTileBGR, cv.CV_GRAY2BGR) cv.Copy(cvTileBGR,cvImg) #should respect the ROI else: cv.Copy(cvTile,cvImg) #should respect the ROI if not self._nolabels: #draw image number in lower left corner, respective to ROI ((tw,th),_) = cv.GetTextSize("%d"%imgNum, self._txtfont) #print "DEBUG: tw, th = %d,%d"%(tw,th) if tw>0 and th>0: cv.Rectangle(cvImg, (0,self._tileSize[1]-1),(tw+1,self._tileSize[1]-(th+1)-self._gutter), (0,0,0), thickness=cv.CV_FILLED ) font = self._txtfont color = self._txtcolor cv.PutText(cvImg, "%d"%imgNum, (1,self._tileSize[1]-self._gutter-2), font, color) #reset ROI cv.SetImageROI(cvImg, (0,0,self._size[0],self._size[1]))
def getSize(self): ((width,height),baseline) = cv.GetTextSize(self.getText(),self.getFont()) return (width+8,height + baseline+8)
import time def playlist(args): for f in args: r = CVreader(f) for d in r: yield d + (f,) framecounter = 0 font = cv.InitFont(cv.CV_FONT_HERSHEY_SIMPLEX, 1, 1) cv.NamedWindow("tv", 1) for cam,l_image,r_image,label in playlist(sys.argv[1:]): ipl = l_image msg = "%4d" % framecounter (w,h),baseline = cv.GetTextSize(msg, font) (_, imageh) = cv.GetSize(l_image) if 0: # Gray out the top-left rectangle cv.SetImageROI(ipl, (0, imageh+baseline, w + 1, imageh)) cv.ConvertScale(ipl, ipl, 0.75) cv.ResetImageROI(ipl) # Draw the time cv.PutText(ipl, msg, (0,imageh+baseline), font, (255,255,255)) cv.ShowImage("tv", ipl) cmd = cv.WaitKey(10) # Space is pause
random.randrange(0, 100) * 0.05 + 0.01, random.randrange(0, 5) * 0.1, random.randrange(0, 10), line_type) cv.PutText(image, "Testing text rendering!", pt1, font, random_color(random)) cv.ShowImage(window_name, image) cv.WaitKey(delay) # prepare a text, and get it's properties font = cv.InitFont(cv.CV_FONT_HERSHEY_COMPLEX, 3, 3, 0.0, 5, line_type) text_size, ymin = cv.GetTextSize("OpenCV forever!", font) pt1 = ((width - text_size[0]) / 2, (height + text_size[1]) / 2) image2 = cv.CloneImage(image) # now, draw some OpenCV pub ;-) for i in range(0, 512, 2): cv.SubS(image2, cv.ScalarAll(i), image) (r, g, b) = colorsys.hsv_to_rgb((i % 100) / 100., 1, 1) cv.PutText(image, "OpenCV forever!", pt1, font, cv.RGB(255 * r, 255 * g, 255 * b)) cv.ShowImage(window_name, image) cv.WaitKey(delay) # wait some key to end cv.WaitKey(0)
def _composite(self, img, pos, imgNum): """ Internal method to composite the thumbnail of a given image into the correct position, given by (row,col). @param img: The image from which a thumbnail will be composited onto the montage @param pos: A tuple (row,col) for the position in the montage layout @param imgNum: The image index of the tile being drawn, this helps us display the appropriate label in the lower left corner if self._labels is not None. """ (row, col) = pos if self._keep_aspect: # Get the current size w, h = img.size # Find the scale scale = min(1.0 * self._tileSize[0] / w, 1.0 * self._tileSize[1] / h) w = int(scale * w) h = int(scale * h) # Resize preserving aspect img2 = img.resize((w, h)).asPIL() # Create a new image with the old image centered x = (self._tileSize[0] - w) / 2 y = (self._tileSize[1] - h) / 2 pil = PIL.Image.new('RGB', self._tileSize, "#000000") pil.paste(img2, (x, y, x + w, y + h)) # Generate the tile tile = pv.Image(pil) else: tile = img.resize(self._tileSize) pos_x = col * (self._tileSize[0] + self._gutter) + self._gutter + self._xpad pos_y = row * (self._tileSize[1] + self._gutter) + self._gutter + self._ypad cvImg = self._cvMontageImage cvTile = tile.asOpenCV() cv.SetImageROI(cvImg, (pos_x, pos_y, self._tileSize[0], self._tileSize[1])) # Save the position of this image self._image_positions.append( [self._images[imgNum], imgNum, pv.Rect(pos_x, pos_y, self._tileSize[0], self._tileSize[1])]) depth = cvTile.nChannels if depth == 1: cvTileBGR = cv.CreateImage(self._tileSize, cv.IPL_DEPTH_8U, 3) cv.CvtColor(cvTile, cvTileBGR, cv.CV_GRAY2BGR) cv.Copy(cvTileBGR, cvImg) #should respect the ROI else: cv.Copy(cvTile, cvImg) #should respect the ROI if self._labels == 'index': #draw image number in lower left corner, respective to ROI lbltext = "%d" % imgNum elif type(self._labels) == list: lbltext = str(self._labels[imgNum]) else: lbltext = None if not lbltext is None: ((tw, th), _) = cv.GetTextSize(lbltext, self._txtfont) #print "DEBUG: tw, th = %d,%d"%(tw,th) if tw > 0 and th > 0: cv.Rectangle(cvImg, (0, self._tileSize[1] - 1), (tw + 1, self._tileSize[1] - (th + 1) - self._gutter), (0, 0, 0), thickness=cv.CV_FILLED) font = self._txtfont color = self._txtcolor cv.PutText(cvImg, lbltext, (1, self._tileSize[1] - self._gutter - 2), font, color) if self._highlighted and (imgNum in self._selected_tiles): #draw a highlight around this image cv.Rectangle(cvImg, (0, 0), (self._tileSize[0], self._tileSize[1]), (0, 255, 255), thickness=4) #reset ROI cv.SetImageROI(cvImg, (0, 0, self._size[0], self._size[1]))