How to replace specific area of image with zeros in video loop

조회 수: 3 (최근 30일)
Ted
Ted 2022년 12월 5일
댓글: Ted 2022년 12월 8일
So I have created a video that tracks 2 different coloured tags, 1 yellow 1 red, tracking the border/distance/centroids.
I am reporting the centroid coordinates in real time in the bottom left corner of the video. However, in my current method the figures are repeating over each other while I need them to replace.
Here you can see my output image where you can see my issue with the text being blurred due to repeated numbers being posted on top of each other:
My thought process to fix this issue is to create a zero matrix that also creates itself everytime the figures updates, so each iteration of figures being displayed will have a black background behind it, so you can see the figures live.
So I am just wondering what the code would be to create a matrix of zeros in the bottom left of the corner so you can see the figures as they update.
Below is my current code for the output seen above, RGB is the snapshot image, redYellowOverlay is the border code, ri/rj/yi/yj mean are centroid coordinates, dim/str/annnotation code is the code for the textbox.
Again, my goal is to also put a matrix of zeros in the loop so it repeats everytime the figure repeats. Any help would be greatly appreciated!
imshow(RGB);
imshow(redYellowOverlay);
hold on;
plot(rjMean,riMean, 'g-x', 'LineWidth',5)
plot(yjMean,yiMean, 'g-x', 'LineWidth',5)
plot([rjMean,yjMean],[riMean,yiMean],'b','LineWidth',2);
dim = [0.15 0.3 0.3 0];
str = {['Red Tag Centroid i coordinates = ',num2str(riMean)],['Red Tag Centroid j coordinates = ',num2str(rjMean)],['Yellow Tag Centroid i coordinates = ',num2str(yiMean)],['Yellow Tag Centroid j coordinates = ',num2str(yjMean)],['City Block Distance = ',num2str(dCityBlock)],['Chessboard Distance = ',num2str(dChessboard)],['Euclidean Distance = ',num2str(dEuclidean)]};
annotation('textbox',dim,'String',str,'FitBoxToText','on','EdgeColor','green','Color','green');

채택된 답변

cr
cr 2022년 12월 5일
편집: cr 2022년 12월 5일
Instead of recreating the annotation in the loop, create just one annotation before the loop, assign a handle to it and inside the loop change the text of the annotation. This will also avoid the problem of overpopulating the figure with objects that are no longer needed. With too many objects the graphics get slow and may crash the session.
E.g.
annohandle = annotation(....);
while ...
::::
str = blah blah..
annohandle.String = str;
end
  댓글 수: 3
cr
cr 2022년 12월 6일
It means that the annotation you created had been deleted by the time you tried updating its string on line97. I guess you are calling imshow inside the loop before line97. Either this must be replaced with update to CData field of image handle, or you should do what you were doing earlier (calling annotation() inside the loop) but delete the previous annotation -you might find this easier to implement but it consumes more time and frames might appear slow.
Method 1 (similar to your first implementation)
annohandle = annotation(...)
while ...
imshow(newsnapshot)
delete(annohandle)
annohandle = annotation(...); %this is your new annotation with new str
end
Method 2 (recommended implementation)
img = imshow(...);
annohandle = annotation(...)
while ...
img.CData = newsnapshot; % newsnapsot is the matrix that was input to imshow() in earlier method
delete(annohandle)
annohandle = annotation(...); %this is your new annotation with new str
end
Ted
Ted 2022년 12월 8일
Yeah this worked perfectly.
Tried messing around with the first way I was doing but was much easier in your recommended implementation.
Thanks for this!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

제품


릴리스

R2022b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by