文章用来记录自己的学习过程,
代码如下(示例):
#pragma once
#include
#include
#include
#include
using namespace cv;
using namespace std;
//实现区域的存储和读取
class RegionFile {
public:
void WriteRegion(Mat region, String filepath);
void ReadRegion(Mat ®ion, String filepath);
};
//定义区域行程编码结构体
typedef struct {
int col; /* line number (row) of run */
int cb; /* column index of beginning of run */
int ce; /* column index of ending of run */
} myregion;
void RegionFile::WriteRegion(Mat region, String filepath) {
vector R;//保存行程中的区域
for (int i = 0; i < region.rows; i++) {
uchar *p = region.ptr(i);
bool beginflag = false;
int begin, end;
for (int j = 0; j < region.cols; j++) {
if (p[j] == 255) {
if (beginflag == false) {
beginflag = true;
begin = j;
}
if (beginflag == true) {
continue;
}
}
if (p[j] == 0) {
if (beginflag == false) {
continue;
}
if (beginflag == true) {
beginflag = false;
end = j - 1;
myregion tempRegion;
tempRegion.col = i;
tempRegion.cb = begin;
tempRegion.ce = end;
R.push_back(tempRegion);
}
}
}
}
//开始存贮
int W = region.cols;
int H = region.rows;
cv::FileStorage fs(filepath, cv::FileStorage::WRITE);
fs << "width" << W;
fs << "height" << H;
fs << "region_info"
<< "[";
for (size_t i = 0; i < R.size(); ++i)
{
fs << "{";
fs << "col" << R[i].col;
fs << "begin" << R[i].cb;
fs << "end" << R[i].ce;
fs << "}"; //
}
fs << "]"; //
fs.release();
}
void RegionFile::ReadRegion(Mat ®ion, String filepath) {
vector R;//保存读取到的区域
//开始读取
cv::FileStorage fs(filepath, cv::FileStorage::READ);
FileNode fn = fs.root();
int W = fn["width"];
int H = fn["height"];
FileNode tps_fn = fn["region_info"];
FileNodeIterator tps_it = tps_fn.begin(), tps_it_end = tps_fn.end();
myregion tempRegion;
for (; tps_it != tps_it_end; ++tps_it)
{
tempRegion.col = (*tps_it)["col"];
tempRegion.cb = (*tps_it)["begin"];
tempRegion.ce = (*tps_it)["end"];
R.push_back(tempRegion);
}
fs.release();
//开始绘制读取到的区域
region = Mat(Size(W, H), CV_8UC1, cv::Scalar::all(0));
myregion tempRegion1;
for (int i = 0; i < R.size(); i++) {
tempRegion1 = R[i];
uchar *p = region.ptr(tempRegion1.col);
for (int j = tempRegion1.cb; j <= tempRegion1.ce; j++) {
p[j] = 255;
}
}
}
代码如下(示例):
#include "stdafx.h"
#include"RunLengthEncoding.h"
#include
//#include
int main()
{
Mat srcRegion = Mat(Size(2000, 1500), CV_8UC1, cv::Scalar::all(0));//创建一个全零的蒙版,在上面绘制区域
//创建区域
Rect r(340, 50, 90, 110);
Rect r1(500, 50, 90, 110);
Mat m = Mat(r.size(), CV_8UC1, cv::Scalar::all(255));//区域使用255表示,全白
Mat m1 = Mat(r1.size(), CV_8UC1, cv::Scalar::all(255));
// 绘制蒙版
cv::circle(srcRegion, cv::Point(srcRegion.cols / 2, srcRegion.rows / 2), 100, cv::Scalar(255), -1);
double angle = 60;
ellipse(srcRegion, Point(700, 800), Size(100, 50), angle, 0, 360, Scalar(255), -1);
rectangle(srcRegion, Point(500, 100), Point(800, 300), Scalar(255), -1);
m.copyTo(srcRegion(r));
m1.copyTo(srcRegion(r1));
//显示创建的区域
namedWindow("src", WINDOW_NORMAL);
imshow("src", srcRegion);
//指定文件路径
String filename = "D:/project/opencv2/RunLengthEncoding/RegionInfo.yaml";
RegionFile mytest;
//写区域时输入的区域必须是二值图蒙版
clock_t start = clock();
mytest.WriteRegion(srcRegion, filename);
clock_t end = clock();
//读取区域时,可输入空的Mat图
Mat dstRegion;
clock_t begin = clock();
mytest.ReadRegion(dstRegion, filename);
clock_t end1 = clock();
//显示读取的区域
namedWindow("dst", WINDOW_NORMAL);
imshow("dst", dstRegion);
//
double programTimes = ((double)end - start) / CLOCKS_PER_SEC;
double programTimes1 = ((double)end1 - begin) / CLOCKS_PER_SEC;
cout << "存储执行时间: " << programTimes<
继续学习