边框扩张

#include "tinyxml.h"
#include <iostream>
#include <string>
#include <windows.h>
#include<vector>
#include<fstream>
#include<opencv2/opencv.hpp>
#include <time.h> 

using namespace std;
using namespace cv;

int main()
{
	srand((unsigned)time(NULL));
	//data值全部存到out.txt里面 存的格式需要后续处理
	//ofstream outfile("out.txt");
	fstream fout("../label.txt", ios::out);
	//string filefullname = "D:\\openpose\\windows\\OpenPoseDemo\\image\\1.xml";		
	//const char* filepath = filefullname.c_str();;
	//TiXmlDocument doc(filepath);
	//TiXmlDocument doc("D:\\openpose\\windows\\OpenPoseDemo\\image\\1.xml");
	string path = "D:/openpose/windows/OpenPoseDemo/image/";
	string path2 = "samples";
	char imgnames[200];
	char xmlnames[200] ;
	char labelnames[200];
	int xmlnumber = 1, imgnumber = 1, labelnumber = 1;

	for (int i = 0; i < 182; i++)
	{
		ofstream outfile("out.txt");
		sprintf(xmlnames, "%s%06d%s", path.c_str(), xmlnumber++, "_pose.xml");
	//	sprintf(xmlnames, "%s%06d%s", "D:/openpose/windows/OpenPoseDemo/image/", xmlnumber++, "_pose.xml");
		TiXmlDocument doc(xmlnames);
		//XmlDocument doc("D:\\openpose\\windows\\OpenPoseDemo\\image\\000004_pose.xml");
		bool loadOkay = doc.LoadFile(TIXML_DEFAULT_ENCODING);
		/*f (!loadOkay) {
			printf("Could not load test file %s. Error='%s'. Exiting.\n", filepath, doc.ErrorDesc());
			exit(1);
			}*/
		TiXmlElement* root = doc.RootElement();
		for (TiXmlElement* elem = root->FirstChildElement(); elem != NULL; elem = elem->NextSiblingElement())
		{
			string elemName = elem->Value();
			if (!elemName.compare("pose_0"))
			{
				for (TiXmlElement* pose_0elem = elem->FirstChildElement(); pose_0elem != NULL; pose_0elem = pose_0elem->NextSiblingElement())
				{
					string pose_0elemName = pose_0elem->Value();

					if (!pose_0elemName.compare("data"))
					{
						TiXmlNode *e = pose_0elem->FirstChild();
						const char *expr = e->ToText()->Value();
						char *buf = new char[strlen(expr) + 1];
						/*	strcpy(buf, expr);
							cout << expr << endl;*/
						outfile << expr << endl;
					}
				}
			}
		}
		ifstream infile("out.txt");
		vector<float>datanums;
		float keys;
		while (infile >> keys)
		{
			datanums.push_back(keys);
		}

		float maxx = 0;
		float maxy = 0;

		for (int i = 0; i < 45; i++)      //datanums.size()

		{
			if (i % 3 == 0)
			{
				if (datanums[i]>maxx)
				{
					maxx = datanums[i];
				}
			}
			if (i % 3 == 1)
			{
				if (datanums[i] > maxy)
				{
					maxy = datanums[i];
				}
			}
		}
		float minx = maxx;
		float miny = maxy;
		for (int i = 0; i < 45; i++)
		{

			if (i % 3 == 0)
			{
				/*	if (datanums[i] == 0 & datanums[i + 1] == 0)
						i = i + 3;*/
				if (datanums[i] == 0)
					continue;
				if (datanums[i] < minx)
				{
					minx = datanums[i];
				}
			}
			if (i % 3 == 1)
			{
				if (datanums[i] == 0)
					continue;
				if (datanums[i] < miny)
				{
					miny = datanums[i];
				}
			}
		}
		//Mat img = imread("D:\\openpose\\windows\\OpenPoseDemo\\image\\c_rendered.jpg");
		//Mat img = imread("D:\\openpose\\examples\\media\\COCO_val2014_000000000564.jpg");
		//Mat img = imread("D:\\openpose\\windows\\OpenPoseDemo\\image\\COCO_val2014_000000000192_000000000000_rendered.jpg");
		sprintf(imgnames, "%s%06d%s", "D:/openpose/windows/OpenPoseDemo/picture/samples/", imgnumber++, ".jpg");
		Mat img = imread(imgnames);


		float wide = maxx - minx;
		float height = maxy - miny;


		miny = miny - height*0.05;
		if (miny <0)
			miny = 5;
		maxy = maxy + height*0.15;  
		if (maxy > img.rows)
			maxy = img.rows - 5;
		minx = minx - wide*0.2;
		if (minx < 0)
			minx = 5;
		maxx = maxx + wide*0.2;
		if (maxx>img.cols)
			maxx = img.cols - 5;
		/*for (int j = 0; j < 4; j++)
		{*/
		sprintf(labelnames, "%s%06d%s", "samples/",labelnumber++, ".jpg");
		fout << labelnames << " ";
		fout << minx << " " << miny << " " << maxx << " " << maxy << " ";
		fout << datanums[0] << " " << datanums[1] << " " << datanums[3] << " " << datanums[4] << " " << datanums[24] << " " << datanums[25] << " " << datanums[30] << " " << datanums[31] << " " << datanums[33] << " " << datanums[34] << " " << datanums[39] << " " << datanums[40] << endl;


		//	cv::circle(img, alignment[i][j], 1, cv::Scalar(255, 255, 0), 3);      //5
		circle(img, Point2d(datanums[0], datanums[1]), 3, Scalar(255, 0, 0), -1, 8, 0);
		circle(img, Point2d(datanums[3], datanums[4]), 3, Scalar(255, 0, 0), -1, 8, 0);
		circle(img, Point2d(datanums[24], datanums[25]), 3, Scalar(255, 0, 0), -1, 8, 0);
		circle(img, Point2d(datanums[30], datanums[31]), 3, Scalar(255, 0, 0), -1, 8, 0);
		circle(img, Point2d(datanums[33], datanums[34]), 3, Scalar(255, 0, 0), -1, 8, 0);
		circle(img, Point2d(datanums[39], datanums[40]), 6, Scalar(255, 0, 0), -1, 8, 0);


	cout << maxx << ' ' << maxy << endl;
	cout << minx << ' ' << miny << endl;
	Rect bbox(minx, miny, maxx - minx, maxy - miny);
	rectangle(img, bbox, cv::Scalar(255, 0, 0), 2);
	imshow("bbox", img);
	waitKey(0);
	outfile.close();
	
    //return 0;
}
	fout.close();
	}







