Skip to content

Commit 0a2cde5

Browse files
committed
image tensor fixes
1 parent 5724f95 commit 0a2cde5

File tree

1 file changed

+4
-45
lines changed

1 file changed

+4
-45
lines changed

src/dtype_next/image_processing_with_tensors.clj

Lines changed: 4 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -668,47 +668,6 @@ flat-tensor
668668
;; This approach directly iterates over sliced channels without extracting them first,
669669
;; combining channel names and data in a single pass.
670670

671-
;; ## Channel Correlation
672-
673-
;; How related are the color channels? High correlation suggests color casts or
674-
;; consistent lighting. Let's compute Pearson correlation between channels:
675-
676-
(defn correlation
677-
"Compute Pearson correlation coefficient between two tensors.
678-
Returns value between -1 (inverse) and 1 (perfect correlation)."
679-
[v1 v2]
680-
(let [mean1 (dfn/mean v1)
681-
mean2 (dfn/mean v2)
682-
centered1 (dfn/- v1 mean1)
683-
centered2 (dfn/- v2 mean2)
684-
numerator (dfn/sum (dfn/* centered1 centered2))
685-
denom1 (dfn/sqrt (dfn/sum (dfn/sq centered1)))
686-
denom2 (dfn/sqrt (dfn/sum (dfn/sq centered2)))]
687-
(/ numerator (* denom1 denom2))))
688-
689-
;; Extract channels and compute pairwise correlations:
690-
691-
(let [[blue green red] (tensor/slice-right original-tensor 1)]
692-
(tc/dataset
693-
[{:pair "Blue-Green" :correlation (correlation blue green)}
694-
{:pair "Blue-Red" :correlation (correlation blue red)}
695-
{:pair "Green-Red" :correlation (correlation green red)}]))
696-
697-
(let [[blue green red] (tensor/slice-right (auto-white-balance
698-
original-tensor)
699-
1)]
700-
(tc/dataset
701-
[{:pair "Blue-Green" :correlation (correlation blue green)}
702-
{:pair "Blue-Red" :correlation (correlation blue red)}
703-
{:pair "Green-Red" :correlation (correlation green red)}]))
704-
705-
;; **Interpretation**:
706-
;; - Correlation near 1.0: Channels move together (consistent lighting, color cast)
707-
;; - Correlation near 0.0: Channels independent (varied colors, good white balance)
708-
;; - High correlations (>0.95) often indicate opportunity for white balance adjustment
709-
710-
;; ---
711-
712671
;; # Spatial Analysis — Edges and Gradients
713672

714673
;; We've explored *global* properties like channel means and histograms. Now let's
@@ -1295,14 +1254,14 @@ gaussian-5x5
12951254

12961255
;; Larger Gaussian blur for stronger effect:
12971256

1298-
(def gaussian-15x15 (gaussian-kernel 15 3.0))
1257+
(def gaussian-9x9 (gaussian-kernel 15 3.0))
12991258

1300-
(def gaussian-blurred-large (convolve-2d grayscale gaussian-15x15))
1259+
(def gaussian-blurred-large (convolve-2d grayscale gaussian-9x9))
13011260

13021261
;; Compare different blur kernels:
13031262

13041263
(kind/table
1305-
[[:original :box-blur-3x3 :gaussian-5x5 :gaussian-15x15]
1264+
[[:original :box-blur-3x3 :gaussian-5x5 :gaussian-9x9]
13061265
[(bufimg/tensor->image grayscale)
13071266
(bufimg/tensor->image (dtype/elemwise-cast blurred-gray :uint8))
13081267
(bufimg/tensor->image (dtype/elemwise-cast gaussian-blurred :uint8))
@@ -1344,7 +1303,7 @@ gaussian-5x5
13441303
(-> {:original grayscale
13451304
:box-3x3 blurred-gray
13461305
:gaussian-5x5 gaussian-blurred
1347-
:gaussian-15x15 gaussian-blurred-large
1306+
;; :gaussian-9x9 gaussian-blurred-large
13481307
:sharpened sharpened-gray}
13491308
(update-vals
13501309
(fn [t]

0 commit comments

Comments
 (0)