- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在尝试找到一个简单的解决方案来实现来自 OPenCV 的OCR 算法。我对图像处理很陌生!我正在播放一个使用 RLE 算法 的特定编解码器解码的视频。
我想做的是,对于每个解码帧,我想将它与前一帧进行比较,并存储两帧之间发生变化的像素。
大多数现有解决方案都给出了两个帧之间的差异,但我只想保留已更改的新像素并将其存储在表格中,然后能够分析每组已更改的像素而不是分析每次都是整个图像。
我计划使用“blobs detection”算法,但在实现之前我遇到了困难。
今天,我正在尝试这个:
char *prevFrame;
char *curFrame;
QVector DiffPixel<LONG>;
//for each frame
DiffPixel.push_back(curFrame-prevFrame);
我真的很想拥有“仅更改像素结果”解决方案。如果我走错了路,谁能给我一些提示或纠正我?
编辑:
新问题,如果有多个像素变化区域怎么办?是否可以为每个更改像素 block 创建一个表,还是只有一个唯一表?举个例子:
最好的结果是有 2 个 mat 矩阵。第一个矩阵带有第一个橙色方 block ,第二个矩阵带有第二个橙色方 block 。这样一来,如果我们仅将结果存储在一个分辨率几乎与全帧相同的矩阵中,就可以避免“扫描”几乎整个帧。
此处的主要目标是最小化要分析以查找文本的区域(也称为分辨率)。
最佳答案
加载图片后:
img1
img2
您可以应用异或运算来获取差异。结果与输入图像的 channel 数相同:
异或
然后您可以创建一个二进制掩码 OR-ing 所有 channel :
面具
您可以将对应于掩码中非零元素的 img2
的值复制到白色图像:
差异
更新
如果您有多个像素发生变化的区域,如下所示:
你会发现一个差异掩码(二值化后所有非零像素都设置为 255)如下:
然后您可以提取连接的组件并在新的黑色初始化掩码上绘制每个连接的组件:
然后,和以前一样,您可以将每个掩码中对应于非零元素的 img2
的值复制到白色图像。
完整代码供引用。请注意,这是答案的更新 版本的代码。您可以在修订历史中找到原始代码。
#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/
我是一名优秀的程序员,十分优秀!