Skip to content

Commit

Permalink
feat: Closing
Browse files Browse the repository at this point in the history
  • Loading branch information
Max committed Feb 4, 2021
1 parent d185336 commit b4467ae
Show file tree
Hide file tree
Showing 8 changed files with 166 additions and 43 deletions.
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
"dependencies": {
"@babel/core": "^7.12.10",
"@babel/preset-env": "^7.12.11",
"@pixinsight/core": "^0.0.1",
"@pixinsight/react": "^0.0.1",
"@pixinsight/ui": "^0.0.1",
"@pixinsight/core": "^0.0.2",
"@pixinsight/react": "^0.0.2",
"@pixinsight/ui": "^0.0.2",
"@types/react": "^17.0.0",
"@types/webpack": "^4.41.26",
"@types/webpack-sources": "^2.1.0",
Expand Down
15 changes: 12 additions & 3 deletions src/ImagePreview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
UIStretch,
UIVerticalSizer,
} from "@pixinsight/ui";
import React, { useMemo, useRef, useState } from "react";
import React, { useEffect, useMemo, useRef, useState } from "react";

export type ReadoutData = [
x: number,
Expand Down Expand Up @@ -75,6 +75,10 @@ export function ImagePreview({
];
}, [image, size]);

useEffect(() => {
controlRef.current?.update();
}, [size, image, cross]);