multi people

#include "tinyxml.h"
#include <iostream>
#include <string>
#include <windows.h>
#include<vector>
#include<fstream>
#include<opencv2/opencv.hpp>
#include <time.h> 

using namespace std;
using namespace cv;

int main()
{
	srand((unsigned)time(NULL));
	//data值全部存到out.txt里面 存的格式需要后续处理
	//ofstream outfile("out.txt");
	fstream fout("../label.txt", ios::out);
	//string filefullname = "D:\\openpose\\windows\\OpenPoseDemo\\image\\1.xml";		
	//const char* filepath = filefullname.c_str();;
	//TiXmlDocument doc(filepath);
	//TiXmlDocument doc("D:\\openpose\\windows\\OpenPoseDemo\\image\\1.xml");
	string path = "D:/openpose/windows/OpenPoseDemo/image/";
	string path2 = "samples";
	char imgnames[200];
	char xmlnames[200] ;
	char labelnames[200];
	int xmlnumber = 1, imgnumber = 0, labelnumber = 1;

	for (int i = 0; i < 282; i++)
	{
		cout << i+1 << " picture" << endl;
		ofstream outfile("out.txt");
		sprintf(xmlnames, "%s%06d%s", path.c_str(), xmlnumber++, "_pose.xml");
	//	sprintf(xmlnames, "%s%06d%s", "D:/openpose/windows/OpenPoseDemo/image/", xmlnumber++, "_pose.xml");
		TiXmlDocument doc(xmlnames);
		//XmlDocument doc("D:\\openpose\\windows\\OpenPoseDemo\\image\\000004_pose.xml");
		bool loadOkay = doc.LoadFile(TIXML_DEFAULT_ENCODING);
		/*f (!loadOkay) {
			printf("Could not load test file %s. Error='%s'. Exiting.\n", filepath, doc.ErrorDesc());
			exit(1);
			}*/
		TiXmlElement* root = doc.RootElement();
		for (TiXmlElement* elem = root->FirstChildElement(); elem != NULL; elem = elem->NextSiblingElement())
		{
			string elemName = elem->Value();
			if (!elemName.compare("pose_0"))
			{
				for (TiXmlElement* pose_0elem = elem->FirstChildElement(); pose_0elem != NULL; pose_0elem = pose_0elem->NextSiblingElement())
				{
					string pose_0elemName = pose_0elem->Value();

					if (!pose_0elemName.compare("data"))
					{
						TiXmlNode *e = pose_0elem->FirstChild();
						const char *expr = e->ToText()->Value();
						char *buf = new char[strlen(expr) + 1];
						/*	strcpy(buf, expr);
							cout << expr << endl;*/
						outfile << expr << endl;
					}
				}
			}
		}
		ifstream infile("out.txt");
		vector<float>datanums;
		float keys;
		while (infile >> keys)
		{
			datanums.push_back(keys);
		}
		//int t = sizeof(datanums)/45.0;
		int t = datanums.size() / 45.0;
		//int t = 4;
	
		
		for (int j = 0; j < t; j++)
		{
			float maxx = 0;
			float maxy = 0;
			for (int i = j * 45; i <45 * (j + 1); i++)      //datanums.size()

			{

				if (i % 3 == 0)
				{
					if (datanums[i]>maxx)
					{
						maxx = datanums[i];
					}
				}
				if (i % 3 == 1)
				{
					if (datanums[i] > maxy)
					{
						maxy = datanums[i];
					}
				}
			}
			float minx = maxx;
			float miny = maxy;
			for (int i = j * 45; i < 45 * (j + 1); i++)
			{

				if (i % 3 == 0)
				{
					/*	if (datanums[i] == 0 & datanums[i + 1] == 0)
							i = i + 3;*/
					if (datanums[i] == 0)
						continue;
					if (datanums[i] < minx)
					{
						minx = datanums[i];
					}
				}
				if (i % 3 == 1)
				{
					if (datanums[i] == 0)
						continue;
					if (datanums[i] < miny)
					{
						miny = datanums[i];
					}
				}
			}
			//Mat img = imread("D:\\openpose\\windows\\OpenPoseDemo\\image\\c_rendered.jpg");
			//Mat img = imread("D:\\openpose\\examples\\media\\COCO_val2014_000000000564.jpg");
			//Mat img = imread("D:\\openpose\\windows\\OpenPoseDemo\\image\\COCO_val2014_000000000192_000000000000_rendered.jpg");
			if (j == 0)
				imgnumber = imgnumber + 1;
				sprintf(imgnames, "%s%06d%s", "D:/openpose/windows/OpenPoseDemo/picture/samples/", imgnumber, ".jpg");
			
			   
			Mat img = imread(imgnames);



			float height = maxy - miny;


			miny = miny - height*0.03;
			if (miny <0)
				miny = 8;
			maxy = maxy + height*0.12;
			if (maxy > img.rows)
				maxy = img.rows - 8;
			minx = datanums[42 + j * 45] - (1.0 / 2)*0.5* (maxy - miny);  //0.5*0.5* (maxy - miny);
			if (minx < 0)
				minx = 8;
			maxx = datanums[42 + j * 45] + (1.0 / 2)*0.5* (maxy - miny);
			if (maxx>img.cols)
				maxx = img.cols - 8;

			//miny = miny ;
			//if (miny <0)
			//	miny = 8;
			//maxy = maxy + height*0.1;
			//if (maxy > img.rows)
			//	maxy = img.rows - 8;
			minx = datanums[42] - (1.0 / 2)*0.5* (maxy - miny);  //0.5*0.5* (maxy - miny);
			if (minx < 0)
				minx = 8;
			maxx = datanums[42] + (1.0 / 2)*0.5* (maxy - miny);
			if (maxx>img.cols)
				maxx = img.cols - 8;

			if (j == 0)
			{
				sprintf(labelnames, "%s%06d%s", "samples/", labelnumber++, ".jpg");
				fout << labelnames << " ";
			
			}
			if (j == t - 1)
			{
				fout << minx << " " << miny << " " << maxx << " " << maxy << " " << datanums[0 + j * 45] << " " << datanums[1 + j * 45] << " " << datanums[3 + j * 45] << " " << datanums[4 + j * 45] << " " << datanums[24 + j * 45] << " " << datanums[25 + j * 45] << " " << datanums[30 + j * 45] << " " << datanums[31 + j * 45] << " " << datanums[33 + j * 45] << " " << datanums[34 + j * 45] << " " << datanums[39 + j * 45] << " " << datanums[40 + j * 45] << endl;
			}
			else{
				fout << minx << " " << miny << " " << maxx << " " << maxy << " " << datanums[0 + j * 45] << " " << datanums[1 + j * 45] << " " << datanums[3 + j * 45] << " " << datanums[4 + j * 45] << " " << datanums[24 + j * 45] << " " << datanums[25 + j * 45] << " " << datanums[30 + j * 45] << " " << datanums[31 + j * 45] << " " << datanums[33 + j * 45] << " " << datanums[34 + j * 45] << " " << datanums[39 + j * 45] << " " << datanums[40 + j * 45] << " ";
			}
			
			//	cv::circle(img, alignment[i][j], 1, cv::Scalar(255, 255, 0), 3);      //5
			circle(img, Point2d(datanums[0 + j * 45], datanums[1 + j * 45]), 3, Scalar(255, 0, 0), -1, 8, 0);
			circle(img, Point2d(datanums[3 + j * 45], datanums[4 + j * 45]), 3, Scalar(255, 0, 0), -1, 8, 0);
			circle(img, Point2d(datanums[24 + j * 45], datanums[25 + j * 45]), 3, Scalar(255, 0, 0), -1, 8, 0);
			circle(img, Point2d(datanums[30 + j * 45], datanums[31 + j * 45]), 3, Scalar(255, 0, 0), -1, 8, 0);
			circle(img, Point2d(datanums[33 + j * 45], datanums[34 + j * 45]), 3, Scalar(255, 0, 0), -1, 8, 0);
			circle(img, Point2d(datanums[39 + j * 45], datanums[40 + j * 45]), 6, Scalar(255, 0, 0), -1, 8, 0);
			circle(img, Point2d(datanums[42 + j * 45], datanums[43 + j * 45]), 6, Scalar(255, 0, 0), -1, 8, 0);


			cout << maxx << ' ' << maxy << endl;
			cout << minx << ' ' << miny << endl;
			Rect bbox(minx, miny, maxx - minx, maxy - miny);
			rectangle(img, bbox, cv::Scalar(255, 0, 0), 2);
			imshow("bbox", img);
			waitKey(0);
			//outfile.close();
		}
		//return 0;
		outfile.close();
		}
		fout.close();
	}



