Skip to content

Commit

Permalink
Added Plotting of Brightness and Focus Value
Browse files Browse the repository at this point in the history
  • Loading branch information
treideme committed Feb 29, 2024
1 parent 8c049de commit 7112c45
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 10 deletions.
5 changes: 3 additions & 2 deletions stereo_viewer/inc/focus.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,9 @@ class Focus {
void process(QPixmap &pixmap);
private:
static cv::Mat to_mat(const QImage &image);
static double focusValue(const cv::Mat &img);
void paint(QPixmap &img);
static double focusValue(const cv::Mat &gray);
static double averageBrightness(const cv::Mat& gray);
void paint(QPixmap &img, double brightness);
size_t m_maxValues;
QColor m_lineColor;
size_t m_lineWidth;
Expand Down
2 changes: 1 addition & 1 deletion stereo_viewer/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ app_inc = include_directories(
cpp = meson.get_compiler('cpp')
# set PATH=C:\qt5\bin;%PATH%
qt5 = import('qt5')
qt5_deps = dependency('qt5', modules: ['Core', 'Gui', 'Widgets', 'Test', 'Network'])
qt5_deps = dependency('qt5', modules: ['Core', 'Gui', 'Widgets', 'Test', 'Network'], main: true)
opencv_dep = dependency('opencv4', required: false)
if not opencv_dep.found()
cv_dir = get_option('cv_dir')
Expand Down
46 changes: 40 additions & 6 deletions stereo_viewer/src/focus.cc
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,10 @@ void Focus::process(QPixmap &pixmap) {
QImage image = pixmap.toImage();
Mat mat = to_mat(image);
if(!mat.empty()) {
double value = focusValue(mat);
Mat gray;
cvtColor(mat, gray, COLOR_BGR2GRAY);
double value = focusValue(gray);
double brightness = averageBrightness(gray);
m_last_values.push_back(value);
// Overflow
if(m_last_values.size() > m_maxValues) {
Expand All @@ -53,7 +56,7 @@ void Focus::process(QPixmap &pixmap) {
m_last_values.insert(m_last_values.begin(), m_maxValues - m_last_values.size(), value);
}
// Draw focus helper
paint(pixmap);
paint(pixmap, brightness);
}
}
}
Expand All @@ -72,17 +75,20 @@ cv::Mat Focus::to_mat(const QImage &image) {
return mat;
}

double Focus::focusValue(const cv::Mat &img) {
Mat gray;
cvtColor(img, gray, COLOR_BGR2GRAY);
double Focus::focusValue(const cv::Mat &gray) {
Mat lap;
Laplacian(gray, lap, CV_64F);
Scalar mu, sigma;
meanStdDev(lap, mu, sigma);
return sigma.val[0] * sigma.val[0];
}

void Focus::paint(QPixmap &img) {
double Focus::averageBrightness(const cv::Mat& gray) {
cv::Scalar meanBrightness = cv::mean(gray);
return meanBrightness[0];
}

void Focus::paint(QPixmap &img, double brightness) {
QPainter paint(&img);
paint.setPen(QPen(m_lineColor, m_lineWidth));

Expand All @@ -104,4 +110,32 @@ void Focus::paint(QPixmap &img) {

paint.drawLine(x1, y1, x2, y2);
}

// Set up the painter for drawing text
QFont font = paint.font();
font.setPixelSize(12); // Adjust the size as needed
paint.setFont(font);
paint.setPen(Qt::red); // Choose a color that contrasts well with your image background

// Create the text to display
QString brightnessText = QString("Brightness: %1").arg(brightness, 3, 'f', 0, ' ');

// Calculate the position to draw the text
int textPadding = 5; // Padding from the top-right corner
QFontMetrics fontMetrics(font);
int textWidth = fontMetrics.width(brightnessText);
int textX = width - textWidth - textPadding; // Adjust X position based on text width
int textY = textPadding + fontMetrics.ascent(); // Adjust Y position to align text properly

// Draw the text on the image
paint.drawText(textX, textY, brightnessText);
if (!m_last_values.empty()) {
double lastValue = m_last_values.back();
QString lastValueText = QString("Sharpness: %1").arg(lastValue, 6, 'f', 0, ' ');
textX = textPadding; // X position is just the padding amount
textY = textPadding + fontMetrics.ascent(); // Adjust Y position to align text properly

// Draw the text on the image
paint.drawText(textX, textY, lastValueText);
}
}
2 changes: 1 addition & 1 deletion stereo_viewer/src/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ else
include_directories : app_inc,
install: true,
dependencies: [app_deps],
gui_app: false,
gui_app: true,
link_args : ['/SUBSYSTEM:WINDOWS']
)
endif

0 comments on commit 7112c45

Please sign in to comment.