Skip to content

Commit

Permalink
remove response to useOpenCv, slight performance improvement, add his…
Browse files Browse the repository at this point in the history
…togram support for IMAGE_ORIENTED using grayscale of the magnitude
  • Loading branch information
Lado Tonia committed Sep 3, 2015
1 parent 32cb3af commit 6ef4d23
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 122 deletions.
8 changes: 4 additions & 4 deletions IPL/include/processes/IPLGradientOperator.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,17 @@ class IPLSHARED_EXPORT IPLGradientOperator : public IPLClonableProcess<IPLGradie
void init();
virtual void destroy();

virtual bool processInputData (IPLImage* data, int inNr, bool useOpenCV);
virtual bool processInputData (IPLImage* data, int inNr, bool) override;
virtual IPLImage* getResultData (int outNr);

protected:
IPLOrientedImage* _result;

private:
bool fastGradient(IPLImage* data);
bool roberts(IPLImage* data, bool useOpenCV);
bool sobel(IPLImage* data, bool useOpenCV);
bool cubicSpline(IPLImage* data, bool useOpenCV);
bool roberts(IPLImage* data);
bool sobel(IPLImage* data);
bool cubicSpline(IPLImage* data);
};

#endif // IPLGRADIENTOPERATOR_H
223 changes: 108 additions & 115 deletions IPL/src/processes/IPLGradientOperator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ void IPLGradientOperator::init()
addInput("Image", IPLData::IMAGE_GRAYSCALE);
addOutput("Image", IPLData::IMAGE_ORIENTED);

// set the openCV support
setOpenCVSupport( IPLOpenCVSupport::OPENCV_ONLY );

// properties
addProcessPropertyInt("algorithm", "Algorithm:Fast Gradient|Roberts|Sobel|Cubic Spline", "", 0, IPL_WIDGET_RADIOBUTTONS);
}
Expand All @@ -48,26 +51,25 @@ void IPLGradientOperator::destroy()
delete _result;
}

bool IPLGradientOperator::processInputData(IPLImage* image , int, bool useOpenCV)
bool IPLGradientOperator::processInputData(IPLImage* image , int, bool)
{
// delete previous result
delete _result;
_result = new IPLOrientedImage(image->width(), image->height());

// get properties
int algorithm = getProcessPropertyInt("algorithm");
std::cout << "Gradient OP = " << algorithm << "\n";

switch (algorithm){
case 0:
default:
return fastGradient(image);
case 1:
return roberts(image, useOpenCV);
return roberts(image);
case 2:
return sobel(image, useOpenCV);
return sobel(image);
case 3:
return cubicSpline(image, useOpenCV);
return cubicSpline(image);
}

//make compiler happy...
Expand Down Expand Up @@ -107,7 +109,7 @@ bool IPLGradientOperator::fastGradient(IPLImage* image)
return true;
}

bool IPLGradientOperator::roberts(IPLImage* image, bool useOpenCV)
bool IPLGradientOperator::roberts(IPLImage* image)
{
static float rxf[2][2] = {{1.0,0},{0,-1.0}};
static cv::Mat rxKernel(2,2,CV_32FC1,rxf);
Expand All @@ -123,7 +125,6 @@ bool IPLGradientOperator::roberts(IPLImage* image, bool useOpenCV)

notifyProgressEventHandler(-1);

if ( useOpenCV ){
cv::Mat input;
cv::Mat gX;
cv::Mat gY;
Expand Down Expand Up @@ -154,128 +155,120 @@ bool IPLGradientOperator::roberts(IPLImage* image, bool useOpenCV)
}
}

}else{

}

return true;
}

