将关键点绘制到图像上

目录

以下是代码的主要部分的解释:

        改正的程序:

python版


用于处理图像和标签数据,通过读取标签文件中的关键点信息,将关键点绘制到图像上,然后保存或显示图像。

#/*
 
#	1. 关键点检测部分使用
#	2. 用于可视化变换后的图像
#	3. 也可以用于保存变换后的图像
#	4. 时间:2019.2.13
#*/
 
include<iostream>
include<io.h>
include<fstream>
include<string>
include<opencv.hpp>
 
using namespace std
using namespace cv
 
int main()
{
	const string root_dir = "G:/FacePoints/Transformed_image/";	#// 变换后的图像存放路径
	const string label_dir = "G:/FacePoints/train_trans.txt";	#// 变换后的文件名及标签存放路径
	const string saved_dir = "G:/FacePoints/DisplayFacePoint/";	#// 保存画上了关键点的图像
 
	#// 处理过程:读图->读标签->实时显示图/保存图
	fstream fe;
	fe.open(label_dir, ios::in);
	char Index[6000];
	int dataNum = 0;
	int pointNum = 7;
 
	while (fe.eof() == 0)
	{
		memset(Index, 0, 6000);
		fe.getline(Index, 6000);
		if (Index == "")
			continue;
		char filename[200] = { 0 };
		char points_str[5000] = { 0 };
		sscanf(Index, "%[^ ] %[^\n]", filename, points_str);
 
		#// 读取关键点坐标(以图象左上角为原点)
		vector<float> points;
		char *pnext = NULL;
		char* token = strtok_s(points_str, " ", &pnext);
		while (token != NULL)
		{
			char* newToken = new char[strlen(token) + 1];
			strcpy_s(newToken, strlen(token) + 1, token);
			float tempvalue = atof(newToken);
			
			tempvalue = int(tempvalue * 40 + 20);	#// 将关键点数据变换到变换后的图像上
			
			points.push_back(tempvalue);
			token = strtok_s(NULL, " ", &pnext);
			delete[]newToken;
			newToken = NULL;
		}
 
		#// 读取图像文件
		string full_path = "";
		full_path = root_dir + filename;
		Mat image = imread(full_path);
 
		#// 显示并保存图像
		for (int i = 0; i < pointNum; i++)
		{
			Point temp;
			temp.x = points[i];
			temp.y = points[i + pointNum];
			circle(image, temp, 0.5, Scalar(0, 0, 255), 0.5);
		}
		imshow("facePoints", image);
		waitKey(1);
 
		resize(image, image, Size(100, 100));
		string save_path = "";
		save_path = saved_dir + filename;
		imwrite(save_path, image);
 
		dataNum++;
	}
	fe.close();
 
	return 0;
}

以下是代码的主要部分的解释:

  1. 代码开始时包含了一些注释,描述了代码的主要目的:处理关键点检测、可视化和保存变换后的图像。

  2. 引入了一系列 C++ 头文件,如 <iostream><io.h><fstream><string><opencv.hpp>,以支持图像处理和文件操作。

  3. 代码进入 main 函数,开始主要的处理过程。

  4. root_dir 存储了变换后的图像存放路径,label_dir 存储了变换后的文件名及标签存放路径,saved_dir 存储了保存画上了关键点的图像的路径。

  5. 通过 fstream 打开 label_dir 文件,准备读取图像文件的标签信息。在循环中,每一行包含一个文件名和其对应的关键点坐标。

  6. 图像文件名和关键点坐标信息被解析出来。

  7. 关键点坐标被处理,似乎进行了一些数学运算(例如,tempvalue = int(tempvalue * 40 + 20)),以将关键点数据映射到变换后的图像上。

  8. 通过 imread 读取图像文件,然后将关键点标记绘制到图像上。每个关键点以红色小圆圈表示。

  9. 使用 imshow 显示带有关键点标记的图像,并使用 waitKey 来实现实时显示,waitKey(1) 表示等待1毫秒,可以显示下一帧图像。

  10. 代码将图像调整大小为 100x100 像素,并保存到 saved_dir 目录下。

  11. 循环遍历所有图像后,关闭标签文件。

        主要目的似乎是用于处理标签文件中的关键点数据,将关键点标记绘制到对应的图像上,并保存或显示带有关键点标记的图像。这可能用于图像处理和关键点检测的任务。但需要注意的是,代码中存在一些错误,如 include 应为 #includeusing namespace 行缺少分号,等等。

        改正的程序:

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

