【问题标题】:OpenCV - is there anything like delete text?OpenCV - 有没有像删除文本这样的东西?
【发布时间】:2011-05-19 07:31:44
【问题描述】:

我需要这样的东西,因为在我看来,当我这样做时

cvRectangle( CVframe, UL, LR, CV_RGB(0,256,53), CV_FILLED);
    string cvtext;
    cvtext += timeStr;
    cvPutText(CVframe, cvtext.c_str(), cvPoint(0,(h/2+10)), &font , CV_RGB(0,0,0));

每次每秒 24 次 cvRectangle 不会覆盖旧文本...

【问题讨论】:

    标签: c++ c opencv


    【解决方案1】:

    我不知道,但也许这会有所帮助。 制作两个鼠标单击事件,LeftClick 和 Right Click。 在我的示例中,当我左键单击鼠标时,它会给出图像的像素坐标,当我右键单击鼠标时,它会从新加载图像。这有点像删除放置在那里的 cv2.putText 函数。

    只需导入任何图像,我以 'lena_color.tiff' 为例。

    import cv2
    def click_event(event, x, y, flags, params):
        global img
        if event == cv2.EVENT_LBUTTONDOWN:
            print(x, ' ', y)
            font = cv2.FONT_HERSHEY_SIMPLEX; fontSize = 2
            point = '.'; text = '  (' + str(x) + ', ' + str(y) + ')'
            color = (255, 0, 0); thickness=6
            cv2.putText(img, point, (x,y), font, fontSize, color, thickness)
            cv2.putText(img, text, (x,y), font, 0.5, color, 1)
            cv2.imshow('image', img)
    
        if event==cv2.EVENT_RBUTTONDOWN:
            img = cv2.imread('lena_color.tiff', 1)
            cv2.imshow('image', img)
            cv2.setMouseCallback('image', click_event)
    
    
    img = cv2.imread('lena_color.tiff', 1)
    cv2.imshow('image', img)
    cv2.setMouseCallback('image', click_event)
    cv2.waitKey(0)
    
    cv2.destroyAllWindows()
    

    【讨论】:

      【解决方案2】:

      没有内置的cvDeleteText 或类似的东西,这可能是有充分理由的。每当您在图像上放置文本时,它都会覆盖该图像中的像素,就像您将它们的值单独设置为 CV_RGB(0,0,0) 一样。如果您想撤消该操作,则需要存储事先存在的所有内容。由于不是每个人都想这样做,如果cvPutText 自动跟踪它写入的像素,那将是浪费空间和时间。

      最好的方法可能是有两个框架,其中一个永远不会被文本触及。代码看起来像这样。

      //Initializing, before your loop that executes 24 times per second:
      CvArr *CVframe, *CVframeWithText; // make sure they're the same size and format
      
      while (looping) {
          cvRectangle( CVframe, UL, LR, CV_RGB(0,256,53), CV_FILLED);
          // And anything else non-text-related, do it to CVframe.
      
          // Now we want to copy the frame without text.
          cvCopy(CVframe, CVframeWithText);
      
          string cvtext;
          cvtext += timeStr;
          // And now, notice in the following line that 
          // we're not overwriting any pixels in CVframe
          cvPutText(CVframeWithText, cvtext.c_str(), cvPoint(0,(h/2+10)), &font , CV_RGB(0,0,0));
          // And then display CVframeWithText.
      
          // Now, the contents of CVframe are the same as if we'd "deleted" the text;
          // in fact, we never wrote text to CVframe in the first place.
      

      希望这会有所帮助!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-07-30
        • 2014-09-29
        • 1970-01-01
        • 1970-01-01
        • 2023-03-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多