void mvCameraWidgets::paintEvent(QPaintEvent * event)
{
//ui.colormap为一个QLabel
//设置比色卡
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing);//反锯齿
int x = ui.colormap->x();
int y = ui.colormap->y();
int w = ui.colormap->rect().width();
int h = ui.colormap->rect().height();
QLinearGradient linearGradient(0, y, 0, y+h);//渐变区域
linearGradient.setColorAt(0, Qt::red);
linearGradient.setColorAt(0.5, Qt::blue);
linearGradient.setColorAt(1, Qt::green);
painter.setBrush(linearGradient);//设置画刷,则painter.drawRect(rect());绘制出渐变背景
painter.drawRect(QRect(x,y,w,h));
}
QImage intChangetoQcolorimg(int img_width, int img_height, QVector<double>& magnetic_strength, double down, double up, QImage & image0)
{
int w_pic = img_width;
int h_pic = img_height;
QVector<QRgb> rgbColourTable;
unsigned char *Buffer; //磁场强度信息值,根据在上下限up,down中比例转换到0-255
Buffer = (unsigned char *)malloc(w_pic*h_pic);
QImage img = QImage(Buffer, w_pic, h_pic, QImage::Format_Indexed8);
if (w_pic*h_pic!=magnetic_strength.size())
{
return img;
}
if (down >= up)
{
return img;
}
for (int i = 0; i < w_pic*h_pic; i++)
{
if (magnetic_strength[i] <= down)
{
Buffer[i] = (uchar)0;
}
if (magnetic_strength[i] >= up)
{
Buffer[i] = (uchar)255;
}
if ((magnetic_strength[i] < up) && (magnetic_strength[i] > down))
{
int tmp = (magnetic_strength[i] - down) / (up - down) * 255;
uchar utmp = (uchar)tmp;
Buffer[i] = utmp;
}
}
QImage copyImage = img.copy(); // 复制整个图像
for (int y = 0; y < 256; y++)
{
QColor color;
int index = (int)((float)(y) / (float)(255) * (float)(image0.height() - 2));
if (index == 0)
{
color = image0.pixelColor(QPoint(1, image0.height() - 2));
}
else if (index == image0.height() - 2)
{
color = image0.pixelColor(QPoint(1, 1));
}
else
{
color = image0.pixelColor(QPoint(1, image0.height() - 2 - index));
}
QRgb rgb = color.rgb();
rgbColourTable.push_back(rgb);
}
copyImage.setColorTable(rgbColourTable);
if (Buffer != NULL)
{
free(Buffer);
Buffer = NULL;
}
return copyImage;
}
void mvCameraWidgets::on_pushButton_colormap_clicked()
{
//构造一组数据
QRect roi;
int w_pic = 200;
int h_pic =2000;
QVector<double> magnetic_strength;
for (int y = 0; y < h_pic; y++)
{
for (int x = 0; x < w_pic; x++)
{
double d = (double)(x/5.0) ;
magnetic_strength.push_back(d);
}
}
//设置渐变图在重写的paintEvent函数里
QPixmap pix = grab(QRect(ui.colormap->x(), ui.colormap->y(), ui.colormap->width(), ui.colormap->height()));
QImage image0 = pix.toImage();
QImage img =intChangetoQcolorimg(w_pic, h_pic, magnetic_strength, down, up, image0);
img.save("img/image_mapping.png");
}
```![最终的伪彩图](https://ucc.alicdn.com/images/user-upload-01/de12992037ec4d319d590298a8b72666.png#pic_center)
![构建的灰度图](https://ucc.alicdn.com/images/user-upload-01/76022e4d17fb4b068e5c77d90c1ae3bf.png#pic_center)
![比色卡](https://ucc.alicdn.com/images/user-upload-01/6a1b2d7adf244267a04f8507a1aae412.png#pic_center)