I need such thing because it seems to me that when I do
cvRectangle( CVframe, UL, LR, CV_RGB(0,256,53), CV_FILLED);
string cvtext;
开发者_StackOverflow社区 cvtext += timeStr;
cvPutText(CVframe, cvtext.c_str(), cvPoint(0,(h/2+10)), &font , CV_RGB(0,0,0));
each time 24 times per second cvRectangle does not overlay old text...
There's no built-in cvDeleteText
or anything like that, and probably for good reason. Whenever you put text on an image, it overwrites the pixels in that image, just as if you had set their values to CV_RGB(0,0,0)
individually. If you wanted to undo that operation, you'd need to store whatever had been there beforehand. Since not everyone wants to do that, it would be a waste of space and time if cvPutText
automatically kept track of the pixels it wrote over.
Probably the best approach would be to have two frames, one of which is never touched by text. The code would look something like this.
//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.
Hope this helps!
I don't know, but maybe this can help. Make two mouse click events, LeftClick and Right Click. In my example, when I left-click the mouse it gives the pixel coordinate of the image, and when I right-click the mouse, it loads the image from the fresh. It's kindof removing what cv2.putText function placed there.
just import any image, I have used 'lena_color.tiff' as an example.
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()
精彩评论