I am getting several error messages for my insertObjectAnnotation line of code and I was wondering what is wrong with it.
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Shelby
el 18 de Abr. de 2024
Comentada: Shelby
el 30 de Abr. de 2024
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 comentarios
Rupesh
el 30 de Abr. de 2024
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!
Respuesta aceptada
Aneela
el 26 de Abr. de 2024
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:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1679656/image.png)
For more information on “insertObjectAnnotation” please refer to the following MathWorks Documentation: https://www.mathworks.com/help/vision/ref/insertobjectannotation.html
0 comentarios
Más respuestas (0)
Ver también
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!