本博文是基于可见成像光通信的解调算法代码。包括:
- ROI区域的提取
- 阈值化
直接给出代码。由于是付费的博客,有什么疑问随时交流哈~
更多本人做的可见光通信工作请见博客:《基于可见光通信的移动机器人室内定位及物联网应用》
#include <stdio.h>
#include <stdlib.h>
#include "vlcCommonInclude.hpp"
#include "std_msgs/String.h"
#include <sstream>
#include "imageProcess.hpp"
#include "positioningCalculation.hpp"
using namespace cv;
using namespace std;
struct PxielPoint {
double i;
int val;
};
Mat polyfit(vector<Point>& in_point, int n)
{
int size = in_point.size();
//所求未知数个数
int x_num = n + 1;
//构造矩阵U和Y
Mat mat_u(size, x_num, CV_64F);
Mat mat_y(size, 1, CV_64F);
for (int i = 0; i < mat_u.rows; ++i)
for (int j = 0; j < mat_u.cols; ++j)
{
mat_u.at<double>(i, j) = pow(in_point[i].x, j);
}
for (int i = 0; i < mat_y.rows; ++i)
{
mat_y.at<double>(i, 0) = in_point[i].y;
}
//矩阵运算,获得系数矩阵K
Mat mat_k(x_num, 1, CV_64F);
mat_k = (mat_u.t()*mat_u).inv()*mat_u.t()*mat_y;
cout << mat_k << endl;
return mat_k;
}
// ID识别函数
int main() {
//准确的ROI捕获
cv::Mat imageLED1 = imread("/home/kwanwaipang/桌面/123/test2048/frame0011.jpg");
// resize(imageLED1,imageLED1,Size(1280,960),0,0,INTER_NEAREST);
//转换为灰度图
Mat grayImage;//�Ҷ�ͼ
cv::cvtColor(imageLED1, grayImage, cv::COLOR_BGR2GRAY);
//imshow("grayImage", grayImage);
///二值化
double m_threshold;//
cv::Mat matBinary;//
m_threshold = getThreshVal_Otsu_8u(grayImage);//
threshold(grayImage, matBinary, m_threshold, 255, 0); //
// imshow("matBinary", matBinary);
Mat matBinary6 = matBinary.clone();
//
Mat element = getStructuringElement(MORPH_ELLIPSE, Size(20, 20));
morphologyEx(matBinary, matBinary, MORPH_CLOSE, element);
//
bwareaopen(matBinary, 500);
// imshow("matBinary-1", matBinary);//连通区域
int Img_local_X1, Img_local_Y1, Img_local_X2, Img_local_Y2, Img_local_X3, Img_local_Y3;
Mat img1_next, matBinary11, img2_next, matBinary2, img3_next, matBinary3;
int X1_min, X1_max, Y1_min, Y1_max, X2_min, X2_max, Y2_min, Y2_max, X3_min, X3_max, Y3_min, Y3_max;
for (int ii = 1;ii < 4;ii++)
{
int X_min, X_max, Y_min, Y_max;
Mat img_next;
ls_LED(matBinary, X_min, X_max, Y_min, Y_max, img_next);
//获得LED1像素中心的位置
double Img_local_X = (X_max + X_min) / 2;
double Img_local_Y = (Y_max + Y_min) / 2;
// 将原图中LED1部分的区域变黑
// 获取图像的行列
double rowB = matBinary.rows; 二值化图像的行数
double colB = matBinary.cols;//二值化图像的列数
Mat matBinary1 = matBinary.clone();//����һ��ͼ������ȥ��LED1��ͼ
for (double i = 0;i < rowB;i++)
{
for (double j = 0;j < colB;j++)
{
double r = pow((i - Img_local_Y), 2) + pow((j - Img_local_X), 2) - pow(((abs(X_max - X_min)) / 2 - 2), 2);//pow(x,y)����x��y�η�
if (r - 360 > 0)//
{
//
matBinary1.at<uchar>(i, j) = matBina