Skip to content

Commit

Permalink
Merge pull request #1 from Konstantysz/gui-dev
Browse files Browse the repository at this point in the history
Gui dev
  • Loading branch information
Konstantysz authored Nov 24, 2021
2 parents 773c0b0 + dce11c6 commit 1505639
Show file tree
Hide file tree
Showing 14 changed files with 628 additions and 45 deletions.
55 changes: 50 additions & 5 deletions ImageAnalysis/ImageAnalysisUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,27 @@ namespace ImageAnalysis
{
namespace utils
{
std::array<int, 256> Histogram(const cv::Mat& input)
{
if (input.type() != CV_8UC1)
{
// TODO: Conversion to proper type
return std::array<int, 256>();
}

auto histogram = std::array<int, 256>();

for (int i = 0; i < input.rows; i++)
{
for (int j = 0; j < input.cols; j++)
{
histogram[(int)input.at<uchar>(i, j)]++;
}
}

return histogram;
}

void SingleConvolve(const cv::Mat& input, cv::Mat& output, const cv::Mat& kernel, const int& i, const int& j)
{
cv::Mat roi;
Expand Down Expand Up @@ -42,7 +63,7 @@ namespace ImageAnalysis

}

cv::Mat Convolve(const cv::Mat& input, const cv::Mat& kernel)
cv::Mat ConvolveZeroPad(const cv::Mat& input, const cv::Mat& kernel)
{
int halfKernelSize = (kernel.cols - 1) / 2;

Expand All @@ -63,15 +84,32 @@ namespace ImageAnalysis
return convolutionResult(cv::Rect(halfKernelSize, halfKernelSize, input.cols, input.rows));
}

cv::Mat FilterKernelGenerator(int size)
cv::Mat Convolve(const cv::Mat& input, const cv::Mat& kernel)
{
int halfKernelSize = (kernel.cols - 1) / 2;

cv::Mat convolutionResult;
input.convertTo(convolutionResult, input.type());

for (int i = halfKernelSize; i < input.cols - halfKernelSize; i++)
{
for (int j = halfKernelSize; j < input.rows - halfKernelSize; j++)
{
ImageAnalysis::utils::SingleConvolve(input, convolutionResult, kernel, i, j);
}
}

return convolutionResult;
}

cv::Mat GaussianKernelGenerator(int size, double sigma)
{
if (size % 2 == 0)
{
throw "Kurwa daj nieparzyst¹";
}

// initialising standard deviation to 1.0
double sigma = 1.0;
double s = 2.0 * sigma * sigma;

// sum is for normalization
Expand All @@ -92,7 +130,7 @@ namespace ImageAnalysis
return kernel /= sum;
}

std::pair<cv::Mat, cv::Mat> Gradient(cv::Mat input)
std::pair<cv::Mat, cv::Mat> Gradient(const cv::Mat& input)
{
cv::Mat input32F;
input.convertTo(input32F, CV_32F);
Expand Down Expand Up @@ -139,13 +177,20 @@ namespace ImageAnalysis
}
else if (angle >= 7 * CV_PI / 8 || angle < CV_PI / 8)
{
theta.at<float>(j, i) = 0;
theta.at<float>(j, i) = 255;
}
}
}

// Scaling gradient to values [0-255]
double gMin, gMax;
cv::minMaxLoc(G, &gMin, &gMax);
G /= gMax;
G *= 255;

cv::Mat G8u;
G.convertTo(G8u, CV_8U);

cv::Mat theta8u;
theta.convertTo(theta8u, CV_8U);

Expand Down
36 changes: 34 additions & 2 deletions ImageAnalysis/ImageAnalysisUtils.h
Original file line number Diff line number Diff line change
@@ -1,21 +1,53 @@
#pragma once
#include <chrono>
#include <windows.h>

namespace ImageAnalysis
{
namespace utils
{
class Timer
{
public:
Timer(std::string name)
{
functionName = name;
tic = std::chrono::steady_clock::now();
}

~Timer()
{
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
auto toc = std::chrono::steady_clock::now();
auto time = std::chrono::duration_cast<std::chrono::milliseconds>(toc - tic).count();

SetConsoleTextAttribute(hConsole, 11);
std::cout << "[" + functionName + "] ";
SetConsoleTextAttribute(hConsole, 15);
std::cout << "Time elapsed: " << time << "[ms]" << std::endl;
}
private:
std::string functionName;
std::chrono::steady_clock::time_point tic;
};

std::array<int, 256> Histogram(const cv::Mat& input);

void SingleConvolve(const cv::Mat& input, cv::Mat& output, const cv::Mat& kernel, const int& i, const int& j);

cv::Mat ConvolveZeroPad(const cv::Mat& input, const cv::Mat& kernel);

cv::Mat Convolve(const cv::Mat& input, const cv::Mat& kernel);

cv::Mat FilterKernelGenerator(int size);
cv::Mat GaussianKernelGenerator(int size, double sigma);

std::pair<cv::Mat, cv::Mat> Gradient(cv::Mat input);
std::pair<cv::Mat, cv::Mat> Gradient(const cv::Mat& input);

cv::Mat NonMaxSuppression(const cv::Mat& gradientIntensity, const cv::Mat& gradientDirection);

cv::Mat DoubleThreshold(const cv::Mat& input, float lowThresholdRatio = 0.05, float highThresholdRatio = 0.09);

cv::Mat HysteresisThresholding(const cv::Mat& input);

} // namespace utils
} // namespace ImageAnalysis
47 changes: 43 additions & 4 deletions ImageAnalysis/ImageAnalyzer.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,53 @@

namespace ImageAnalysis
{
typedef std::pair<int, cv::Point2i> CircleParameters;
struct Circle
{
Circle(int x, int y, int r, int votes) : x(x), y(y), radius(r)
{
float diameter = 2 * CV_PI * r;
probability = (r > 0) ? static_cast<float>(votes) / diameter : 0.F;
filtered = false;
}
float probability;
int x;
int y;
int radius;
bool filtered;
};

class ImageAnalyzer
{
public:
virtual cv::Mat GaussianBlur(const cv::Mat& input, const int& kernelSize) = 0;
virtual cv::Mat GaussianBlur(
const cv::Mat& input,
const int& kernelSize,
const double& sigma = 1.0
) = 0;

virtual cv::Mat LoGFilter(
const cv::Mat& input,
const int& kernelSize,
const double& sigma = 1.0
) = 0;

virtual cv::Mat OtsuThreshold(
const cv::Mat& input,
const int& thresholdValue
) = 0;
virtual cv::Mat BGR2Grayscale(const cv::Mat& input) = 0;
virtual cv::Mat Canny(const cv::Mat& input, float lowThresholdRatio = 0.05, float highThresholdRatio = 0.09) = 0;
virtual std::vector<CircleParameters> CircleHoughTransform(const cv::Mat& input, const int& lowRadiusThreshold, const int& highRadiusThreshold) = 0;

virtual cv::Mat Canny(
const cv::Mat& input,
const float& lowThresholdRatio = 0.05,
const float& highThresholdRatio = 0.09
) = 0;

virtual std::vector<Circle> CircleHoughTransform(
const cv::Mat& input,
const int& lowRadiusThreshold,
const int& highRadiusThreshold,
const int& minDistance
) = 0;
};
} // namespace ImageAnalysis
Loading

0 comments on commit 1505639

Please sign in to comment.