gpt4 book ai didi

c++ - 光学字符识别 : Difference between two frames

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:10:08 24 4
gpt4 key购买 nike

我正在尝试找到一个简单的解决方案来实现来自 OPenCV 的OCR 算法。我对图像处理很陌生!我正在播放一个使用 RLE 算法 的特定编解码器解码的视频。

我想做的是,对于每个解码帧,我想将它与前一帧进行比较,并存储两帧之间发生变化的像素。

大多数现有解决方案都给出了两个帧之间的差异,但我只想保留已更改的新像素并将其存储在表格中,然后能够分析每组已更改的像素而不是分析每次都是整个图像。

我计划使用“blobs detection”算法,但在实现之前我遇到了困难。

今天,我正在尝试这个:

char *prevFrame;
char *curFrame;
QVector DiffPixel<LONG>;

//for each frame
DiffPixel.push_back(curFrame-prevFrame);

enter image description here

我真的很想拥有“仅更改像素结果”解决方案。如果我走错了路,谁能给我一些提示或纠正我?

编辑:

新问题,如果有多个像素变化区域怎么办?是否可以为每个更改像素 block 创建一个表,还是只有一个唯一表?举个例子:

Multiple Areas Pixels

最好的结果是有 2 个 mat 矩阵。第一个矩阵带有第一个橙色方 block ,第二个矩阵带有第二个橙色方 block 。这样一来,如果我们仅将结果存储在一个分辨率几乎与全帧相同的矩阵中,就可以避免“扫描”几乎整个帧。

此处的主要目标是最小化要分析以查找文本的区域(也称为分辨率)。

最佳答案

加载图片后:

img1

enter image description here

img2

enter image description here

您可以应用异或运算来获取差异。结果与输入图像的 channel 数相同:

异或

enter image description here

然后您可以创建一个二进制掩码 OR-ing 所有 channel :

面具

enter image description here

您可以将对应于掩码中非零元素的 img2 的值复制到白色图像:

差异

enter image description here


更新

如果您有多个像素发生变化的区域,如下所示:

enter image description here

你会发现一个差异掩码(二值化后所有非零像素都设置为 255)如下:

enter image description here

然后您可以提取连接的组件并在新的黑色初始化掩码上绘制每个连接的组件:

enter image description here

然后,和以前一样,您可以将每个掩码中对应于非零元素的 img2 的值复制到白色图像。

enter image description here

完整代码供引用。请注意,这是答案的更新 版本的代码。您可以在修订历史中找到原始代码。

#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;
}

关于c++ - 光学字符识别 : Difference between two frames,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34024865/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com