#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 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;i0.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