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.
 
 
 
 
 
 

370 lines
8.7 KiB

#include "multipoint.h"
#include "QDebug"
#include <QFile>
#include <QFileDialog>
#include <QDir>
MultiPoint::MultiPoint(QObject *parent)
: QObject{parent}
{
thread = new QThread();
thread->start();
this->moveToThread(thread);
ready = false;
b_save = false;
n_save = 0;
stagecount =0;
ittime = 5;
enable = false;
b_rmbld = false;
b_thre = 4;
stages = nullptr;
kstages = nullptr;
bstages = nullptr;
}
void MultiPoint::saveData(cv::Mat img)
{
cv::Mat temp;
img.copyTo(temp);
datas[cv::mean(img)[0]/4] = temp;
emit signalNewFrame(cv::mean(img)[0]/4);
}
void MultiPoint::calcData(QVector<double> vct)
{
qDebug()<<__FILE__<<__LINE__;
ready = false;
stagecount = vct.length();
if(stagecount<2)
return;
// cv::Mat mask(rows,cols,CV_8UC1);
// for(int i = 0;i<rows;i++){
// for(int j = 0;j<cols;j++){
// }
// }
// if(deviceid == ""){
// }
double v0 = vct.first();
double vn = vct.last();
if(stages !=nullptr)
free(stages);
if(kstages != nullptr)
free(kstages);
if(bstages !=nullptr)
free(bstages);
stages = (double*)malloc(sizeof(double)*rows*cols*(stagecount+1));
kstages = (double*)malloc(sizeof(double)*rows*cols*(stagecount+1));
bstages = (double*)malloc(sizeof(double)*(stagecount+1));
memset(kstages,0,sizeof(double)*rows*cols);
memset(bstages,0,sizeof(double)*1);
int k = 1;
for(;k<stagecount;k++){
double vs = vct.at(k-1);
double ve = vct.at(k);
cv::Mat ms = datas[vs];
cv::Mat me = datas[ve];
vs = (vs-v0)/(vn-v0)*65535;
ve = (ve-v0)/(vn-v0)*65535;
double* ss = stages+(k-1)*rows*cols;
double* ks = kstages+k*rows*cols;
*(bstages+k) = vs;
/// y = kx+b
/// vs = k*x0+b
/// ve = k*x1+b
/// k = (ve-vs)/(x1-x0)
/// b = vs-k*x0
///
for(int i = 0;i<ms.rows;i++){
for(int j = 0;j< ms.cols;j++){
*(ss+i*ms.cols+j) = ms.at<float>(i,j);
*(ks+i*ms.cols+j) =(ve-vs)/(me.at<float>(i,j) - ms.at<float>(i,j));
}
}
}
double* ss = stages+(k-1)*rows*cols;
double* ks = kstages+k*rows*cols;
double* bs = bstages+k;
*(bs) = 65535;
// *(bs) = vct.last();
// cv::Mat ms = end.value();
qDebug()<<__FILE__<<__LINE__;
cv::Mat ms = datas[vct.last()];
qDebug()<<vct;
for(int i = 0;i<rows;i++){
qDebug()<<"i"<<i;
for(int j = 0;j<cols;j++){
*(ss+i*cols+j) = ms.at<float>(i,j);
*(ks+i*cols+j) = 0;
}
}
ready = true;
enable = true;
}
void MultiPoint::runData(cv::Mat img)
{
// qDebug()<<__FILE__<<__LINE__<<img.rows<<img.cols;
if(b_save)
meanmat(img);
if(ready && enable){
cv::Mat dst(img.rows,img.cols,img.type());
for(int i = 0; i< img.rows;i++){
for(int j = 0; j< img.cols;j++){
int k = 0;
for(; k<stagecount;k++)
{
if( img.at<ushort>(i,j) < stages[k*img.rows*img.cols+i*img.cols+j])
break;
}
if(k == 0)
dst.at<ushort>(i,j) = bstages[1];
else
dst.at<ushort>(i,j) = kstages[k*img.rows*img.cols+i*img.cols+j]
*(img.at<ushort>(i,j)-stages[(k-1)*img.rows*img.cols+i*img.cols+j])+ bstages[k];
}
}
if(!msk.empty() && b_rmbld){
for(int i = 1;i< rows-1;i++){
for(int j = 1;j<cols-1;j++){
if(msk.at<uchar>(i,j) == 1){
int n = 8- (msk.at<uchar>(i+1,j)+
msk.at<uchar>(i-1,j)+
msk.at<uchar>(i+1,j-1)+
msk.at<uchar>(i-1,j-1)+
msk.at<uchar>(i+1,j+1)+
msk.at<uchar>(i-1,j+1)+
msk.at<uchar>(i,j+1)+
msk.at<uchar>(i,j-1));
int sum = (1-msk.at<uchar>(i+1,j))*dst.at<ushort>(i+1,j)+
(1-msk.at<uchar>(i-1,j))*dst.at<ushort>(i-1,j)+
(1-msk.at<uchar>(i+1,j+1))*dst.at<ushort>(i+1,j+1)+
(1-msk.at<uchar>(i-1,j+1))*dst.at<ushort>(i-1,j+1)+
(1-msk.at<uchar>(i+1,j-1))*dst.at<ushort>(i+1,j-1)+
(1-msk.at<uchar>(i-1,j-1))*dst.at<ushort>(i-1,j-1)+
(1-msk.at<uchar>(i,j+1))*dst.at<ushort>(i,j+1)+
(1-msk.at<uchar>(i,j-1))*dst.at<ushort>(i,j-1);
if(n>0)
dst.at<ushort>(i,j) = sum/n;
else dst.at<ushort>(i,j) = 0;
}
}
}
}
emit signalsendimg(dst);
}
else{
emit signalsendimg(img);
}
}
void MultiPoint::meanmat(cv::Mat img)
{
// if(n_save == 32){
if(n_save == 32){
img.convertTo(msave,CV_32FC1,1.0/32.0);
}
else if(n_save>0){
for(int i = 0;i<img.rows;i++){
for(int j = 0;j<img.cols;j++){
msave.at<float>(i,j) += img.at<ushort>(i,j)/32.0;
}
}
}
else{
saveData(msave);
b_save = false;
}
n_save --;
}
void MultiPoint::save()
{
// n_save = 32;
n_save = 32;
b_save = true;
}
void MultiPoint::savedata(QString path, double ittime)
{
QDir d;
d.mkdir("datas/"+path);
deviceid = path;
QFile afs("datas/"+path+"/"+QString::number(ittime,'f',2)+".tgz");
afs.open(QIODevice::ReadWrite);
afs.write((char*)stages,(stagecount+1)*cols*rows*8);
afs.write((char*)kstages,(stagecount+1)*cols*rows*8);
afs.write((char*)bstages,(stagecount+1)*8);
afs.close();
}
void MultiPoint::loaddata()
{
QDir d;
if(!d.exists("datas/"+deviceid) || deviceid=="." || deviceid==".." || deviceid==""){
emit signalLog( u8"没有这个设备号的数据");
ready = false;
return;
}
QFile afs("datas/"+deviceid+"/"+QString::number(ittime,'f',2)+".tgz");
if(!afs.exists()){
emit signalLog( u8"没有这个积分时间的数据");
ready = false;
return;
}
emit signalLog(u8"设备号"+deviceid+u8",积分时间"+QString::number(ittime,'f',2)+u8"数据已加载");
afs.open(QIODevice::ReadWrite);
QByteArray data = afs.readAll();
stagecount = data.length()/((2*rows*cols+1)*8) - 1;
if(stages !=nullptr)
free(stages);
if(kstages != nullptr)
free(kstages);
if(bstages !=nullptr)
free(bstages);
stages = (double*)malloc(sizeof(double)*rows*cols*(stagecount+1));
kstages = (double*)malloc(sizeof(double)*rows*cols*(stagecount+1));
bstages = (double*)malloc(sizeof(double)*(stagecount+1));
memcpy(stages,data.data(),sizeof(double)*rows*cols*(stagecount+1));
memcpy(kstages,data.data()+sizeof(double)*rows*cols*(stagecount+1),sizeof(double)*rows*cols*(stagecount+1));
memcpy(bstages,data.data()+2*sizeof(double)*rows*cols*(stagecount+1),sizeof(double)*(stagecount+1));
msk = cv::Mat(rows,cols,CV_8UC1);
for(int i = 0;i<rows;i++){
for(int j = 0;j<cols;j++){
msk.at<uchar>(i,j) = 0;
for(int k = 0;k<stagecount;k++){
if(kstages[k*rows*cols+i*cols+j]<0 ||kstages[k*rows*cols+i*cols+j]>b_thre )
msk.at<uchar>(i,j) = 1;
}
}
}
// qDebug()<<"stagecount"<<stagecount;
ready = true;
}
void MultiPoint::off()
{
enable = false;
}
void MultiPoint::setparas(int _row, int _col)
{
rows = _row;
cols = _col;
}
void MultiPoint::setdevice(QString id)
{
deviceid = id;
if(enable)
loaddata();
}
void MultiPoint::setit(double it)
{
ittime = it;
if(enable)
loaddata();
}
void MultiPoint::setrmblk(bool b)
{
b_rmbld = b;
}
void MultiPoint::slotSetThre(double thre)
{
b_thre = thre;
if(ready){
msk = cv::Mat(rows,cols,CV_8UC1);
for(int i = 0;i<rows;i++){
for(int j = 0;j<cols;j++){
msk.at<uchar>(i,j) = 0;
for(int k = 0;k<stagecount;k++){
if(kstages[k*rows*cols+i*cols+j]<0 ||kstages[k*rows*cols+i*cols+j]>b_thre )
msk.at<uchar>(i,j) = 1;
}
}
}
QString log = u8"总共有"+QString::number(cv::sum(msk).val[0])+u8"个盲元点";
emit signalLog(log);
// qDebug()<<"stagecount"<<stagecount;
// ready = true;
}
}