opencv实现LabelImg

写了个小工具,实现和labelimg一样的效果,输出一个txt文件

参数为:类别代号,包围框中心点x坐标,包围框中心点y坐标,包围框宽度,包围框高度。参数均已归一化

Label_Img.cpp

#include<opencv2/core/core.hpp>
#include<opencv2/imgproc/imgproc.hpp>
#include<opencv2/highgui/highgui.hpp>
#include<opencv2/opencv.hpp>
#include<iostream>
#include<fstream>
#include <iomanip>
using namespace cv;
using namespace std;
Mat org, dst, img, tmp;
string param_str;
void on_mouse(int event, int x, int y, int flags, void *ustc) {

	static Point pre_pt = Point(-1, -1);//初始坐标  
	static Point cur_pt = Point(-1, -1);//实时坐标  
	char temp[16];      //显示的(x,y)字符串
	if (event == CV_EVENT_LBUTTONDOWN) {//左键按下,读取初始坐标,并在图像上该点处划圆  

		org.copyTo(img);
		sprintf(temp, "(%d,%d)", x, y);   //格式化输出
		pre_pt = Point(x, y);
		putText(img, temp, pre_pt, FONT_HERSHEY_SIMPLEX, 0.5, Scalar(0, 0, 0, 255), 1, 8);//在窗口上显示坐标  
		circle(img, pre_pt, 2, Scalar(255, 0, 0, 0), CV_FILLED, CV_AA, 0);//划圆  
		imshow("img", img);
	}
	else if (event == CV_EVENT_MOUSEMOVE && !(flags & CV_EVENT_FLAG_LBUTTON)){//左键没有按下的情况下鼠标移动的处理函数  
		img.copyTo(tmp);
		sprintf(temp, "(%d,%d)", x, y);
		cur_pt = Point(x, y);
		putText(tmp, temp, cur_pt, FONT_HERSHEY_SIMPLEX, 0.5, Scalar(0, 0, 0, 255));//只是实时显示鼠标移动的坐标  
		imshow("img", tmp);
	}
	else if (event == CV_EVENT_MOUSEMOVE && (flags & CV_EVENT_FLAG_LBUTTON)){ //左键按下时,鼠标移动,则在图像上划矩形  
		img.copyTo(tmp);
		sprintf(temp, "(%d,%d)", x, y);
		cur_pt = Point(x, y);
		putText(tmp, temp, cur_pt, FONT_HERSHEY_SIMPLEX, 0.5, Scalar(0, 0, 0, 255));
		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);
		sprintf(temp, "(%d,%d)", x, y);
		cur_pt = Point(x, y);
		putText(img, temp, cur_pt, FONT_HERSHEY_SIMPLEX, 0.5, Scalar(0, 0, 0, 255));
		circle(img, pre_pt, 2, Scalar(255, 0, 0, 0), CV_FILLED, CV_AA, 0);
		rectangle(img, pre_pt, cur_pt, Scalar(0, 255, 0, 0), 1, 8, 0);//根据初始点和结束点,将矩形画到img上  
		imshow("img", img);
		img.copyTo(tmp);
		//截取矩形包围的图像,并保存到dst中  
		int width = abs(pre_pt.x - cur_pt.x);
		int height = abs(pre_pt.y - cur_pt.y);
		if (width == 0 || height == 0) {
			printf("width == 0 || height == 0");
			return;
		}
		dst = org(Rect(min(cur_pt.x, pre_pt.x), min(cur_pt.y, pre_pt.y), width, height));
		namedWindow("dst");          //即新建立一个窗口进行输出结果
		imshow("dst", dst);
		
		float x = (float)(min(cur_pt.x, pre_pt.x) + width) / (2 * img.cols);
		float y = (float)(min(cur_pt.y, pre_pt.y) + height) / (2 * img.rows);
		float w = (float)width / img.cols;
		float h = (float)height / img.rows;

		ofstream out(param_str, ios::app);
		cout << "input your classNum of this ROI: ";
		int c;
		cin >> c;
		out << c << " " << setiosflags(ios::fixed) << setprecision(6) << x << " " << y << " " << w << " " << h << endl;
		cout << c << " " << setiosflags(ios::fixed) << setprecision(6) << x << " " << y << " " << w << " " << h << endl;
		out.close();

		waitKey(0);
	}
}
int main(int argc, char* argv[]) {
	org = imread(argv[1]);
	org.copyTo(img);
	org.copyTo(tmp);
	param_str = argv[2];
	namedWindow("img");//定义一个img窗口  
	setMouseCallback("img", on_mouse, 0);//调用回调函数  
	imshow("img", img);
	waitKey(0);
	return 0;
}

CMakeLists.txt

cmake_minimum_required( VERSION 2.8 )
project( Label_Img )
# 添加c++ 11标准支持
set( CMAKE_CXX_FLAGS "-std=c++11" )
# 寻找OpenCV库
find_package( OpenCV REQUIRED )
# 添加头文件
include_directories( ${OpenCV_INCLUDE_DIRS} )
add_executable( Label_Img Label_Img.cpp )
# 链接OpenCV库
target_link_libraries( Label_Img ${OpenCV_LIBS} )

编译以后

./Label_Img 目标图像目录 保存的参数txt文件目录

在按住鼠标直到松开以后可以画出一个矩形框,这时在终端输入你画的框里的物体所属的类别,回车,继续下一个

结果如图:

这样,就可以在上层目录下生成一个txt文件了

猜你喜欢

转载自blog.csdn.net/CSDN_dzh/article/details/81537117