[Happy DSA] 图像的contour trace算法C++实现

本文介绍了如何使用Moore-Neighbor算法来提取二维图像的contours。算法从图像底部开始,按照特定顺序扫描,找到黑色像素并进行回溯,记录边界像素。文章提供了C++代码实现,并强调了搜索顺序和结束条件的重要性。
摘要由CSDN通过智能技术生成

问题提出:

给定一个二维图像,基于某个threshold,来提取contours。

在图形图像学中,这个问题有比较好的解决方案,google "coutour trace",可以得到以下2个比较好的参考文献:

1. http://en.wikipedia.org/wiki/Moore_neighborhood

2. http://www.imageprocessingplace.com/downloads_V3/root_downloads/tutorials/contour_tracing_Abeer_George_Ghuneim/index.html


算法描述:

The following is a formal description of the Moore-Neighbor tracing algorithm:

Input: A square tessellation, T, containing a connected component P of black cells.

Output: A sequence B (b1, b2 ,..., bkof boundary pixels i.e. the contour.

Define M(a) to be the 

以下是DSA签名算法C++实现示例: ```cpp #include <openssl/dsa.h> #include <openssl/opensslv.h> #include <iostream> #include <string> #include <vector> using namespace std; // DSA签名算法函数 bool DSA_Sign(const string& message, const string& privateKeyFile, vector<unsigned char>& signature) { // 读取私钥文件 FILE* fp = fopen(privateKeyFile.c_str(), "r"); if (fp == NULL) { cout << "Failed to read private key" << endl; return false; } // 解析私钥 DSA* dsa = PEM_read_DSAPrivateKey(fp, NULL, NULL, NULL); fclose(fp); if (dsa == NULL) { cout << "Failed to parse private key" << endl; return false; } // 计算消息的哈希值 unsigned char md[SHA_DIGEST_LENGTH]; SHA1((unsigned char*)message.c_str(), message.length(), md); // 签名 unsigned int sig_len; signature.resize(DSA_size(dsa)); if (!DSA_sign(0, md, SHA_DIGEST_LENGTH, &signature[0], &sig_len, dsa)) { cout << "Failed to sign message" << endl; DSA_free(dsa); return false; } signature.resize(sig_len); // 释放资源 DSA_free(dsa); return true; } // DSA验证签名函数 bool DSA_Verify(const string& message, const string& publicKeyFile, const vector<unsigned char>& signature) { // 读取公钥文件 FILE* fp = fopen(publicKeyFile.c_str(), "r"); if (fp == NULL) { cout << "Failed to read public key" << endl; return false; } // 解析公钥 DSA* dsa = PEM_read_DSA_PUBKEY(fp, NULL, NULL, NULL); fclose(fp); if (dsa == NULL) { cout << "Failed to parse public key" << endl; return false; } // 计算消息的哈希值 unsigned char md[SHA_DIGEST_LENGTH]; SHA1((unsigned char*)message.c_str(), message.length(), md); // 验证签名 int ret = DSA_verify(0, md, SHA_DIGEST_LENGTH, &signature[0], signature.size(), dsa); // 释放资源 DSA_free(dsa); return (ret == 1); } int main() { // 要签名的消息 string message = "Hello, world!"; // 签名 vector<unsigned char> signature; if (!DSA_Sign(message, "dsa_private_key.pem", signature)) { cout << "Failed to sign message" << endl; return -1; } // 输出签名结果 cout << "Signature: "; for (size_t i = 0; i < signature.size(); i++) { printf("%02X", signature[i]); } cout << endl; // 验证签名 if (DSA_Verify(message, "dsa_public_key.pem", signature)) { cout << "Signature verified" << endl; } else { cout << "Signature verification failed" << endl; } return 0; } ``` 注意,该示例使用了OpenSSL库来实现DSA签名算法。在使用之前需要安装并配置好OpenSSL库。另外,需要替换私钥文件和公钥文件的文件名为实际的文件名。
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值