【opencv】提取论文中曲线坐标重新拟合绘制

                              原图                                      剪切出并处理的曲线

导出到csv格式


最后在matlab中进行拟合或者直接绘制,或者平滑绘制



opencv代码

#include <opencv2/opencv.hpp>
#include <iostream>
#include <fstream>

using namespace cv;
using namespace std;

void main()
{
	vector<Point2f> pt;
	vector<Point2f> pt2;
	string str;

	cout << "图片名:" << endl;
	cin >> str;

	Mat img = imread(str, 0);

	if (img.empty())return;

	Mat bin;
	threshold(img, bin, 128, 255, CV_THRESH_BINARY);

	for (int j = 0; j < bin.cols; j++)
	{
		for (int i = 0; i < bin.rows; i++)
		{
			
			uchar pixel = bin.at<uchar>(i,j);
			if (pixel == 0)
			{
				pt.push_back(Point2f(i, j));//以左下为原点
				break;
			}
		}
	}

	//用户调整部分
	float offsetx = 0; 
	float offsety = 0;
	float xmin = 0, xmax = 1, ymin = 0, ymax = 1;
	int ptmd = 0;//点输出密度
	cout << "请输入横坐标(x)偏移像素数:" << endl;
	cin >> offsetx;
	cout << "请输入纵坐标(y)偏移像素数:" << endl;
	cin >> offsety;
	cout << "请输入横坐标(x)最小值:" << endl;
	cin >> xmin;
	cout << "请输入横坐标(x)最大值:" << endl;
	cin >> xmax;
	cout << "请输入纵坐标(y)最小值:" << endl;
	cin >> ymin;
	cout << "请输入纵坐标(y)最大值:" << endl;
	cin >> ymax;
	cout << "请输入点密度:" << endl;
	cin >> ptmd;

	if (ymax <= ymin || xmax <= xmin)
	{
		cout << "参数有错误" << endl;
		return;
	}

	for (int i = 0; i < pt.size(); i++)
	{
		Point2f newpt;
		newpt.x = pt[i].y;
		newpt.y = bin.rows - pt[i].x - 1;
		newpt.x = (xmax - xmin) * (newpt.x + offsetx) / bin.cols + xmin;
		newpt.y = (ymax - ymin) * (newpt.y + offsety) / bin.rows + ymin;
		pt2.push_back(newpt);
	}
	cout << "计算结束,输出数据" << endl;

	//csv文件写入部分  
	ofstream oFile;  //定义文件输出流     

	oFile.open("坐标.csv", ios::out | ios::trunc);    //打开要输出的文件     这样就很容易的输出一个需要的excel 文件    
	//输出保存数据
	for (int i = 0; i < pt2.size() ;i++)  
	{  
		oFile << pt2[i].x <<","<< pt2[i].y << endl;  
		i+= ptmd;
	}  
}

matlab提取输出坐标

M = csvread('坐标.csv');  
x = M(:,1)';  
y = M(:,2)';    

下载 https://download.csdn.net/download/qq_15947787/10300429

猜你喜欢

转载自blog.csdn.net/qq_15947787/article/details/79641601