裁剪中间的腰:


#include "tinyxml.h"
#include <iostream>
#include <string>
#include <windows.h>
#include<vector>
#include<fstream>
#include<opencv.hpp>
#include <time.h> 

using namespace std;
using namespace cv;

int main()
{
	srand((unsigned)time(NULL));
	//data值全部存到out.txt里面 存的格式需要后续处理
	ifstream fin("filename_val.txt");
	char line[1024] = { 0 };
	string filename = "";
	string path_xml = "F:/program/tinyxml/xml_val/";//xml目录
	string path_pic = "F:/program/tinyxml/pic_val/";//图片目录
	char xmlnames[200];
	
	jump:
	while (fin.getline(line, sizeof(line)))
	{
		stringstream word(line);
		word >> filename;
		ofstream outfile("out.txt");
		string picdir = path_pic + filename + ".jpg";//图片完整路径
		sprintf(xmlnames, "%s%s%s", path_xml.c_str(), filename.c_str(), "_pose.xml");//xml完整路径

		TiXmlDocument doc(xmlnames);
		bool loadOkay = doc.LoadFile(TIXML_DEFAULT_ENCODING);
		TiXmlElement* root = doc.RootElement();
		for (TiXmlElement* elem = root->FirstChildElement(); elem != NULL; elem = elem->NextSiblingElement())
		{
			string elemName = elem->Value();
			if (!elemName.compare("pose_0"))
			{
				for (TiXmlElement* pose_0elem = elem->FirstChildElement(); pose_0elem != NULL; pose_0elem = pose_0elem->NextSiblingElement())
				{
					string pose_0elemName = pose_0elem->Value();

					if (!pose_0elemName.compare("data"))
					{
						TiXmlNode *e = pose_0elem->FirstChild();
						if (e == NULL){
							fin.clear();
							goto jump;
						}
						const char *expr = e->ToText()->Value();
						char *buf = new char[strlen(expr) + 1];
						outfile << expr << endl;
					}
				}
			}
		}
		ifstream infile("out.txt");
		vector<float>datanums;
		float keys;
		while (infile >> keys)
		{
			datanums.push_back(keys);
		}
		int t = 1; 
		if (datanums[4] == 0 || datanums[5] == 0 || datanums[42] == 0 || datanums[25] == 0)
			continue;
		for (int j = 0; j < t; j++)
		{
			float maxx = 0;
			float maxy = 0;
			for (int i = j * 45; i <45 * (j + 1); i++)      //datanums.size()
			{
				if (i % 3 == 0)
				{
					if (datanums[i]>maxx)
					{
						maxx = datanums[i];
					}
				}
				if (i % 3 == 1)
				{
					if (datanums[i] > maxy)
					{
						maxy = datanums[i];
					}
				}
			}
			float minx = maxx;
			float miny = maxy;
			for (int i = j * 45; i < 45 * (j + 1); i++)
			{
				if (i % 3 == 0)
				{
					if (datanums[i] == 0)
						continue;
					if (datanums[i] < minx)
					{
						minx = datanums[i];
					}
				}
				if (i % 3 == 1)
				{
					if (datanums[i] == 0)
						continue;
					if (datanums[i] < miny)
					{
						miny = datanums[i];
					}
				}
			}
			
			Mat img = imread(picdir);
			float height = maxy - miny;
			miny = datanums[4 + j * 45] - height*0.03;
			if (miny <0)
				miny = 8;
			maxy = datanums[25 + j * 45] + height*0.12;
			if (maxy > img.rows)
				maxy = img.rows - 8;
			minx = datanums[42 + j * 45] - 1.0 / 2 * (maxy - miny);  //0.5*0.5* (maxy - miny);
			if (minx < 0)
				minx = 8;
			maxx = datanums[42 + j * 45] + 1.0 / 2 * (maxy - miny);
			if (maxx>img.cols)
				maxx = img.cols - 8;

			float Width = maxx - minx;
			float Height = maxy - miny;
			string path_out = "F:/program/tinyxml/out/" + filename + ".jpg";//输出路径
			imwrite(path_out, img(Rect(minx, miny, Width, Height)));//保存ROI区域
			在图片上显示关键点
			//circle(img, Point2d(datanums[0 + j * 45], datanums[1 + j * 45]), 3, Scalar(0, 0, 255), -1, 8, 0);
			//circle(img, Point2d(datanums[3 + j * 45], datanums[4 + j * 45]), 3, Scalar(0, 0, 255), -1, 8, 0);
			//circle(img, Point2d(datanums[24 + j * 45], datanums[25 + j * 45]), 3, Scalar(0, 0, 255), -1, 8, 0);
			//circle(img, Point2d(datanums[30 + j * 45], datanums[31 + j * 45]), 3, Scalar(0, 0, 255), -1, 8, 0);
			//circle(img, Point2d(datanums[33 + j * 45], datanums[34 + j * 45]), 3, Scalar(0, 0, 255), -1, 8, 0);
			//circle(img, Point2d(datanums[39 + j * 45], datanums[40 + j * 45]), 6, Scalar(0, 0, 255), -1, 8, 0);
			//circle(img, Point2d(datanums[42 + j * 45], datanums[43 + j * 45]), 6, Scalar(0, 0, 255), -1, 8, 0);


			//cout << maxx << ' ' << maxy << endl;
			//cout << minx << ' ' << miny << endl;
			//Rect bbox(minx, miny, maxx - minx, maxy - miny);
			//rectangle(img, bbox, cv::Scalar(255, 0, 0), 2);
			//imshow("bbox", img);

			//测试
			cout << filename << endl;
		}
		fin.clear();
	}
	fin.close();
}






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值