bool IPLGradientOperator::sobel(IPLImage* image, bool useOpenCV)
bool IPLGradientOperator::sobel(IPLImage* image)
{
int width = image->width();
int height = image->height();

const int kSize = 3;

// fast gradient
int progress = 0;
int maxProgress = height*width;

notifyProgressEventHandler(-1);

if ( useOpenCV ){
cv::Mat input;
cv::Mat gX;
cv::Mat gY;
cvtColor(image->toCvMat(),input,CV_BGR2GRAY);

Sobel(input,gX,CV_32F,1,0,kSize);
Sobel(input,gY,CV_32F,0,1,kSize);

std::cout << gX.channels() << " channels\n";

for(int x=1; x<width; x++)
{
for(int y=1; y<height; y++)
{
// progress
notifyProgressEventHandler(100*progress++/maxProgress);
ipl_basetype gx = gX.at<cv::Vec<float,1>>(y,x).val[0] * FACTOR_TO_FLOAT ;
ipl_basetype gy = gY.at<cv::Vec<float,1>>(y,x).val[0] * FACTOR_TO_FLOAT ;

double phase = (gx!=0.0 || gy!=0.0 )? atan2( -gy, gx ) : 0.0;
int width = image->width();
int height = image->height();

while( phase > 2.0 * PI ) phase -= 2.0 * PI;
while( phase < 0.0 ) phase += 2.0 * PI;
const int kSize = 3;

phase /= 2.0 * PI;
// fast gradient
int progress = 0;
int maxProgress = height*width;

_result->phase(x,y) = phase;
_result->magnitude(x,y) = sqrt(gx*gx + gy*gy);
}
}
}else{
notifyProgressEventHandler(-1);

}
return true;
cv::Mat input;
cv::Mat gX;
cv::Mat gY;
cvtColor(image->toCvMat(),input,CV_BGR2GRAY);

Sobel(input,gX,CV_32F,1,0,kSize);
Sobel(input,gY,CV_32F,0,1,kSize);

for(int x=1; x<width; x++)
{
for(int y=1; y<height; y++)
{
// progress
notifyProgressEventHandler(100*progress++/maxProgress);
ipl_basetype gx = gX.at<cv::Vec<float,1>>(y,x).val[0] * FACTOR_TO_FLOAT ;
ipl_basetype gy = gY.at<cv::Vec<float,1>>(y,x).val[0] * FACTOR_TO_FLOAT ;

double phase = (gx!=0.0 || gy!=0.0 )? atan2( -gy, gx ) : 0.0;

while( phase > 2.0 * PI ) phase -= 2.0 * PI;
while( phase < 0.0 ) phase += 2.0 * PI;

phase /= 2.0 * PI;

_result->phase(x,y) = phase;
_result->magnitude(x,y) = sqrt(gx*gx + gy*gy);
}
}
return true;
}

