QImage转cv::Mat:
cv::Mat TempCVImage;
m_srcQImage = img.copy();
qDebug() << "Image Format:" << m_srcQImage.format();
std::cout << "image format--:" << std::endl;
if (m_srcQImage.format() == QImage::Format_Indexed8) { //黑白图
qDebug() << "Image Format:" << "QImage::Format_Indexed8";
TempCVImage = cv::Mat(m_srcQImage.height(), m_srcQImage.width(), CV_8UC1, (void*)m_srcQImage.constBits(), m_srcQImage.bytesPerLine());
} else if (m_srcQImage.format() == QImage::Format_Grayscale8) { //黑白图
qDebug() << "Image Format:" << "QImage::Format_Grayscale8";
TempCVImage = cv::Mat(m_srcQImage.height(), m_srcQImage.width(), CV_8UC1, (void*)m_srcQImage.constBits(), m_srcQImage.bytesPerLine());
} else if (m_srcQImage.format() == QImage::Format_RGB888) { //彩图
qDebug() << "Image Format:" << "QImage::Format_RGB888";
TempCVImage = cv::Mat(m_srcQImage.height(), m_srcQImage.width(), CV_8UC3, (void*)m_srcQImage.constBits(), m_srcQImage.bytesPerLine());
cv::cvtColor(TempCVImage, TempCVImage, CV_RGB2BGR);
} else if (m_srcQImage.format() == QImage::Format_RGB32) { //彩图
qDebug() << "Image Format:" << "QImage::Format_RGB32";
m_srcQImage = m_srcQImage.convertToFormat( QImage::Format_RGBA8888 );
TempCVImage = cv::Mat(m_srcQImage.height(), m_srcQImage.width(), CV_8UC4, (void*)m_srcQImage.constBits(), m_srcQImage.bytesPerLine());
cv::cvtColor(TempCVImage, TempCVImage, CV_RGBA2BGR);
} else if (m_srcQImage.format() == QImage::Format_ARGB32) { //彩图
qDebug() << "Image Format:" << "QImage::Format_ARGB32";
m_srcQImage = m_srcQImage.convertToFormat( QImage::Format_RGBA8888 );
TempCVImage = cv::Mat(m_srcQImage.height(), m_srcQImage.width(), CV_8UC4, (void*)m_srcQImage.constBits(), m_srcQImage.bytesPerLine());
cv::cvtColor(TempCVImage, TempCVImage, CV_RGBA2BGR);
}
QImage转cv::Mat const uchar pSrc = (const uchar)m_dstCVImage.data; QImage dst_img; if (m_dstCVImage.type() == CV_8UC1) { //黑白图 dst_img = QImage(pSrc, m_dstCVImage.cols, m_dstCVImage.rows, m_dstCVImage.cols, QImage::Format_Indexed8).copy(); dst_img.setColorTable(m_sColorTable); } else if (m_dstCVImage.type() == CV_8UC3) { //BGR图 dst_img = QImage(pSrc, m_dstCVImage.cols, m_dstCVImage.rows, m_dstCVImage.step, QImage::Format_RGB888).rgbSwapped(); }
|