I am getting several error messages for my insertObjectAnnotation line of code and I was wondering what is wrong with it.
1 次查看(过去 30 天)
显示 更早的评论
I am building a code that takes an image, scans it for words, and highlights the text I am scanning for.
global imaget;
ocrResults = ocr(imaget);
words = {ocrResults.Words};
searchWords = {'Urgent', 'Important', 'Action required', 'Secured Document'};
fprintf(ocrResults.Text);
highlightedImage = insertObjectAnnotation(imaget, 'rectangle', ocrResults.WordBoundingBoxes, words);
imshow(highlightedImage, 'Parent', app.UIAxes);
4 个评论
Rupesh
2024-4-30
Hi Shelby,
I understand that you are working on a project that involves scanning images for certain keywords using OCR technology in MATLAB, and you aim to highlight these words in the image. You encountered an error indicating that labels passed to insertObjectAnnotation must be character vectors. This issue arises because the function requires each label in the annotation to be a character vector, but the OCR results might not directly meet this requirement. To solve this, you'll need to ensure that only the words you're interested in are highlighted and that each of these words is correctly formatted as a character vector before annotation. you can do some below modifications in your code to solve the problem:
% Perform OCR on the image
ocrResults = ocr(imaget);
words = ocrResults.Words;
searchWords = {'Urgent', 'Important', 'Action required', 'Secured Document'};
% Prepare labels for highlighting
labels = {}; % Initialize an empty cell array for labels
for i = 1:length(words)
if any(strcmpi(words{i}, searchWords))
labels{end+1} = char(words{i}); % Convert to character vector and add to labels
end
end
% Filter bounding boxes for the words to be highlighted
highlightBoxes = ocrResults.WordBoundingBoxes(ismember(words, searchWords), :);
% Highlight the words in the image
highlightedImage = insertObjectAnnotation(imaget, 'rectangle', highlightBoxes, labels);
% Display the highlighted image
imshow(highlightedImage, 'Parent', app.UIAxes)
This code snippet ensures that only specified keywords are highlighted in your image. It filters both the words and their bounding boxes based on your criteria, then converts each label into a character vector to meet the requirements of insertObjectAnnotation. You can also refer to below documents to get more infor regarding Objectannotaions operations involved in images.
Hope this helps!
采纳的回答
Aneela
2024-4-26
Hi Shelby,
The error you are encountering is because “ocrResults.Words” is a cell array of strings. However, it should be a character vector.
I have tried the similar code with a sample picture.
img = imread("KLAUS.png");
ocrResults = ocr(img);
words = ocrResults.Words; % Remove the curly braces "{ }".
fprintf(ocrResults.Text);
highlightedImage = insertObjectAnnotation(img, 'rectangle', ocrResults.WordBoundingBoxes, words);
imshow(highlightedImage);
I got the following results:
For more information on “insertObjectAnnotation” please refer to the following MathWorks Documentation: https://www.mathworks.com/help/vision/ref/insertobjectannotation.html
0 个评论
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!