function onPaint() {
if (!controlRef.current || !bmp) {
return;
Expand Down Expand Up @@ -114,14 +118,19 @@ export function ImagePreview({
(pixelValue & 0x000000ff) / 0xff,
];

let readout = `X: ${rx} Y: ${ry}`;
const [imageX, imageY] = [
Math.round((rx * image?.width) / bmp?.width),
Math.round((ry * image?.height) / bmp?.height),
];

let readout = `X: ${imageX} Y: ${imageY}`;
if (channelsCount === 1 && c0 != null) {
readout += ` K: ${c0.toFixed(4)}`;
} else {
readout += ` R: ${c0.toFixed(4)} G: ${c1.toFixed(4)} B: ${c2.toFixed(4)}`;
}
setReadoutText(readout);
setReadout([rx, ry, channelsCount, c0, c1, c2]);
setReadout([imageX, imageY, channelsCount, c0, c1, c2]);
}

function onMousePressInternal(...args: any[]) {
Expand Down
4 changes: 4 additions & 0 deletions src/ImagePreviewSelect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ export function ImagePreviewSelect({
}
}, [rect]);

useEffect(() => {
controlRef.current?.update();
}, [zoom, size, rect, image]);

function onPaint() {
if (!controlRef.current || !bmp) {
return;
Expand Down
2 changes: 1 addition & 1 deletion src/NumericControl.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export function NumericControl(props: ComponentProps<typeof UISlider>) {
const [value, setValue] = useState<number>(props.value ?? 0);

useEffect(() => {
setText(props.value.toString());
setText(props.value?.toString() ?? '');
setValue(props.value * factor);
}, [props.value]);

Expand Down
139 changes: 119 additions & 20 deletions src/ScriptDialog.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
import {
FrameStyle_Box,
TextAlign_Center,
TextAlign_VertCenter,
} from "@pixinsight/core";
import { FrameStyle_Box, TextAlign_VertCenter } from "@pixinsight/core";
import { useDialog } from "@pixinsight/react";
import {
UIControl,
Expand All @@ -23,17 +19,20 @@ import { ImagePreviewSelect } from "./ImagePreviewSelect";
import { NumericControl } from "./NumericControl";
import { binarize } from "./process/binarize";
import { convolute } from "./process/convolute";
import { dilation } from "./process/dilation";
import { MorphologicalOperator, morphology } from "./process/morphology";
import { assignThroughMask, subtract } from "./process/pixelMath";
import { structures } from "./process/structures";

export const SCRIPT_NAME = "Star De-emphasizer";
export const SCRIPT_VERSION = version;
const SCRIPT_DESCRIPTION = `<b> ${SCRIPT_NAME} v${version}</b> &mdash; This script uses the method suggested by Adam Block to de-emphasize stars.<br><br>Copyright (c) 2021 Maxim Valenko @AstroSwell`;

export const defaultParameters = {
structuresMinLayer: 1,
structuresMaxLayer: 3,
binarizeThreshold: 0.2,
closingSize: 7,
closingDilationSize: 9,
dilationSize: 5,
convolutionSize: 9,
};
Expand All @@ -46,6 +45,7 @@ type Step =
| "luminance"
| "structures"
| "binarized"
| "closed"
| "dilated"
| "convoluted"
| "halos"
Expand All @@ -57,6 +57,7 @@ const stepMap: Record<Step, Step> = {
luminance: "original",
structures: "luminance",
binarized: "structures",
closed: "binarized",
dilated: "binarized",
convoluted: "dilated",
halos: "convoluted",
Expand Down Expand Up @@ -86,7 +87,8 @@ export function ScriptDialog({
const [currentStep, setCurrentStep] = useState<Step>("original");
const [previousStep, setPreviousStep] = useState<Step>("original");

const [isEnabledControls, setIsEnabledControls] = useState(true);
const [isControlsEnabled, setIsControlsEnabled] = useState(true);
const [isClosingEnabled, setIsClosingEnabled] = useState(false);

const [parameters, setParameters] = useState({
...defaultParameters,
Expand All @@ -112,6 +114,10 @@ export function ScriptDialog({
starless: previewImages.starless,
});
setCurrentStep("original");
} else {
setPreviewImages({
starless: previewImages.starless,
});
}

if (starlessImage) {
Expand All @@ -122,6 +128,10 @@ export function ScriptDialog({
original,
starless: previewStarlessImage,
});
} else {
setPreviewImages({
original
});
}
}, [rect, targetImage, starlessImage]);

Expand Down Expand Up @@ -157,8 +167,30 @@ export function ScriptDialog({
threshold: parameters.binarizeThreshold,
});

let closedImage: Image | undefined;
let binarizedMinusClosed: Image | undefined;

if (isClosingEnabled) {
console.log("Closing...");
closedImage = morphology(
MorphologicalOperator.DilationFilter,
morphology(
MorphologicalOperator.ErosionFilter,
binarizedImage,
parameters.closingSize
),
parameters.closingDilationSize
);

binarizedMinusClosed = subtract(binarizedImage, closedImage)
}

console.log("MorphologicalTransformation Dilation...");
const dilatedImage = dilation(binarizedImage, parameters.dilationSize);
const dilatedImage = morphology(
MorphologicalOperator.DilationFilter,
binarizedMinusClosed ?? binarizedImage,
parameters.dilationSize
);

console.log("Convolution...");
const convolutedImage = convolute(dilatedImage, parameters.convolutionSize);
Expand All @@ -173,6 +205,7 @@ export function ScriptDialog({
lumImage,
structuresImage,
binarizedImage,
closedImage,
dilatedImage,
convolutedImage,
halosImage,
Expand All @@ -185,13 +218,14 @@ export function ScriptDialog({
return;
}

setIsEnabledControls(false);
setIsControlsEnabled(false);

try {
const {
lumImage,
structuresImage,
binarizedImage,
closedImage,
dilatedImage,
convolutedImage,
halosImage,
Expand All @@ -203,6 +237,7 @@ export function ScriptDialog({
luminance: lumImage,
structures: structuresImage,
binarized: binarizedImage,
closed: closedImage,
dilated: dilatedImage,
convoluted: convolutedImage,
halos: halosImage,
Expand All @@ -215,15 +250,15 @@ export function ScriptDialog({
console.error(error);
}

setIsEnabledControls(true);
setIsControlsEnabled(true);
}

function onApplyClick() {
if (!targetImage || !starlessImage) {
return;
}

setIsEnabledControls(false);
setIsControlsEnabled(false);

try {
const { finalImage } = process(targetImage, starlessImage);
Expand All @@ -241,7 +276,7 @@ export function ScriptDialog({
console.error(error);
}

setIsEnabledControls(true);
setIsControlsEnabled(true);
}

function onResetClick() {
Expand Down Expand Up @@ -341,7 +376,7 @@ export function ScriptDialog({
textAlignment={TextAlign_VertCenter}
/>
<UISpinBox
minWidth={70}
autoAdjustWidth={false}
minValue={1}
maxValue={10}
value={parameters.structuresMinLayer}
Expand All @@ -360,7 +395,7 @@ export function ScriptDialog({
textAlignment={TextAlign_VertCenter}
/>
<UISpinBox
minWidth={70}
autoAdjustWidth={false}
maxValue={10}
value={parameters.structuresMaxLayer}
minValue={parameters.structuresMinLayer}
Expand All @@ -376,7 +411,10 @@ export function ScriptDialog({

<UIGroupBox title="Binarization" spacing={5} margin={5}>
<UIHorizontalSizer spacing={5}>
<UILabel text="Threshold: " textAlignment={TextAlign_Center} />
<UILabel
text="Threshold: "
textAlignment={TextAlign_VertCenter}
/>
<NumericControl
value={parameters.binarizeThreshold}
onValueUpdated={(binarizeThreshold) => {
Expand All @@ -391,9 +429,59 @@ export function ScriptDialog({
</UIHorizontalSizer>
</UIGroupBox>

<UIGroupBox
title="Closing"
spacing={5}
margin={5}
titleCheckBox={true}
checked={isClosingEnabled}
onCheck={(checked) => setIsClosingEnabled(checked)}
>
<UIHorizontalSizer spacing={5}>
<UILabel
text="Size: "
textAlignment={TextAlign_VertCenter}
minWidth={70}
/>
<UISpinBox
minValue={3}
maxValue={11}
stepSize={2}
value={parameters.closingSize}
onValueUpdated={(closingSize) => {
setParameters({ ...parameters, closingSize });
onParameterChange?.("closingSize", closingSize);
}}
/>
<UIStretch />
</UIHorizontalSizer>

<UIHorizontalSizer spacing={5}>
<UILabel
text="Dilation size: "
textAlignment={TextAlign_VertCenter}
minWidth={70}
/>
<UISpinBox
minValue={3}
maxValue={11}
stepSize={2}
value={parameters.closingDilationSize}
onValueUpdated={(closingDilationSize) => {
setParameters({ ...parameters, closingDilationSize });
onParameterChange?.(
"closingDilationSize",
closingDilationSize
);
}}
/>
<UIStretch />
</UIHorizontalSizer>
</UIGroupBox>

<UIGroupBox title="Dilation" spacing={5} margin={5}>
<UIHorizontalSizer spacing={5}>
<UILabel text="Size: " textAlignment={TextAlign_Center} />
<UILabel text="Size: " textAlignment={TextAlign_VertCenter} />
<UISpinBox
minValue={3}
maxValue={11}
Expand All @@ -410,7 +498,7 @@ export function ScriptDialog({

<UIGroupBox title="Convolution" spacing={5} margin={5}>
<UIHorizontalSizer spacing={5}>
<UILabel text="Size: " textAlignment={TextAlign_Center} />
<UILabel text="Size: " textAlignment={TextAlign_VertCenter} />
<UISpinBox
minValue={3}
maxValue={61}
Expand All @@ -437,7 +525,7 @@ export function ScriptDialog({
<UIPushButton
text="Process Preview"
onClick={onProcessPreviewClick}
enabled={Boolean(targetImage && starlessImage) && isEnabledControls}
enabled={Boolean(targetImage && starlessImage) && isControlsEnabled}
/>

<UIStretch />
Expand Down Expand Up @@ -491,6 +579,17 @@ export function ScriptDialog({
}
onMouseDoubleClick={() => saveAsView(previewImages.binarized)}
/>
{isClosingEnabled && (
<ImagePreview
image={previewImages.closed}
title="Closing"
active={currentStep === "closed"}
onMousePress={() =>
previewImages.closed && setCurrentStep("closed")
}
onMouseDoubleClick={() => saveAsView(previewImages.closed)}
/>
)}
<ImagePreview
image={previewImages.dilated}
title="Dilated"
Expand Down Expand Up @@ -551,13 +650,13 @@ export function ScriptDialog({
onClick={onResetClick}
icon=":/icons/reload.png"
text="Reset"
enabled={isEnabledControls}
enabled={isControlsEnabled}
/>
<UIPushButton
onClick={onApplyClick}
icon=":/icons/ok.png"
text="Apply"
enabled={isEnabledControls}
enabled={isControlsEnabled}
/>
</UIHorizontalSizer>
</UIVerticalSizer>
Expand Down
2 changes: 2 additions & 0 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ Object.entries(defaultParameters).forEach(([key, value]) => {
parameters[key] = parsed;
});

console.clear();

render(
<ScriptDialog
parameters={parameters}
Expand Down
Loading

0 comments on commit b4467ae

Please sign in to comment.