bool IPLGradientOperator::cubicSpline(IPLImage* image, bool useOpenCV)
bool IPLGradientOperator::cubicSpline(IPLImage* image)
{
static float k1f[4][4] = {{-0.5,1.5,-1.5,0.5},{1.0,-2.5,2.0,-0.5},{-0.5,0,0.5,0},{0,1.0,0,0}};
static cv::Mat k1(4,4,CV_32FC1,k1f);
static float k2f[4][4] = {{-0.5,1.0,-0.5,0},{1.5,-2.5,0,1},{-1.5,2.0,0.5,0},{0.5,-0.5,0,0}};
static cv::Mat k2(4,4,CV_32FC1,k2f);

int width = image->width();
int height = image->height();
int progress = 0;
int maxProgress = height*width;
static float k1f[4][4] = {{-0.5,1.5,-1.5,0.5},{1.0,-2.5,2.0,-0.5},{-0.5,0,0.5,0},{0,1.0,0,0}};
static cv::Mat k1(4,4,CV_32FC1,k1f);
static float k2f[4][4] = {{-0.5,1.0,-0.5,0},{1.5,-2.5,0,1},{-1.5,2.0,0.5,0},{0.5,-0.5,0,0}};
static cv::Mat k2(4,4,CV_32FC1,k2f);

notifyProgressEventHandler(-1);

if ( useOpenCV ){
cv::Mat input = image->toCvMat();
cv::Mat coeff = cv::Mat::zeros(4,4,CV_32FC1);

float dx,dy,x3,x2,y3,y2,xf,yf;

for (int x=1; x < width - 2; x++){
for (int y = 1; y < height - 2; y++){
// progress
notifyProgressEventHandler(100*progress++/maxProgress);

for (int i=0;i<4;i++)
for (int j=0;j<4;j++)
coeff.at<cv::Vec<float,1>>(i,j)[0] = static_cast<float>(input.at<cv::Vec<unsigned char,4>>(y-1+i,x-1+j).val[0]) * FACTOR_TO_FLOAT ;

coeff = k1*coeff;
coeff = coeff*k2;
xf = static_cast<float>(x)/static_cast<float>(width);
x2 = xf*xf;
x3 = x2*xf;
yf = static_cast<float>(y)/static_cast<float>(height);
y2 = yf*yf;
y3 = y2*yf;
float dyMatf[4][4] = {{3.f*y2*x3,3.f*y2*x2,3.f*y2*xf,3.f*y2},
{2.f*yf*x3,2.f*yf*x2,2.f*yf*xf,2.f*yf},
{x3,x2,xf,1.f},
{0.f,0.f,0.f,0.f}};
float dxMatf[4][4] = {{3.f*x2*y3,2.f*xf*y3,y3,0.f},
{3.f*x2*y2,2.f*xf*y2,y2,0.f},
{3.f*x2*yf,2.f*xf*yf,yf,0.f},
{3.f*x2,2.f*xf,1.f,0.f}};
cv::Mat dxMat(4,4,CV_32FC1,dxMatf);
cv::Mat dyMat(4,4,CV_32FC1,dyMatf);
dx = coeff.dot(dxMat);
dy = coeff.dot(dyMat);

double phase = (dx!=0.0 || dy!=0.0 )? atan2( -dy, dx ) : 0.0;

while( phase > 2.0 * PI ) phase -= 2.0 * PI;
while( phase < 0.0 ) phase += 2.0 * PI;

// phase 0.0-1.0
phase /= 2 * PI;
int width = image->width();
int height = image->height();
int progress = 0;
int maxProgress = height*width;

notifyProgressEventHandler(-1);

_result->phase(x,y) = phase;
_result->magnitude(x,y) = sqrt(dx*dx + dy*dy);
}
}
}else{
cv::Mat input = image->toCvMat();
cv::Mat coeffFull = cv::Mat::zeros(height,width,CV_32FC1);
cv::Mat coeff = cv::Mat::zeros(height,width,CV_32FC1);

for (int i=0;i<width;i++)
for (int j=0;j<height;j++)
coeffFull.at<cv::Vec<float,1>>(i,j)[0] = static_cast<float>(input.at<cv::Vec<unsigned char,4>>(i,j).val[0]) * FACTOR_TO_FLOAT ;

float dx,dy,x3,x2,y3,y2,xf,yf;

for (int x=1; x < width - 2; x++){
for (int y = 1; y < height - 2; y++){
// progress
notifyProgressEventHandler(100*progress++/maxProgress);

coeffFull(cv::Range(y-1,y+3),cv::Range(x-1,x+3)).copyTo(coeff);

//std::cout << coeff << std::endl;

coeff = k1*coeff;
coeff = coeff*k2;
xf = static_cast<float>(x)/static_cast<float>(width);
x2 = xf*xf;
x3 = x2*xf;
yf = static_cast<float>(y)/static_cast<float>(height);
y2 = yf*yf;
y3 = y2*yf;
float dyMatf[4][4] = {{3.f*y2*x3,3.f*y2*x2,3.f*y2*xf,3.f*y2},
{2.f*yf*x3,2.f*yf*x2,2.f*yf*xf,2.f*yf},
{x3,x2,xf,1.f},
{0.f,0.f,0.f,0.f}};
float dxMatf[4][4] = {{3.f*x2*y3,2.f*xf*y3,y3,0.f},
{3.f*x2*y2,2.f*xf*y2,y2,0.f},
{3.f*x2*yf,2.f*xf*yf,yf,0.f},
{3.f*x2,2.f*xf,1.f,0.f}};
cv::Mat dxMat(4,4,CV_32FC1,dxMatf);
cv::Mat dyMat(4,4,CV_32FC1,dyMatf);
dx = coeff.dot(dxMat);
dy = coeff.dot(dyMat);

double phase = (dx!=0.0 || dy!=0.0 )? atan2( -dy, dx ) : 0.0;

while( phase > 2.0 * PI ) phase -= 2.0 * PI;
while( phase < 0.0 ) phase += 2.0 * PI;

// phase 0.0-1.0
phase /= 2 * PI;

_result->phase(x,y) = phase;
_result->magnitude(x,y) = sqrt(dx*dx + dy*dy);
}
}

return true;
}

Expand Down
6 changes: 3 additions & 3 deletions ImagePlay/src/IPHistogramWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ void IPHistogramWidget::updateHistogram(IPLImage *image)
{
_histogram.reset(new IPLHistogram(image->plane(0), 2, 100));
}
else if(_type == IPLData::IMAGE_GRAYSCALE)
else if(_type == IPLData::IMAGE_GRAYSCALE || _type == IPLData::IMAGE_ORIENTED )
{
_histogram.reset(new IPLHistogram(image->plane(0), 256, 100));
}
Expand Down Expand Up @@ -141,7 +141,7 @@ void IPHistogramWidget::paintEvent(QPaintEvent* e)
painter.setCompositionMode(QPainter::CompositionMode_SourceOver);
}
}
else if(_type == IPLData::IMAGE_GRAYSCALE)
else if(_type == IPLData::IMAGE_GRAYSCALE || _type == IPLData::IMAGE_ORIENTED )
{
std::vector<int> values = _logarithmic ? _histogram->logarithmic() : _histogram->linear();

Expand Down Expand Up @@ -198,7 +198,7 @@ void IPHistogramWidget::mouseMoveEvent(QMouseEvent* event)

emit highlightChangedColor(_hightlightPosition, valueR, valueG, valueB, percentageR, percentageG, percentageB);
}
else if(_type == IPLData::IMAGE_GRAYSCALE)
else if(_type == IPLData::IMAGE_GRAYSCALE || _type == IPLData::IMAGE_ORIENTED )
{
int value = _histogram->rawValueAt(_hightlightPosition);
float percentage = _histogram->percentageAt(_hightlightPosition);
Expand Down

0 comments on commit 6ef4d23

Please sign in to comment.