图像处理:亮度加权
Image processing: luminance weighted
我想权衡亮度值
示例:
我有一个亮度值向量:
vector <int> lum {50,100,150,200,250);
我有一个系数向量:
vector <float> coef {5.1 , 2.55 , 1.7 , 1.275, 1.02 }
我创建了一个空图像:
Mat1 m(15):
所以,我的第一个亮度值是 50 (lum[0]=50),我希望它应用于矩阵的 5.1 (coef[0]=5.1) 第一个像素。为此,我需要用第一个和第二个亮度值对第 6 个像素进行加权。在我的例子中,我的第 6 个像素的亮度将是 95,因为 (0.1*50)+ (0.9*100)=95
目前,对于第二个系数(coef[1]=2.55),我在之前的计算中使用了 0.9 on 2.55。该系数仍为 1,65,因此第 7 个像素的亮度为 100,第 8 个像素的亮度为 (0.65*100)+ (0.35*150) = 117,5.
等等...
其实我有这个:
//Blibliothèque Opencv
#include "opencv2/core/core.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/imgcodecs.hpp"
#include "opencv2/highgui/highgui.hpp"
// cpp include
#include <iostream>
#include <cmath>
#include <math.h>
#include <string.h>
#include <vector>
#include <opencv2/opencv.hpp>
#define MPI 3.14159265358979323846264338327950288419716939937510
#define RAD2DEG (180./MPI)
using namespace cv;
using namespace std;
vector <int> lum{ 50, 100, 150, 200, 250 };
vector <float> coef (5,0);
vector <int> newv(15, 0);
float pixelRef = 255, angle = 0, coeff = 0;
int main()
{
for (int n = 0; n < lum.size(); ++n)
{
//get angle
angle = ((acos(lum[n] / pixelRef)));
cout << "angle :" << angle*RAD2DEG << endl;
// get coefficient
coef [n] = (1 / (cos(angle)));
cout << "coeff :" << coef [n] << endl;
// try to weighted my pixels
newv[n] = (coef*lum[n]) + ((1 - coeff)*lum[n + 1]);
}
return 0;
}
我将 coef
的最后一个元素修改为 3.02f
以表明此代码也能很好地处理最后一个元素。结果序列为:
50, 50, 50, 50, 50, 95, 100, 117.5, 150, 182.5, 218.75, 250, 250,
代码可能 re-written 更好,但我会把它留给你:
#include <opencv2/opencv.hpp>
#include <vector>
#include <iostream>
using namespace cv;
using namespace std;
int main()
{
vector <int> lum{ 50, 100, 150, 200, 250 };
vector <float> coef{ 5.1f, 2.55f, 1.7f, 1.275f, 3.02f };
vector<float> v;
int idx_lum = 0;
int idx_coef = 0;
while (true)
{
int c = int(coef[idx_coef]);
for (int i = 0; i < c; ++i)
{
v.push_back(float(lum[idx_lum]));
}
float alpha = fmod(coef[idx_coef], 1.f);
float beta = 1.f - alpha;
v.push_back(alpha * lum[idx_lum] + beta * lum[idx_lum + 1]);
idx_lum++;
idx_coef++;
coef[idx_coef] = coef[idx_coef] - beta;
if (idx_lum >= lum.size() - 1 || idx_coef >= coef.size() - 1)
{
int cc = int(coef[idx_coef]);
for (int i = 0; i < cc; ++i)
{
v.push_back(float(lum[idx_lum]));
}
// Only if the last remainder is needed
//float alpha = fmod(coef[idx_coef], 1.f);
//v.push_back(alpha * lum[idx_lum]);
break;
}
}
// Print out the values
copy(v.begin(), v.end(), ostream_iterator<float>(cout, ", "));
// Get a cv::Mat from the std::vector
Mat1f m = Mat1f(v).t();
return 0;
}
我想权衡亮度值
示例: 我有一个亮度值向量:
vector <int> lum {50,100,150,200,250);
我有一个系数向量:
vector <float> coef {5.1 , 2.55 , 1.7 , 1.275, 1.02 }
我创建了一个空图像:
Mat1 m(15):
所以,我的第一个亮度值是 50 (lum[0]=50),我希望它应用于矩阵的 5.1 (coef[0]=5.1) 第一个像素。为此,我需要用第一个和第二个亮度值对第 6 个像素进行加权。在我的例子中,我的第 6 个像素的亮度将是 95,因为 (0.1*50)+ (0.9*100)=95
目前,对于第二个系数(coef[1]=2.55),我在之前的计算中使用了 0.9 on 2.55。该系数仍为 1,65,因此第 7 个像素的亮度为 100,第 8 个像素的亮度为 (0.65*100)+ (0.35*150) = 117,5.
等等...
其实我有这个:
//Blibliothèque Opencv
#include "opencv2/core/core.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/imgcodecs.hpp"
#include "opencv2/highgui/highgui.hpp"
// cpp include
#include <iostream>
#include <cmath>
#include <math.h>
#include <string.h>
#include <vector>
#include <opencv2/opencv.hpp>
#define MPI 3.14159265358979323846264338327950288419716939937510
#define RAD2DEG (180./MPI)
using namespace cv;
using namespace std;
vector <int> lum{ 50, 100, 150, 200, 250 };
vector <float> coef (5,0);
vector <int> newv(15, 0);
float pixelRef = 255, angle = 0, coeff = 0;
int main()
{
for (int n = 0; n < lum.size(); ++n)
{
//get angle
angle = ((acos(lum[n] / pixelRef)));
cout << "angle :" << angle*RAD2DEG << endl;
// get coefficient
coef [n] = (1 / (cos(angle)));
cout << "coeff :" << coef [n] << endl;
// try to weighted my pixels
newv[n] = (coef*lum[n]) + ((1 - coeff)*lum[n + 1]);
}
return 0;
}
我将 coef
的最后一个元素修改为 3.02f
以表明此代码也能很好地处理最后一个元素。结果序列为:
50, 50, 50, 50, 50, 95, 100, 117.5, 150, 182.5, 218.75, 250, 250,
代码可能 re-written 更好,但我会把它留给你:
#include <opencv2/opencv.hpp>
#include <vector>
#include <iostream>
using namespace cv;
using namespace std;
int main()
{
vector <int> lum{ 50, 100, 150, 200, 250 };
vector <float> coef{ 5.1f, 2.55f, 1.7f, 1.275f, 3.02f };
vector<float> v;
int idx_lum = 0;
int idx_coef = 0;
while (true)
{
int c = int(coef[idx_coef]);
for (int i = 0; i < c; ++i)
{
v.push_back(float(lum[idx_lum]));
}
float alpha = fmod(coef[idx_coef], 1.f);
float beta = 1.f - alpha;
v.push_back(alpha * lum[idx_lum] + beta * lum[idx_lum + 1]);
idx_lum++;
idx_coef++;
coef[idx_coef] = coef[idx_coef] - beta;
if (idx_lum >= lum.size() - 1 || idx_coef >= coef.size() - 1)
{
int cc = int(coef[idx_coef]);
for (int i = 0; i < cc; ++i)
{
v.push_back(float(lum[idx_lum]));
}
// Only if the last remainder is needed
//float alpha = fmod(coef[idx_coef], 1.f);
//v.push_back(alpha * lum[idx_lum]);
break;
}
}
// Print out the values
copy(v.begin(), v.end(), ostream_iterator<float>(cout, ", "));
// Get a cv::Mat from the std::vector
Mat1f m = Mat1f(v).t();
return 0;
}