You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

141 lines
2.5 KiB

#include <memory>
#include "utils.h"
#include "opencv2/opencv.hpp"
using namespace std;
using namespace cv;
void Map16BitTo8Bit(unsigned short* psh16BitData, long lDataLen, unsigned char* pby8BitData)
{
if (psh16BitData == NULL || pby8BitData == NULL || lDataLen <= 0)
{
return;
}
//指向直方图的数据指针
int* pHist = new int[65536];
memset(pHist, 0, 65536 * sizeof(int));
int i = 0;
for (i = 0; i < lDataLen; i += 10)
{
pHist[psh16BitData[i]]++;
}
//设置阈值大小为: AreaSigma*图象大小/100
int nSigma = (int)(0.03 * lDataLen);
int nSum = 0;
int nMin = 0;
int nMax = 0;
//求映射的最大最小值
for (i = 0; i < 65536; i++)
{
nSum += pHist[i];
if (nSum >= nSigma)
{
nMin = i;
break;
}
}
nSum = 0;
for (i = 65535; i >= 0; i--)
{
nSum += pHist[i];
if (nSum >= nSigma)
{
nMax = i;
break;
}
}
//计算对比度亮度
float K = (float)(120.0 / (nMax - nMin + 1));
float C = (float)(-K * nMin);
//图像映射
for (i = 0; i < lDataLen; i++)
{
int nValue = (int)(K * psh16BitData[i] + C);
if (nValue < 0)
{
pby8BitData[i] = 0;
}
else if (nValue > 255)
{
pby8BitData[i] = 255;
}
else
{
pby8BitData[i] = nValue;
}
}
delete[] pHist;
}
SimTargetImage_Y16::SimTargetImage_Y16(int w, int h)
{
pSrc = new unsigned short[w * h];
Y8Mat = cv::Mat(h,w,CV_8UC1);
nImageWidth = w;
nImageHeight = h;
}
SimTargetImage_Y16::~SimTargetImage_Y16()
{
if (pSrc)
{
delete[]pSrc;
pSrc = nullptr;
}
}
void SimTargetImage_Y16::setBackGround(int gray, int std)
{
for (size_t i = 0; i < nImageHeight; i++)
{
for (size_t j = 0; j < nImageWidth; j++)
{
pSrc[i * nImageWidth + j] = rand() % std + gray;
}
}
}
void SimTargetImage_Y16::addTarget(int x, int y, int w, int h, int gray)
{
for (size_t i = y - h/2; i < y + h/2; i++)
{
for (size_t j = x - w/2; j < x+w/2; j++)
{
pSrc[i * nImageWidth + j] = gray;
}
}
}
unsigned short* SimTargetImage_Y16::getImageData()
{
return pSrc;
}
cv::Mat SimTargetImage_Y16::getMatRGB()
{
Map16BitTo8Bit(pSrc, nImageHeight * nImageWidth, Y8Mat.data);
cv::Mat rgb;
cv::cvtColor(Y8Mat,rgb,COLOR_GRAY2RGB);
return rgb.clone();
}