# Segmentation of sunspots

I recently went for a walk with my camera and snapped a few pictures of the sun. The pictures didn't turn out too well, but this was to be expected without a telescope. I thought it might be a nice exercise to segment out the sunspots and see how my results compare to the data of SOHO. Here is what I came up with:

img = Import["http://i.imgur.com/4lDwE33.jpg"];
soho = Import["http://i.imgur.com/XNmroTf.jpg"];

(*determine center and radius of the sun in the the image*)
ComponentMeasurements[
MorphologicalComponents[img, 0.2], {"Centroid",

(*segment the sun from background, polar transformp, post process *)
ImageTransformation[img,
DataRange -> Full, PlotRange -> {{0, 360 °}, {1, radius}}];
polar = ColorConvert[%, "LUV"];
mean = Mean@ImageData[polar][[All, All, 1]][[#]] & /@
normalPolar =
Image@Rescale[
ImageData[polar][[All, All, 1]][[#]] - mean[[#]] & /@

(*transform back to cartesien coordinates*)
normalCart =
ImageTransformation[
normalPolar, {ArcTan @@ (radius - #),
DataRange -> {{-180 °, 180 °}, {0, radius}},
(*segmentation of sun spots*)
segmentation =
ColorNegate@
Dilation[
Binarize[ImageCorrelate[%, GaussianMatrix[6], CosineDistance],
(*generate spot labels*)
centroids =
ComponentMeasurements[
SelectComponents[MorphologicalComponents[ColorNegate@segmentation],
labeled = Show[normalCart,
Graphics[{Red, Circle @@ # & /@ centroids[[All, 2, 1 ;; 2]],
Text, {centroids[[All, 2, 3]] - 1, {0, 6 + #[[2]]} + #[[1]] & /@
centroids[[All, 2, 1 ;; 2]]}]}]
];

(* plot:
"original image", "post processed image"
"segmented sun spots", "labeled post processed image"
"image from Solar and Heliospheric Observatory (SOHO)"
*)
GraphicsGrid[{{ImageCrop[img], normalCart}, {segmentation,
labeled}, {soho}}, Frame -> All, ImageSize -> 900]


While I'm quite happy with the result, I thought some of you guys might come up with some clever ideas to make the approach more robust and the results even better.

I'm also very thankful for a review of my code. You never stop learning!

• Nice question. The part of the code that separates the sun from the background can be replaced by ImageCrop@ImageMultiply[Binarize@img, img] I think. Sep 2 '14 at 10:49
• What is the problem with MinDetect[img, .02]? Sep 3 '14 at 5:33

Well I decided to give it a bit of a go...First import the image and convert to grayscale, then crop to focus on the area of interest. Then I used a LaplacianGaussianFilter, which is often used in blob detection.

img = ImageAdjust@ColorConvert[Import["http://i.imgur.com/4lDwE33.jpg"], "Grayscale"];
smallimg = ImageAdjust@ImageTake[img, {200, 500}, {200, 600}];

(* pretty colours! *)
coloredlogimg = Colorize[ColorNegate@logimg, ColorFunction -> "SunsetColors"];
GraphicsRow[{smallimg, coloredlogimg}, ImageSize -> Full]


The interesting thing about this is that it starts to reveal the separate sunspots right in the centre of the image, which are distinct in the SOHO comparison image but less clear in your photo. This is what initially drew me to the question!

I then proceed to binarize the image (with an automatic threshold), do a distance transform followed by maxima detection, then finally use WatershedComponents to pick out the sunspots:

binimg = Binarize[logimg, Method -> "MinimumError"];
maximg = MaxDetect[distimg, 0.2];
components = DeleteBorderComponents[
WatershedComponents[logimg, maximg, Method -> "Rainfall"]];
measures = ComponentMeasurements[

g1 = Show[coloredlogimg,
Graphics[{Red,
Map[Circle[#, 10] &, measures[[All, 2, 1]]], Blue,

soho = ImageAdjust@Import["http://i.imgur.com/XNmroTf.jpg"]