You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
165 lines
4.1 KiB
165 lines
4.1 KiB
9 years ago
|
////////////////////////////////////////////////////////////////////////////////
|
||
|
/**
|
||
|
* \file opencvprocessor.cpp
|
||
|
* \version v1.0
|
||
|
* \author Ing. Dominik Malcik
|
||
|
*/
|
||
|
////////////////////////////////////////////////////////////////////////////////
|
||
|
|
||
|
#include "opencvprocessor.h"
|
||
|
|
||
|
#include "string.h"
|
||
|
#include <string>
|
||
|
#include <iostream>
|
||
|
|
||
|
OpenCVprocessor::OpenCVprocessor(QWidget *parent) :
|
||
|
QMainWindow(parent)
|
||
|
{
|
||
|
}
|
||
|
|
||
|
IplImage * OpenCVprocessor::qImageToCvImage(QImage *qimg) {
|
||
|
|
||
|
IplImage *imgHeader = cvCreateImageHeader( cvSize(qimg->width(), qimg->height()), IPL_DEPTH_8U, 4);
|
||
|
imgHeader->imageData = (char*) qimg->bits();
|
||
|
|
||
|
uchar* newdata = (uchar*) malloc(sizeof(uchar) * qimg->byteCount());
|
||
|
memcpy(newdata, qimg->bits(), qimg->byteCount());
|
||
|
imgHeader->imageData = (char*) newdata;
|
||
|
|
||
|
return imgHeader;
|
||
|
}
|
||
|
|
||
|
QImage OpenCVprocessor::cvImageToQImage(const IplImage *iplImage) {
|
||
|
int height = iplImage->height;
|
||
|
int width = iplImage->width;
|
||
|
|
||
|
if (iplImage->depth == IPL_DEPTH_8U && iplImage->nChannels == 3) {
|
||
|
const uchar *qImageBuffer = (const uchar*)iplImage->imageData;
|
||
|
|
||
|
QImage img(qImageBuffer, width, height, QImage::Format_RGB888);
|
||
|
return img.rgbSwapped();
|
||
|
|
||
|
} else if (iplImage->depth == IPL_DEPTH_8U && iplImage->nChannels == 1){
|
||
|
|
||
|
const uchar *qImageBuffer = (const uchar*)iplImage->imageData;
|
||
|
QImage img(qImageBuffer, width, height, QImage::Format_Indexed8);
|
||
|
|
||
|
QVector<QRgb> colorTable;
|
||
|
for (int i = 0; i < 256; i++){
|
||
|
colorTable.push_back(qRgb(i, i, i));
|
||
|
}
|
||
|
|
||
|
img.setColorTable(colorTable);
|
||
|
return img;
|
||
|
|
||
|
}else{
|
||
|
return QImage();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
QImage OpenCVprocessor::processThreshold_Edges(QImage * input, bool processEdges, int threshValue) {
|
||
|
|
||
|
IplImage * img = NULL;
|
||
|
img = qImageToCvImage(input);
|
||
|
|
||
|
// transform source image to greyscale
|
||
|
IplImage *dst = cvCreateImage( cvSize( img->width, img->height ), IPL_DEPTH_8U, 1 );
|
||
|
cvCvtColor( img, dst, CV_RGB2GRAY );
|
||
|
|
||
|
// make threshold
|
||
|
cvThreshold(dst, dst, threshValue, 255, CV_THRESH_BINARY);
|
||
|
|
||
|
if (processEdges) {
|
||
|
cvCanny( dst, dst, 1.0, 1.0, 3);
|
||
|
}
|
||
|
|
||
|
QImage qEdges = cvImageToQImage(dst);
|
||
|
cvReleaseImage(&dst);
|
||
|
cvReleaseImage(&img);
|
||
|
return qEdges;
|
||
|
}
|
||
|
|
||
|
|
||
|
QImage OpenCVprocessor::normalizeImage(QImage * input, int red, int green, int blue, int rThrs, int gThrs, int bThrs) {
|
||
|
|
||
|
// load image
|
||
|
IplImage * img = NULL;
|
||
|
img = qImageToCvImage(input);
|
||
|
|
||
|
// prepare particular channels
|
||
|
IplImage* imgRed = cvCreateImage(cvGetSize(img), 8, 1);
|
||
|
IplImage* imgGreen = cvCreateImage(cvGetSize(img), 8, 1);
|
||
|
IplImage* imgBlue = cvCreateImage(cvGetSize(img), 8, 1);
|
||
|
|
||
|
// split channels
|
||
|
cvSplit(img, imgBlue, imgGreen, imgRed, NULL);
|
||
|
|
||
|
// change to bitmask!
|
||
|
int lowRed = red-rThrs;
|
||
|
if (lowRed < 0) {
|
||
|
lowRed = 0;
|
||
|
}
|
||
|
if (lowRed > 255) {
|
||
|
lowRed = 255;
|
||
|
}
|
||
|
|
||
|
int highRed = red+rThrs;
|
||
|
if (highRed < 0) {
|
||
|
highRed = 0;
|
||
|
}
|
||
|
if (highRed > 255) {
|
||
|
highRed = 255;
|
||
|
}
|
||
|
|
||
|
int lowGreen = green-gThrs;
|
||
|
if (lowGreen < 0) {
|
||
|
lowGreen = 0;
|
||
|
}
|
||
|
if (lowGreen > 255) {
|
||
|
lowGreen = 255;
|
||
|
}
|
||
|
|
||
|
int highGreen = green+gThrs;
|
||
|
if (highGreen < 0) {
|
||
|
highGreen = 0;
|
||
|
}
|
||
|
if (highGreen > 255) {
|
||
|
highGreen = 255;
|
||
|
}
|
||
|
|
||
|
int lowBlue = blue-bThrs;
|
||
|
if (lowBlue < 0) {
|
||
|
lowBlue = 0;
|
||
|
}
|
||
|
if (lowBlue > 255) {
|
||
|
lowBlue = 255;
|
||
|
}
|
||
|
|
||
|
int highBlue = blue+bThrs;
|
||
|
if (highBlue < 0) {
|
||
|
highBlue = 0;
|
||
|
}
|
||
|
if (highBlue > 255) {
|
||
|
highBlue = 255;
|
||
|
}
|
||
|
|
||
|
// mask each channle
|
||
|
cvInRangeS(imgRed, cvScalar(lowRed), cvScalar(highRed), imgRed);
|
||
|
cvInRangeS(imgGreen, cvScalar(lowGreen), cvScalar(highGreen), imgGreen);
|
||
|
cvInRangeS(imgBlue, cvScalar(lowBlue), cvScalar(highBlue), imgBlue);
|
||
|
|
||
|
cvMul(imgRed, imgGreen, imgRed);
|
||
|
cvMul(imgRed, imgBlue, imgRed);
|
||
|
|
||
|
// transform result to QImage format
|
||
|
QImage qOutput = cvImageToQImage(imgRed);
|
||
|
|
||
|
// release memory
|
||
|
cvReleaseImage(&img);
|
||
|
cvReleaseImage(&imgRed);
|
||
|
cvReleaseImage(&imgGreen);
|
||
|
cvReleaseImage(&imgBlue);
|
||
|
return qOutput;
|
||
|
}
|
||
|
|