195 lines
4.7 KiB
C++
195 lines
4.7 KiB
C++
#include "cloudage.h"
|
||
|
||
|
||
Cloudage::Cloudage(int radius)
|
||
{
|
||
m_iRadius = radius;
|
||
}
|
||
|
||
void Cloudage::setRadius(int radius)
|
||
{
|
||
m_iRadius = radius;
|
||
}
|
||
|
||
QImage Cloudage::readImage(QString imagePath)
|
||
{
|
||
QImage qImage;
|
||
qImage.load(imagePath);
|
||
|
||
m_qImgPath = imagePath;
|
||
|
||
return qImage;
|
||
}
|
||
|
||
QImage Cloudage::createGrayscaleImage(int width, int height)
|
||
{
|
||
QImage grayscaleImage(width, height, QImage::Format_Indexed8);//提问:如果使用QImage::Format_Grayscale8怎么实现?
|
||
QVector<QRgb> grayTable; //设置颜色表
|
||
for (int i = 0; i < 256; i++)
|
||
grayTable.push_back(qRgb(i, i, i));
|
||
grayscaleImage.setColorTable(grayTable);
|
||
|
||
return grayscaleImage;
|
||
}
|
||
|
||
void Cloudage::getPosOfCenterpixel(int width, int height, int &widthCenter,int &heightCenter)
|
||
{
|
||
widthCenter = width/2;
|
||
heightCenter = height/2;
|
||
}
|
||
|
||
bool Cloudage::isValidPos(int x1, int y1, int x2, int y2)
|
||
{
|
||
float tmp =pow((x1 - x2), 2) + pow((y1 - y2), 2);
|
||
float distance = sqrt(tmp);
|
||
|
||
if(distance > m_iRadius)
|
||
return false;
|
||
else
|
||
return true;
|
||
}
|
||
|
||
float Cloudage::getcloudage1(QImage image, bool saveBinaryImg)
|
||
{
|
||
int width=image.width();
|
||
int height=image.height();
|
||
|
||
int widthCenter;
|
||
int heightCenter;
|
||
getPosOfCenterpixel(width,height,widthCenter,heightCenter);
|
||
|
||
QImage cloudQImage = createGrayscaleImage(width, height);
|
||
|
||
|
||
// int cloudInt[width][height] = {0};//错误:不能用变量在栈上定义数组 → 只能定义在堆上!
|
||
|
||
//申请空间
|
||
int** cloudInt = new int*[width];
|
||
for(int i=0;i<width;i++)
|
||
cloudInt[i] = new int[height];
|
||
|
||
//云点识别
|
||
float cloudPixelCounter = 0;
|
||
float totalPixelCountor = 0;
|
||
for(int i=0;i<width;i++)
|
||
{
|
||
for(int j=0;j<height;j++)
|
||
{
|
||
if(!isValidPos(i,j,widthCenter,heightCenter))//判断是否在有效半径内,如果不在就不统计
|
||
{
|
||
cloudQImage.setPixel(i,j,0);
|
||
continue;
|
||
}
|
||
|
||
totalPixelCountor++;
|
||
|
||
QColor pixelTmp = image.pixelColor(i,j);
|
||
|
||
float red = (float)pixelTmp.red();
|
||
float blue = (float)pixelTmp.blue();
|
||
float rate = red/blue;
|
||
|
||
if(rate>0.6)
|
||
{
|
||
cloudQImage.setPixel(i,j,255);
|
||
cloudInt[i][j] = 1;
|
||
cloudPixelCounter++;
|
||
}
|
||
else
|
||
{
|
||
cloudQImage.setPixel(i,j,150);
|
||
}
|
||
}
|
||
}
|
||
|
||
//计算云量
|
||
float cloudage = cloudPixelCounter / totalPixelCountor;
|
||
|
||
if(saveBinaryImg)
|
||
{
|
||
QStringList imgPathTMP = m_qImgPath.split(".");
|
||
QString name = imgPathTMP[0] + "_BinaryImg_ratio1." + imgPathTMP[1];
|
||
cloudQImage.save(name);
|
||
}
|
||
|
||
|
||
//释放空间
|
||
for(int i=0;i<width;i++)
|
||
delete []cloudInt[i];
|
||
delete []cloudInt;
|
||
|
||
return cloudage;
|
||
}
|
||
|
||
float Cloudage::getcloudage2(QImage image, bool saveBinaryImg)
|
||
{
|
||
int width=image.width();
|
||
int height=image.height();
|
||
|
||
int widthCenter;
|
||
int heightCenter;
|
||
getPosOfCenterpixel(width,height,widthCenter,heightCenter);
|
||
|
||
QImage cloudQImage = createGrayscaleImage(width, height);
|
||
|
||
|
||
// int cloudInt[width][height] = {0};//错误:不能用变量在栈上定义数组 → 只能定义在堆上!
|
||
|
||
//申请空间
|
||
int** cloudInt = new int*[width];
|
||
for(int i=0;i<width;i++)
|
||
cloudInt[i] = new int[height];
|
||
|
||
//云点识别
|
||
float cloudPixelCounter = 0;
|
||
float totalPixelCountor = 0;
|
||
for(int i=0;i<width;i++)
|
||
{
|
||
for(int j=0;j<height;j++)
|
||
{
|
||
if(!isValidPos(i,j,widthCenter,heightCenter))//判断是否在有效半径内,如果不在就不统计
|
||
{
|
||
cloudQImage.setPixel(i,j,0);
|
||
continue;
|
||
}
|
||
|
||
totalPixelCountor++;
|
||
|
||
QColor pixelTmp = image.pixelColor(i,j);
|
||
|
||
float red = (float)pixelTmp.red();
|
||
float blue = (float)pixelTmp.blue();
|
||
float rate = (blue - red) / (blue + red);
|
||
|
||
if(rate<0.2)
|
||
{
|
||
cloudQImage.setPixel(i,j,255);
|
||
cloudInt[i][j] = 1;
|
||
cloudPixelCounter++;
|
||
}
|
||
else
|
||
{
|
||
cloudQImage.setPixel(i,j,150);
|
||
}
|
||
}
|
||
}
|
||
|
||
//计算云量
|
||
float cloudage = cloudPixelCounter / totalPixelCountor;
|
||
|
||
if(saveBinaryImg)
|
||
{
|
||
QStringList imgPathTMP = m_qImgPath.split(".");
|
||
QString name = imgPathTMP[0] + "_BinaryImg_ratio2." + imgPathTMP[1];
|
||
cloudQImage.save(name);
|
||
}
|
||
|
||
|
||
//释放空间
|
||
for(int i=0;i<width;i++)
|
||
delete []cloudInt[i];
|
||
delete []cloudInt;
|
||
|
||
return cloudage;
|
||
}
|