概述
1. 前言
本文主要是使用opencv库实现从鼠标输入框选ROI区域,框选后截图并将框选出的区域的像素值保存在txt文本中,同一张图片可以重复框选区域并保存信息,当前图片信息框选完成后,按空格键可进入到下一张图片进行操作。
2. 实践
// label.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <opencv.hpp>
#include <string>
using namespace cv;
using namespace std;
cv::Mat org,dst,img,tmp;
Point prevPt = Point(-1, -1);
string lpath = "..//..//";
string rpath = "..//..//";
int laneCount = 1;
int roadCount = 1;
bool flag = 1;
int GetImageValue(Mat& img, int flag)
{
if (flag == 1)
{
char buffer[1];
itoa(laneCount, buffer, 10);
string str = buffer;
string path = "";
path = lpath + str + "-lane.txt";
ofstream fp(path);
if (!fp)
{
printf("file open failed!");
return -1;
}
for (int i = 0; i < img.rows; i++)
{
uchar* data = img.ptr<uchar>(i);
for (int j = 0; j < img.cols * 3; j = j + 3)
{
fp << (int)data[j] << "," << (int)data[j + 1] << "," << (int)data[j + 2] << "," << flag << endl;;
}
}
laneCount++;
}
else
{
char buffer[1];
itoa(roadCount, buffer, 10);
string str = buffer;
string path = "";
path = rpath + str + "-road.txt";
ofstream fp(path);
if (!fp)
{
printf("file open failed!");
return -1;
}
for (int i = 0; i < img.rows; i++)
{
uchar* data = img.ptr<uchar>(i);
for (int j = 0; j < img.cols * 3; j = j + 3)
{
fp << (int)data[j] << "," << (int)data[j + 1] << "," << (int)data[j + 2] << "," << flag << endl;
}
}
roadCount++;
}
return 1;
}
void OnMouse(int event, int x, int y, int flags, void* ustc)
{
static Point pre_pt = (-1, -1);
static Point cur_pt = (-1, -1);
if (event == CV_EVENT_LBUTTONDOWN)
{
org.copyTo(img);
pre_pt = Point(x, y);
}
else if (event == CV_EVENT_MOUSEMOVE && (flags & CV_EVENT_FLAG_LBUTTON))//摁下左键,flags为1
{
img.copyTo(tmp);
cur_pt = Point(x, y);
rectangle(tmp, pre_pt, cur_pt, Scalar(0, 255, 0, 0), 1, 8, 0);
imshow("img", tmp);
}
else if (event == CV_EVENT_LBUTTONUP)
{
org.copyTo(img);
cur_pt = Point(x, y);
rectangle(img, pre_pt, cur_pt, Scalar(0, 255, 0, 0), 1, 8, 0);
imshow("img", img);
img.copyTo(tmp);
int width = abs(pre_pt.x - cur_pt.x);
int height = abs(pre_pt.y - cur_pt.y);
if (width == 0 || height == 0)
{
return;
}
dst = org(Rect(min(cur_pt.x, pre_pt.x), min(cur_pt.y, pre_pt.y), width, height));
Mat temp = dst.clone();
GetImageValue(temp, flag);
namedWindow("dst", 1);
imshow("dst", dst);
}
}
int main()
{
string imgPath = "";
vector<String> vecFilename;
glob(imgPath, vecFilename);
for (int i = 0; i < vecFilename.size(); i++)
{
Mat tempImg = imread(vecFilename[i], 1);
tempImg.copyTo(org);
org.copyTo(img);
namedWindow("img");
setMouseCallback("img", OnMouse, 0);
imshow("img", img);
waitKey(0);
}
return 0;
}
// ReductionDimension.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <opencv.hpp>
using namespace cv;
using namespace std;
int GetLineNumber(string path)
{
ifstream fp(path);
string line;
int lineNum = 0;
while (getline(fp, line))
{
lineNum++;
}
return lineNum;
}
void GetMatAndLabel(string path, Mat& dataMat, vector<int>& label)
{
ifstream fp(path);
int rsize = GetLineNumber(path);
Mat temp = Mat::zeros(Size(3, rsize), CV_8UC1);
temp.copyTo(dataMat);
if (!fp)
{
printf("open failed");
}
string line;
vector<int> value;
while (getline(fp, line))
{
int pos = 0;
vector<int> index;
//index.push_back(0);
while ((pos = line.find(",", pos)) != string::npos)
{
index.push_back(pos);
pos++;
}
index.push_back(line.size());
string subStr;
for (int i = 0; i < index.size(); i++)
{
if (i == 0)
{
subStr = line.substr(0, index[i]);
int val = stoi(subStr);
value.push_back(val);
}
else if (i < 3 && i != 0)
{
subStr = line.substr(index[i - 1] + 1, index[i] - index[i - 1] - 1);
int val = stoi(subStr);
value.push_back(val);
}
else
{
subStr = line.substr(index[i - 1] + 1, index[i] - index[i - 1] - 1);
int val = stoi(subStr);
label.push_back(val);
}
}
}
int count = 0;
for (int i = 0; i < dataMat.rows; i++)
{
uchar* data = dataMat.ptr<uchar>(i);
for (int j = 0; j < dataMat.cols; j++)
{
data[j] = value[count];
count++;
}
}
}
int main()
{
char* lanepath = "D:\temp\label\lane.txt";
char* roadpath = "D:\temp\label\road.txt";
Mat laneMat, roadMat, dataMat;
vector<int> laneLabel, roadLabel, label;
GetMatAndLabel(roadpath, roadMat, roadLabel);
GetMatAndLabel(lanepath, laneMat, laneLabel);
vconcat(roadMat, laneMat, dataMat);
label.insert(label.end(), roadLabel.begin(), roadLabel.end());
label.insert(label.end(), laneLabel.begin(), laneLabel.end());
LDA lda = LDA(dataMat, label, 1);
Mat eigvector = lda.eigenvectors().clone();
return 0;
}
最后
以上就是无心蛋挞为你收集整理的opencv 实现鼠标输入框选ROI区域的全部内容,希望文章能够帮你解决opencv 实现鼠标输入框选ROI区域所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复