Kstars

focusblurriness.cpp
1/*
2 SPDX-FileCopyrightText: 2024 John Evans <john.e.evans.email@googlemail.com>
3
4 SPDX-License-Identifier: GPL-2.0-or-later
5*/
6
7#include "focusblurriness.h"
8
9namespace Ekos
10{
11
12FocusBlurriness::FocusBlurriness()
13{
14 m_maxX = m_maxY = -1.0;
15}
16
17FocusBlurriness::~FocusBlurriness()
18{
19}
20
21ImageMosaicMask * FocusBlurriness::getMosaicMask(const QSharedPointer<ImageMask> &mask, const int tile)
22{
23 ImageMosaicMask *mosaicMask = nullptr;
24 if (mask)
25 {
26 if (tile < 0 || tile >= NUM_TILES)
27 qCDebug(KSTARS_EKOS_FOCUS) << QString("%1 called with invalid mosaic tile %2").arg(__FUNCTION__)
28 .arg(tile);
29 else
30 {
31 mosaicMask = dynamic_cast<ImageMosaicMask *>(mask.get());
32 if (!mosaicMask)
33 qCDebug(KSTARS_EKOS_FOCUS) << QString("%1 called with invalid 2 mosaic tile %2").arg(__FUNCTION__)
34 .arg(tile);
35 }
36 }
37 else
38 qCDebug(KSTARS_EKOS_FOCUS) << QString("%1 called for mosaic tile but no mask").arg(__FUNCTION__);
39
40 return mosaicMask;
41}
42
43int FocusBlurriness::getCVType(const int type)
44{
45 int cvType = -1;
46 switch (type)
47 {
48 case TBYTE:
49 cvType = CV_MAKETYPE(CV_8U, 1); // uint8_t
50 break;
51
52 case TSHORT:
53 cvType = CV_MAKETYPE(CV_16S, 1); // short
54 break;
55
56 case TUSHORT:
57 cvType = CV_MAKETYPE(CV_16U, 1); // unsigned short
58 break;
59
60 case TLONG:
61 cvType = CV_MAKETYPE(CV_32S, 1); // long
62 break;
63
64 case TULONG:
65 qCDebug(KSTARS_EKOS_FOCUS) << "OpenCV does not support " << type << " Cannot calc blurriness";
66 break;
67
68 case TFLOAT:
69 cvType = CV_MAKETYPE(CV_32F, 1); // long
70 break;
71
72 case TLONGLONG:
73 qCDebug(KSTARS_EKOS_FOCUS) << "OpenCV does not support " << type << " Cannot calc blurriness";
74 break;
75
76 case TDOUBLE:
77 cvType = CV_MAKETYPE(CV_64F, 1); // double
78 break;
79
80 default:
81 qCDebug(KSTARS_EKOS_FOCUS) << "Unknown image buffer datatype " << type << " Cannot calc blurriness";
82 break;
83 }
84 return cvType;
85}
86
87cv::Rect FocusBlurriness::calcROIfromTile(const int tile, ImageMosaicMask *mosaicMask)
88{
89 unsigned int topLeftX = 0, topLeftY = 0, width = 0, height = 0;
90
91 if (tile >= 0 && tile < NUM_TILES)
92 {
93 // Calculating for a single (square) mosaic tile
94 topLeftX = mosaicMask->tiles()[tile].topLeft().x();
95 topLeftY = mosaicMask->tiles()[tile].topLeft().y();
96 width = mosaicMask->tiles()[tile].width();
97 height = width;
98 }
99 cv::Rect cvROI(topLeftX, topLeftY, width, height);
100 return cvROI;
101}
102
103void FocusBlurriness::applyRingMaskToImage(cv::Mat &img, const int width, const int height, ImageRingMask *ringMask)
104{
105 // Setup a mask for img with all elements zero except the ring with elements 1
106 // Since its the same mask for each datapoint in an Autofocus I could calculate it once
107 // but it seems quite fast.
108 cv::Mat cvMask = cv::Mat::zeros(img.size(), CV_8UC1);
109 const float diagonalRadius = std::sqrt((width * width + height * height) / 4.0);
110 const float innerRadius = ringMask->innerRadius() * diagonalRadius;
111 const float outerRadius = ringMask->outerRadius() * diagonalRadius;
112 const cv::Point center(width / 2, height / 2);
113 cv::circle(cvMask, center, outerRadius, 255, cv::FILLED, cv::FILLED);
114 cv::circle(cvMask, center, innerRadius, 0, cv::FILLED, cv::FILLED);
115
116 // Apply the mask and convert back to img for further processing. For some reason not using an
117 // intermediate Mat (res) works while just using img does not.
118 cv::Mat res;
119 img.copyTo(res, cvMask);
120 img = res;
121 if (debug)
122 {
123 cv::imshow("Mask", cvMask);
124 cv::imshow("Image with mask", img);
125 }
126}
127} // namespace
Type type(const QSqlDatabase &db)
Ekos is an advanced Astrophotography tool for Linux.
Definition align.cpp:83
T * get() const const
QString arg(Args &&... args) const const
QTextStream & center(QTextStream &stream)
This file is part of the KDE documentation.
Documentation copyright © 1996-2025 The KDE developers.
Generated on Fri Jan 3 2025 11:47:14 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.