OCR: difference between two frames

I am trying to find a simple solution for implementing the OCR algorithm from OPenCV . I am very new to image processing! I play a video that is decoded by a special codec using the RLE algorithm .

What I would like to do is that for each decoded frame I would like to compare it with the previous one and save the pixels that changed between the two frames.

Most existing solutions give the difference between the two frames, but I would just save the new pixels that were changed, and save them in a table, and then analyze each group of pixels that were changed instead of analyzing the entire image every time.

I planned to use " blobs detection ". algoritm mais I am stuck before implementing it.

Today I try this:

char *prevFrame; char *curFrame; QVector DiffPixel<LONG>; //for each frame DiffPixel.push_back(curFrame-prevFrame); 

enter image description here

I really want to get the solution "Only modified pixel result". Can someone give me some advice or fix me if I make a mistake?

EDIT:

A new question, what if there are several areas with modified pixels ? Will it be possible to have one table for blocks with modified pixels, or will it be only one unique table? Example:

Multiple Pixel Areas

The best result would be to have 2 matte matrices. The first matrix with the first orange square and the second matrix with the second orange square. Thus, he avoids the need to “scan” almost the entire frame if we save the result in one matrix only with a resolution almost equal to the full frame.

The main goal here is to minimize the area (aka resolution) for analysis in order to find the text.

+5
source share
1 answer

After loading the images:

img1

enter image description here

img2

enter image description here

you can apply the XOR operation to get the differences. The result has the same number of input image channels:

Xor

enter image description here

Then you can create the binary mask OR-ing of all channels:

mask

enter image description here

You can copy the img2 values ​​that correspond to non-zero elements in the mask to a white image:

diff

enter image description here


UPDATE

If you have several areas in which the pixel changes, for example:

enter image description here

You will find the difference mask (after binarization, all nonzero pixels are set to 255), for example:

enter image description here

Then you can extract the connected components and draw each connected component on a new black-initialized mask:

enter image description here

Then, as before, you can copy the img2 values ​​that correspond to non-zero elements in each mask to a white image.

enter image description here

Full code for reference. Please note that this is the code for the updated version of the response. The source code can be found in the change history.

 #include <opencv2\opencv.hpp> #include <vector> using namespace cv; using namespace std; int main() { // Load the images Mat img1 = imread("path_to_img1"); Mat img2 = imread("path_to_img2"); imshow("Img1", img1); imshow("Img2", img2); // Apply XOR operation, results in a N = img1.channels() image Mat maskNch = (img1 ^ img2); imshow("XOR", maskNch); // Create a binary mask // Split each channel vector<Mat1b> masks; split(maskNch, masks); // Create a black mask Mat1b mask(maskNch.rows, maskNch.cols, uchar(0)); // OR with each channel of the N channels mask for (int i = 0; i < masks.size(); ++i) { mask |= masks[i]; } // Binarize mask mask = mask > 0; imshow("Mask", mask); // Find connected components vector<vector<Point>> contours; findContours(mask.clone(), contours, RETR_LIST, CHAIN_APPROX_SIMPLE); for (int i = 0; i < contours.size(); ++i) { // Create a black mask Mat1b mask_i(mask.rows, mask.cols, uchar(0)); // Draw the i-th connected component drawContours(mask_i, contours, i, Scalar(255), CV_FILLED); // Create a black image Mat diff_i(img2.rows, img2.cols, img2.type()); diff_i.setTo(255); // Copy into diff only different pixels img2.copyTo(diff_i, mask_i); imshow("Mask " + to_string(i), mask_i); imshow("Diff " + to_string(i), diff_i); } waitKey(); return 0; } 
+9
source

Source: https://habr.com/ru/post/1237176/


All Articles