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

KDE's Doxygen guidelines are available online.