using namespace std;
using namespace cv;

int main()
{
    const string root_dir = "G:/FacePoints/Transformed_image/";    // 变换后的图像存放路径
    const string label_dir = "G:/FacePoints/train_trans.txt";       // 变换后的文件名及标签存放路径
    const string saved_dir = "G:/FacePoints/DisplayFacePoint/";     // 保存画上了关键点的图像

    // 处理过程:读图->读标签->实时显示图/保存图
    fstream fe;
    fe.open(label_dir, ios::in);
    char Index[6000];
    int dataNum = 0;
    int pointNum = 7;

    while (!fe.eof())
    {
        memset(Index, 0, 6000);
        fe.getline(Index, 6000);
        if (strlen(Index) == 0)
            continue;
        char filename[200] = { 0 };
        char points_str[5000] = { 0 };
        sscanf(Index, "%[^ ] %[^\n]", filename, points_str);

        // 读取关键点坐标(以图像左上角为原点)
        vector<float> points;
        char* pnext = NULL;
        char* token = strtok_s(points_str, " ", &pnext);
        while (token != NULL)
        {
            char* newToken = new char[strlen(token) + 1];
            strcpy_s(newToken, strlen(token) + 1, token);
            float tempvalue = atof(newToken);

            tempvalue = int(tempvalue * 40 + 20);   // 将关键点数据变换到变换后的图像上

            points.push_back(tempvalue);
            token = strtok_s(NULL, " ", &pnext);
            delete[] newToken;
            newToken = NULL;
        }

        // 读取图像文件
        string full_path = "";
        full_path = root_dir + filename;
        Mat image = imread(full_path);

        // 显示并保存图像
        for (int i = 0; i < pointNum; i++)
        {
            Point temp;
            temp.x = points[i];
            temp.y = points[i + pointNum];
            circle(image, temp, 0.5, Scalar(0, 0, 255), 0.5);
        }
        imshow("facePoints", image);
        waitKey(1);

        resize(image, image, Size(100, 100));
        string save_path = "";
        save_path = saved_dir + filename;
        imwrite(save_path, image);

        dataNum++;
    }
    fe.close();

    return 0;
}

python版

# /*

#	1. 关键点检测部分使用
#	2. 用于可视化变换后的图像
#	3. 也可以用于保存变换后的图像
#	4. 时间:2019.2.13
# */	改正后的pthon版2023.10.13

import cv2
import numpy as np

root_dir = "G:/FacePoints/Transformed_image/"  # 变换后的图像存放路径
label_dir = "G:/FacePoints/train_trans.txt"  # 变换后的文件名及标签存放路径
saved_dir = "G:/FacePoints/DisplayFacePoint/"  # 保存画上了关键点的图像

# 处理过程:读图->读标签->实时显示图/保存图
with open(label_dir, 'r') as fe:
    dataNum = 0
    pointNum = 7

    for line in fe:
        line = line.strip()
        if not line:
            continue

        filename, points_str = line.split(' ', 1)

        # 读取关键点坐标(以图像左上角为原点)
        points = [int(float(x) * 40 + 20) for x in points_str.split()]

        # 读取图像文件
        full_path = root_dir + filename
        image = cv2.imread(full_path)

        # 显示并保存图像
        for i in range(pointNum):
            x, y = points[i], points[i + pointNum]
            cv2.circle(image, (x, y), 1, (0, 0, 255), 1)

        cv2.imshow("facePoints", image)
        cv2.waitKey(1)

        image = cv2.resize(image, (100, 100))
        save_path = saved_dir + filename
        cv2.imwrite(save_path, image)

        dataNum += 1

cv2.destroyAllWindows()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值