编程语言之类型之间转换
生活随笔
收集整理的這篇文章主要介紹了
编程语言之类型之间转换
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1. QString轉換String
string s = qstr.toStdString();2.String轉換QString
QString qstr2 = QString::fromStdString(s);3.mat與qimage互轉
QImage cvMat2QImage(const cv::Mat& mat) {// 8-bits unsigned, NO. OF CHANNELS = 1if(mat.type() == CV_8UC1){QImage image(mat.cols, mat.rows, QImage::Format_Indexed8);// Set the color table (used to translate colour indexes to qRgb values)image.setNumColors(256);for(int i = 0; i < 256; i++){image.setColor(i, qRgb(i, i, i));}// Copy input Matuchar *pSrc = mat.data;for(int row = 0; row < mat.rows; row ++){uchar *pDest = image.scanLine(row);memcpy(pDest, pSrc, mat.cols);pSrc += mat.step;}return image;}// 8-bits unsigned, NO. OF CHANNELS = 3else if(mat.type() == CV_8UC3){// Copy input Matconst uchar *pSrc = (const uchar*)mat.data;// Create QImage with same dimensions as input MatQImage image(pSrc, mat.cols, mat.rows, mat.step, QImage::Format_RGB888);return image.rgbSwapped();}else if(mat.type() == CV_8UC4){// Copy input Matconst uchar *pSrc = (const uchar*)mat.data;// Create QImage with same dimensions as input MatQImage image(pSrc, mat.cols, mat.rows, mat.step, QImage::Format_ARGB32);return image.copy();}else{return QImage();} }4. QImage轉Mat
Mat QImage2cvMat(QImage image) {Mat mat;switch (image.format()){case QImage::Format_ARGB32:case QImage::Format_RGB32:case QImage::Format_ARGB32_Premultiplied:mat = Mat(image.height(), image.width(), CV_8UC4, (void*)image.constBits(), image.bytesPerLine());break;case QImage::Format_RGB888:mat = Mat(image.height(), image.width(), CV_8UC3, (void*)image.constBits(), image.bytesPerLine());cv::cvtColor(mat, mat, CV_BGR2RGB);break;case QImage::Format_Indexed8:mat = Mat(image.height(), image.width(), CV_8UC1, (void*)image.constBits(), image.bytesPerLine());break;}return mat; }5.QImage與QPixmap互轉
QPainter p(this);QPixmap pixmap;pixmap.load("../image/路飛.jpg");//QPixmap->QImageQImage tempImage = pixmap.toImage();p.drawImage(0,0,tempImage);QImage image;image.load("../image/路飛.jpg");//QImage->QPixmapQPixmap tempPixmap = QPixmap::fromImage(image);p.drawPixmap(450,0,tempPixmap);總結
以上是生活随笔為你收集整理的编程语言之类型之间转换的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: OpenCV学习笔记之改变图像的对比度和
- 下一篇: Qt学习笔记之样式表