Text recognition from a dot-matrix display

9 次查看(过去 30 天)
I have a video from which I extract image frames at a specified time interval. It is a video of reading from an oxygen online analyzer. I need to recognize the text in each extracted image using MATLAB and append that to a text file, instead of manually looking into each image and noting down the reading in a text file. I have tried to binarize the image and do OCR on the region containing the text alone (by getting the position of the region of interest using getPosition and drawRectangle). I have also used "seven-segment" as language model. Despite these, the recognized text is either empty character array or an array with irrelevant numbers. Can you suggest some techniques like pre-processing for better OCR of such images?
vid = VideoReader('D:\ChemE\MTech\MTP\PurityData\Closed_Pressurization_Vacuum\0.5LPM\30_c1.mp4');
numFrames = vid.NumberOfFrames;
n=numFrames;
fps = 30; %frames per second
fileID = fopen('D:\ChemE\MTech\MTP\PurityData\Closed_Pressurization_Vacuum\0.5LPM\30_c1.txt','w'); %open text file for writing
titl = "Time(s) Purity(%)"; % for the text file which will be appended with the text from OCR
fprintf(fileID,"%s\n",titl);
frames = read (vid,1); %read the first frame of the video to identify ROI
figure;imshow(frames);
rect = drawrectangle; % to select the region of interest (ROI)
roi = rect.Position;
figure(close);
for i = 1:fps:n
frames = read(vid,i); %read the ith frame (at interval of 1 second)
% set a file name for images
fname = ['D:\ChemE\MTech\MTP\PurityData\Closed_Pressurization_Vacuum\0.5LPM\30_c1\30_c1_' int2str(i),'.jpg'];
imwrite(frames,fname); %writes the frame to an image
I = imread(fname); % read the image
bI = imbinarize(rgb2gray(I)); %binarize the image after converting it to a grayscale image
ocrResults = ocr(bI,roi,Language = "seven-segment"); %recognize text
val = ocrResults.Text; %store the recognized text
conf = ocrResults.WordConfidences;
fprintf("The purity observed is ""%s"" with confidence %.4f \n", val, conf);
fprintf(fileID,'%.1f\t %s\n',(i-1)/30,val); %write the text to file
end

采纳的回答

Shubham
Shubham 2024-2-23
Hi Sai,
It seems that you are extracting images from a video and performing OCR by selecting a region of interest to recognize the text. Since you have already tried binarizing the image, try preprocessing the image before performing OCR.
I have saved the image that you have attached in the question and tried to preprocess it. Have a look at the following steps:
1. Import the image and convert it to grayscale:
img = imread('ocr_img.jpeg'); %this is the image present in the ques
grayI = rgb2gray(img); % Convert the image to grayscale
2. Following that you could apply a filter to reduce noise. For example, try a median filter:
filteredI = medfilt2(grayI); % Noise reduction
For more information on filters, you could try referring to the following documentation: https://www.mathworks.com/help/images/noise-removal.html#:~:text=Get-,Kmedian,-%3D%20medfilt2(J)%3B%0Aimshowpair
3. Adjust the contrast and sharpen the image:
contrastAdjustedI = imadjust(filteredI); % Contrast adjustment
sharpenedI = imsharpen(contrastAdjustedI); % Sharpening
For more information about “imadjust” and “imsharpen you can refer to the following documentation links:
4. As a final preprocessing step, binarize and dilate the image to fill gaps. Prior to dilation, complement the image. Have a look at the following code snippet:
bI = imcomplement(sharpenedI);
% Create a structuring element (adjust the size as needed)
se = strel('disk', 6);
% Dilate the image to fill in gaps
dilatedImage = imdilate(bI, se);
filledImage = imcomplement(dilatedImage);
For more details about “imdilate please find the documentation here: https://www.mathworks.com/help/images/ref/imdilate.html
5. For retrieving the text through OCR, you can follow the same process of creating a region of interest as shown in the following code snippet:
rect = drawrectangle; % to select the region of interest (ROI)
roi = rect.Position;
textStruct = ocr(filledImage,roi,Language = "seven-segment");
text = textStruct.Text;
disp(text);
Using the above steps, I was able to retrieve the numerical value present in the image.
I hope this helps!

更多回答(0 个)

Community Treasure Hunt

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

Start Hunting!

Translated by