-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
155 additions
and
195 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,149 +1,149 @@ | ||
#include "boundaries.h" | ||
#include <QDebug> | ||
|
||
|
||
|
||
Boundaries::Boundaries() | ||
{ | ||
|
||
} | ||
|
||
inline Mat Boundaries::QImageToCvMat( const QImage &inImage, bool inCloneImageData) | ||
{ | ||
switch ( inImage.format() ) | ||
{ | ||
// 8-bit, 4 channel | ||
case QImage::Format_ARGB32: | ||
case QImage::Format_ARGB32_Premultiplied: | ||
{ | ||
cv::Mat mat( inImage.height(), inImage.width(), | ||
CV_8UC4, | ||
const_cast<uchar*>(inImage.bits()), | ||
static_cast<size_t>(inImage.bytesPerLine()) | ||
); | ||
|
||
return (inCloneImageData ? mat.clone() : mat); | ||
} | ||
|
||
// 8-bit, 3 channel | ||
case QImage::Format_RGB32: | ||
case QImage::Format_RGB888: | ||
{ | ||
if ( !inCloneImageData ) | ||
{ | ||
qWarning() << "ASM::QImageToCvMat() - Conversion requires cloning because we use a temporary QImage"; | ||
} | ||
|
||
QImage swapped = inImage; | ||
|
||
if ( inImage.format() == QImage::Format_RGB32 ) | ||
{ | ||
swapped = swapped.convertToFormat( QImage::Format_RGB888 ); | ||
} | ||
|
||
swapped = swapped.rgbSwapped(); | ||
|
||
return cv::Mat( swapped.height(), swapped.width(), | ||
CV_8UC3, | ||
const_cast<uchar*>(swapped.bits()), | ||
static_cast<size_t>(swapped.bytesPerLine()) | ||
).clone(); | ||
} | ||
|
||
// 8-bit, 1 channel | ||
case QImage::Format_Indexed8: | ||
{ | ||
cv::Mat mat( inImage.height(), inImage.width(), | ||
CV_8UC1, | ||
const_cast<uchar*>(inImage.bits()), | ||
static_cast<size_t>(inImage.bytesPerLine()) | ||
); | ||
|
||
return (inCloneImageData ? mat.clone() : mat); | ||
} | ||
|
||
default: | ||
qWarning() << "ASM::QImageToCvMat() - QImage format not handled in switch:" << inImage.format(); | ||
break; | ||
} | ||
|
||
return cv::Mat(); | ||
} | ||
|
||
inline QImage Boundaries::cvMatToQImage( const cv::Mat &inMat ) | ||
{ | ||
switch ( inMat.type() ) | ||
{ | ||
// 8-bit, 4 channel | ||
case CV_8UC4: | ||
{ | ||
QImage image( inMat.data, | ||
inMat.cols, inMat.rows, | ||
static_cast<int>(inMat.step), | ||
QImage::Format_ARGB32 ); | ||
#include "boundaries.h" | ||
#include <QDebug> | ||
|
||
return image; | ||
} | ||
|
||
// 8-bit, 3 channel | ||
case CV_8UC3: | ||
{ | ||
QImage image( inMat.data, | ||
inMat.cols, inMat.rows, | ||
static_cast<int>(inMat.step), | ||
QImage::Format_RGB888 ); | ||
|
||
return image.rgbSwapped(); | ||
} | ||
Boundaries::Boundaries() | ||
{ | ||
|
||
// 8-bit, 1 channel | ||
case CV_8UC1: | ||
} | ||
|
||
inline Mat Boundaries::QImageToCvMat( const QImage &inImage, bool inCloneImageData) | ||
{ | ||
switch ( inImage.format() ) | ||
{ | ||
#if QT_VERSION >= QT_VERSION_CHECK(5, 5, 0) | ||
QImage image( inMat.data, | ||
inMat.cols, inMat.rows, | ||
static_cast<int>(inMat.step), | ||
QImage::Format_Grayscale8 ); | ||
#else | ||
static QVector<QRgb> sColorTable; | ||
|
||
// only create our color table the first time | ||
if ( sColorTable.isEmpty() ) | ||
// 8-bit, 4 channel | ||
case QImage::Format_ARGB32: | ||
case QImage::Format_ARGB32_Premultiplied: | ||
{ | ||
cv::Mat mat( inImage.height(), inImage.width(), | ||
CV_8UC4, | ||
const_cast<uchar*>(inImage.bits()), | ||
static_cast<size_t>(inImage.bytesPerLine()) | ||
); | ||
|
||
return (inCloneImageData ? mat.clone() : mat); | ||
} | ||
|
||
// 8-bit, 3 channel | ||
case QImage::Format_RGB32: | ||
case QImage::Format_RGB888: | ||
{ | ||
sColorTable.resize( 256 ); | ||
if ( !inCloneImageData ) | ||
{ | ||
qWarning() << "ASM::QImageToCvMat() - Conversion requires cloning because we use a temporary QImage"; | ||
} | ||
|
||
QImage swapped = inImage; | ||
|
||
for ( int i = 0; i < 256; ++i ) | ||
if ( inImage.format() == QImage::Format_RGB32 ) | ||
{ | ||
sColorTable[i] = qRgb( i, i, i ); | ||
swapped = swapped.convertToFormat( QImage::Format_RGB888 ); | ||
} | ||
|
||
swapped = swapped.rgbSwapped(); | ||
|
||
return cv::Mat( swapped.height(), swapped.width(), | ||
CV_8UC3, | ||
const_cast<uchar*>(swapped.bits()), | ||
static_cast<size_t>(swapped.bytesPerLine()) | ||
).clone(); | ||
} | ||
|
||
QImage image( inMat.data, | ||
inMat.cols, inMat.rows, | ||
static_cast<int>(inMat.step), | ||
QImage::Format_Indexed8 ); | ||
// 8-bit, 1 channel | ||
case QImage::Format_Indexed8: | ||
{ | ||
cv::Mat mat( inImage.height(), inImage.width(), | ||
CV_8UC1, | ||
const_cast<uchar*>(inImage.bits()), | ||
static_cast<size_t>(inImage.bytesPerLine()) | ||
); | ||
|
||
image.setColorTable( sColorTable ); | ||
#endif | ||
return (inCloneImageData ? mat.clone() : mat); | ||
} | ||
|
||
return image; | ||
default: | ||
qWarning() << "ASM::QImageToCvMat() - QImage format not handled in switch:" << inImage.format(); | ||
break; | ||
} | ||
|
||
default: | ||
qWarning() << "ASM::cvMatToQImage() - cv::Mat image type not handled in switch:" << inMat.type(); | ||
break; | ||
return cv::Mat(); | ||
} | ||
|
||
return QImage(); | ||
} | ||
|
||
QImage Boundaries::execute(int value){ | ||
inputMat = QImageToCvMat(inputImage, true); | ||
this->outputMap.create(inputMat.size(), inputMat.type()); | ||
cvtColor(inputMat, inputMatGrayScaled, CV_BGR2GRAY); | ||
blur(inputMatGrayScaled, detectedEdges, Size(3,3)); | ||
Canny(detectedEdges, detectedEdges, value, value*ratio, kernelSize); | ||
outputMap = Scalar::all(0); | ||
inputMat.copyTo(outputMap, detectedEdges); | ||
return cvMatToQImage(outputMap); | ||
} | ||
inline QImage Boundaries::cvMatToQImage( const cv::Mat &inMat ) | ||
{ | ||
switch ( inMat.type() ) | ||
{ | ||
// 8-bit, 4 channel | ||
case CV_8UC4: | ||
{ | ||
QImage image( inMat.data, | ||
inMat.cols, inMat.rows, | ||
static_cast<int>(inMat.step), | ||
QImage::Format_ARGB32 ); | ||
|
||
return image; | ||
} | ||
|
||
// 8-bit, 3 channel | ||
case CV_8UC3: | ||
{ | ||
QImage image( inMat.data, | ||
inMat.cols, inMat.rows, | ||
static_cast<int>(inMat.step), | ||
QImage::Format_RGB888 ); | ||
|
||
return image.rgbSwapped(); | ||
} | ||
|
||
// 8-bit, 1 channel | ||
case CV_8UC1: | ||
{ | ||
#if QT_VERSION >= QT_VERSION_CHECK(5, 5, 0) | ||
QImage image( inMat.data, | ||
inMat.cols, inMat.rows, | ||
static_cast<int>(inMat.step), | ||
QImage::Format_Grayscale8 ); | ||
#else | ||
static QVector<QRgb> sColorTable; | ||
|
||
// only create our color table the first time | ||
if ( sColorTable.isEmpty() ) | ||
{ | ||
sColorTable.resize( 256 ); | ||
|
||
for ( int i = 0; i < 256; ++i ) | ||
{ | ||
sColorTable[i] = qRgb( i, i, i ); | ||
} | ||
} | ||
|
||
QImage image( inMat.data, | ||
inMat.cols, inMat.rows, | ||
static_cast<int>(inMat.step), | ||
QImage::Format_Indexed8 ); | ||
|
||
image.setColorTable( sColorTable ); | ||
#endif | ||
|
||
return image; | ||
} | ||
|
||
default: | ||
qWarning() << "ASM::cvMatToQImage() - cv::Mat image type not handled in switch:" << inMat.type(); | ||
break; | ||
} | ||
|
||
return QImage(); | ||
} | ||
|
||
QImage Boundaries::execute(int value){ | ||
inputMat = QImageToCvMat(inputImage, true); | ||
this->outputMap.create(inputMat.size(), inputMat.type()); | ||
cvtColor(inputMat, inputMatGrayScaled, CV_BGR2GRAY); | ||
blur(inputMatGrayScaled, detectedEdges, Size(3,3)); | ||
Canny(detectedEdges, detectedEdges, value, value*ratio, kernelSize); | ||
outputMap = Scalar::all(0); | ||
inputMat.copyTo(outputMap, detectedEdges); | ||
return cvMatToQImage(outputMap); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters