0

enter image description here

enter image description hereenter image description here

When I attempt to open this image in GIMP it also comes up as black, same as in ImageSharp,enter image description here

enter image description here

just confused about how to show this tif, ImageSharp doesn't even recognize it as 16 bit grayscale so i'm trying to find a way to display this image using C#, i'd like to use MagickImage if Could. It's coming from a microscope.

any help would be greatly appreciated

the image doesn't allow itself to be shared on stack overflow it just says the link is brokenenter image description here

//here are the parameters imagemagick gives me:
Name: AnimationDelay, Value: 0
Name: AnimationIterations, Value: 0
Name: AnimationTicksPerSecond, Value: 100
Name: ArtifactNames, Value: ImageMagick.MagickImage+<get_ArtifactNames>d__43
Name: AttributeNames, Value: ImageMagick.MagickImage+<get_AttributeNames>d__45
Name: BackgroundColor, Value: #FFFFFFFFFFFFFFFF
Name: BaseHeight, Value: 2040
Name: BaseWidth, Value: 2040
Name: BlackPointCompensation, Value: False
Name: BorderColor, Value: #DFDFDFDFDFDFFFFF
Name: BoundingBox, Value: 
Name: ChannelCount, Value: 1
Name: Channels, Value: ImageMagick.MagickImage+<get_Channels>d__64
Name: ChromaBluePrimary, Value: ImageMagick.PrimaryInfo
Name: ChromaGreenPrimary, Value: ImageMagick.PrimaryInfo
Name: ChromaRedPrimary, Value: ImageMagick.PrimaryInfo
Name: ChromaWhitePoint, Value: ImageMagick.PrimaryInfo
Name: ClassType, Value: Direct
Name: ColorFuzz, Value: 0%
Name: ColormapSize, Value: -1
Name: ColorSpace, Value: Gray
Name: ColorType, Value: Grayscale
Name: Comment, Value: 
Name: Compose, Value: Over
Name: Compression, Value: LZW
Name: Density, Value: 0x0 inch
Name: Depth, Value: 16
Name: EncodingGeometry, Value: 
Name: Endian, Value: LSB
Name: FileName, Value: C:\\Users\\jdmso\\Downloads\\mystery.tif
Name: FilterType, Value: Undefined
Name: Format, Value: Tiff
Name: FormatInfo, Value: Tiff: Tagged Image File Format (+R+W+M)
Name: Gamma, Value: 0.45454545454545453
Name: GifDisposeMethod, Value: Undefined
Name: HasAlpha, Value: False
Name: Height, Value: 2040
Name: Interlace, Value: NoInterlace
Name: Interpolate, Value: Undefined
Name: IsDisposed, Value: False
Name: IsOpaque, Value: True
Name: Label, Value: 
Name: MatteColor, Value: #BDBDBDBDBDBDFFFF
Name: Orientation, Value: TopLeft
Name: Page, Value: 2040x2040
Name: ProfileNames, Value: ImageMagick.MagickImage+<get_ProfileNames>d__154
Name: Quality, Value: 0
Name: RenderingIntent, Value: Undefined
Name: Settings, Value: ImageMagick.MagickReadSettings
Name: Signature, Value: 5639160dfdd3d30c160b4dcb3edf252d9c24266393e60ae8500c5457f7ee23b5
Name: TotalColors, Value: 1
Name: VirtualPixelMethod, Value: Undefined
Name: Width, Value: 2040

I don't know how to share the zip file?

jdmneon
  • 444
  • 7
  • 12
  • 1
    Please post the original image, not screen snaps. If necessary, zip it so that no changes occur. – fmw42 Sep 22 '22 at 17:13
  • Why exactly do you think the problem is with ImageMagick and not with whatever/however you are trying to display the image? Perhaps ImageMagick handles the file just fine, and it's something else in your code that has an issue... –  Sep 22 '22 at 17:16
  • have you tried to convert it to 8bit grayscale? my guess is probably many tools cannot properly handle 16bit grayscale images – Erdogan Kurtur Sep 22 '22 at 17:36
  • What command did you use to display the image in Imagemagick? – fmw42 Sep 22 '22 at 19:49
  • how can I post the zipped file? – jdmneon Sep 22 '22 at 22:58
  • We can't share Tiff images (or zipped files) in Stack Overflow site. Try one of the [following sites](https://www.online-tech-tips.com/software-reviews/the-5-best-anonymous-file-sharing-and-hosting-sites/). Please replace the screenshot with the actual code (and try to post an executable code sample). – Rotem Sep 23 '22 at 20:57
  • https://filedropper.com/d/s/GRejpFPwiwyJwbGq1cwrNlGIjefXQe – jdmneon Sep 26 '22 at 23:22

1 Answers1

1

For 16 bit grayscale, we may use auto-level option:

image.AutoLevel();

Assume your PC supports only 8 bits grayscale display (in RGB format, the grayscale is displayed as 8 bits per red, green and blue, when r=g=b for each pixel).

When we want to display a 16 bits image, we have to convert it to 8 bits first.
The default conversion used by your viewer is getting the upper 8 bits of every 16 bits, and ignoring the lower 8 bits (equivalent of dividing all pixels by 256).

In case the pixels range is about [0, 255] for example (or say [0, 1000]), the image is going to be very dark, or entirely black.
In your case, the pixels range is probably low, so the displayed image looks black.

The "auto-level" processing, adjusts the pixel range using "linear stretching".
Finds the minimum and the maximum pixel values, and applies scale and offset that brings the minimum to 0 (black) and the maximum to 255 [or 65535 for 16bits] (white).


For correctly handling 16 bit images, we have to install Magick.NET-Q16.
See the following post for details.

For testing, I used the oldest release of MagickViewer.

Added the following code:

//Update for automatic ajusting the 16 bits image for display
////////////////////////////////////////////////////////////////////////////
foreach (var image in Images)
{
    image.AutoLevel();
}
////////////////////////////////////////////////////////////////////////////

Complete testing code:

//=================================================================================================
// Copyright 2013-2014 Dirk Lemstra <https://magickviewer.codeplex.com/>
//
// Licensed under the ImageMagick License (the "License"); you may not use this file except in 
// compliance with the License. You may obtain a copy of the License at
//
//   http://www.imagemagick.org/script/license.php
//
// Unless required by applicable law or agreed to in writing, software distributed under the
// License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
// express or implied. See the License for the specific language governing permissions and
// limitations under the License.
//=================================================================================================
//https://github.com/dlemstra/MagickViewer/tree/6.8.9.501

using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Linq;
using System.Threading;
using System.Windows.Threading;
using ImageMagick;
using Microsoft.Win32;

namespace MagickViewer
{
    //==============================================================================================
    internal sealed class ImageManager
    {
        //===========================================================================================
        private static readonly object _Semaphore = new object();
        private static readonly string[] _GhostscriptFormats = new string[]
        {
            ".EPS", ".PDF", ".PS"
        };
        //===========================================================================================
        private Dispatcher _Dispatcher;
        private OpenFileDialog _OpenDialog;
        private SaveFileDialog _SaveDialog;
        //===========================================================================================
        private void ConstructImages()
        {
            if (Images != null)
                Images.Dispose();

            Images = new MagickImageCollection();
        }
        //===========================================================================================
        [SuppressMessage("Microsoft.Globalization", "CA1308:NormalizeStringsToUppercase")]
        private static string CreateFilter(IEnumerable<MagickFormatInfo> formats)
        {
            string filter = "All supported formats (...)|*." + string.Join(";*.",
                                            from formatInfo in formats
                                            orderby formatInfo.Format
                                            select formatInfo.Format.ToString().ToLowerInvariant());

            filter += "|" + string.Join("|",
                                            from formatInfo in formats
                                            orderby formatInfo.Description
                                            group formatInfo.Format by formatInfo.Description into g
                                            select g.Key + "|*." + string.Join(";*.", g).ToLowerInvariant());
            return filter;
        }
        //===========================================================================================
        private void Initialize()
        {
            _OpenDialog = new OpenFileDialog();
            SetOpenFilter();

            _SaveDialog = new SaveFileDialog();
            SetSaveFilter();
        }
        //===========================================================================================
        private void OnLoaded()
        {
            if (Loaded == null)
                return;

            _Dispatcher.Invoke((Action)delegate()
            {
                Loaded(this, EventArgs.Empty);
                Monitor.Exit(_Semaphore);
            });
        }
        //===========================================================================================
        private void OnLoading()
        {
            if (Loading != null)
                Loading(this, EventArgs.Empty);
        }
        //===========================================================================================
        private void ReadImage(FileInfo file)
        {
            ConstructImages();

            try
            {
                MagickReadSettings settings = new MagickReadSettings();
                if (_GhostscriptFormats.Contains(file.Extension.ToUpperInvariant()))
                    settings.Density = new MagickGeometry(300, 300);

                Images.Read(file, settings);
                FileName = file.Name;

                //Update for automatic ajusting the 16 bits image for display
                ////////////////////////////////////////////////////////////////////////////
                foreach (var image in Images)
                {
                    image.AutoLevel();
                }
                ////////////////////////////////////////////////////////////////////////////
            }
            catch (MagickErrorException)
            {
                //TODO: Handle error
            }

            OnLoaded();
        }
        //===========================================================================================
        private void Save(string fileName)
        {
            Images.Write(fileName);
        }
        //===========================================================================================
        private void SetOpenFilter()
        {
            var formats = from formatInfo in MagickNET.SupportedFormats
                              where formatInfo.IsReadable
                              select formatInfo;

            _OpenDialog.Filter = CreateFilter(formats);
        }
        //===========================================================================================
        private void SetSaveFilter()
        {
            var formats = from formatInfo in MagickNET.SupportedFormats
                              where formatInfo.IsWritable
                              select formatInfo;

            _SaveDialog.Filter = CreateFilter(formats);
        }
        //===========================================================================================
        public ImageManager(Dispatcher dispatcher)
        {
            _Dispatcher = dispatcher;

            Initialize();
        }
        //===========================================================================================
        public event EventHandler Loading;
        //===========================================================================================
        public event EventHandler Loaded;
        //===========================================================================================
        public string FileName
        {
            get;
            private set;
        }
        //===========================================================================================
        public MagickImageCollection Images
        {
            get;
            private set;
        }
        //===========================================================================================
        public static bool IsSupported(string fileName)
        {
            if (string.IsNullOrEmpty(fileName))
                return false;

            if (fileName.Length < 2)
                return false;

            string extension = Path.GetExtension(fileName);
            if (string.IsNullOrEmpty(extension))
                return false;

            extension = extension.Substring(1);
            MagickFormat format;
            if (!Enum.TryParse<MagickFormat>(extension, true, out format))
                return false;

            return (from formatInfo in MagickNET.SupportedFormats
                      where formatInfo.IsReadable && formatInfo.Format == format
                      select formatInfo).Any();

        }
        //===========================================================================================
        public void Load(string fileName)
        {
            Monitor.Enter(_Semaphore);

            OnLoading();

            Thread thread = new Thread(() => ReadImage(new FileInfo(fileName)));
            thread.Start();
        }
        //===========================================================================================
        public void ShowOpenDialog()
        {
            if (_OpenDialog.ShowDialog() != true)
                return;

            Load(_OpenDialog.FileName);
        }
        //===========================================================================================
        public void ShowSaveDialog()
        {
            if (_SaveDialog.ShowDialog() != true)
                return;

            Save(_SaveDialog.FileName);
        }
        //===========================================================================================
    }
    //==============================================================================================
}
Rotem
  • 30,366
  • 4
  • 32
  • 65
  • I am use the newest Q16 version and it works with the images off of other microscopes – jdmneon Sep 22 '22 at 23:21
  • it seems to have a density of Name: Density, Value: 0x0 inch – jdmneon Sep 23 '22 at 00:22
  • Are you saying that `AutoLevel()` doesn't solve the issue? – Rotem Sep 23 '22 at 08:07
  • no it does not solve the problem, I am using Q16, Can I send you the file somehow? I can get it to open in ImageJ but not in ImageSharp or ImageMagick or anything in C# – jdmneon Sep 26 '22 at 23:14
  • AutoLevel doesn't solve the issue. I believe the issue may have something to do with LZW compression. If I open this file in ImageSharp or ImageMagick and save it, then I can no longer open it in any program, it's not being loaded correctly and I don't know why https://filedropper.com/d/s/GRejpFPwiwyJwbGq1cwrNlGIjefXQe – jdmneon Sep 26 '22 at 23:17
  • I downloaded `mystery.tif` file, executed the "MagickViewer" code sample under "Complete testing code" (code at the bottom of my answer), and the image is [displayed correctly](https://i.stack.imgur.com/Lfv4F.png). Did you try executing that "MagickViewer" code sample? – Rotem Sep 27 '22 at 08:14
  • Rotem what verison are you using it comes up as black. I have opened an Issue with ImageMagick if you have found a way to view please post here, they are currently working on it. It does not work in newest version on WIndows https://github.com/ImageMagick/ImageMagick/issues/5597 – jdmneon Sep 27 '22 at 20:35
  • 1
    The MagickViewer example is [https://github.com/dlemstra/MagickViewer/tree/6.8.9.501](https://github.com/dlemstra/MagickViewer/tree/6.8.9.501), and the Magick.NET-Q16-x64 version is also `6.8.9.501` (relatively old version). Can you please post a complete code sample that I can build and execute (edit your question)? – Rotem Sep 27 '22 at 20:45
  • I truly do appreciate your input it will probably help dlemstra solve the bug, versions before 6.9 do work, its not your imaginatoin so a bug was introduced later I cannot roll back to version 6 it lacks features and speed enhancements that we need. this will give you a better idea of whats going on it seems like ImageMagick needs to read the RowsPerStrip tag or do something when it is absent. The best way to help at this point is to help fix the bug they have identified in ImageMagick There has been real progress made in the last few days github.com/ImageMagick/ImageMagick/issues/5597 – jdmneon Sep 29 '22 at 20:52
  • I thought the solution may be converting the image to 8 bits, but I see the bug concerns loading the image (converting to 8 bits after loading is not going to help). After the bug is fixed, you may still want to execute `image.AutoLevel()`. – Rotem Sep 29 '22 at 21:26
  • how are you getting that old version to run? I can't run it at all in Visual Studio 2022 which doesn't totally surprise me that version is like 8 and 1/2 years old now – jdmneon Oct 02 '22 at 00:22
  • **1.** I downloaded the code from [here](https://github.com/dlemstra/MagickViewer/tree/6.8.9.501). **2.** I opened `MagickViewer.sln` in Visual Studio 2019. I didn't change the configuration (the target framework is ".NET Framework 4 Client Profile"). **3.** I Installed NuGet Package for the Solution: "Magick.NET-Q16-x64", selected version 6.8.9.501 – Rotem Oct 02 '22 at 16:41
  • yeah I ended up getting it to work but it doesn't have tons of functionality we are already using, in theory we could juggle versions but it's just too old we will just wait two weeks or so for the next ImageMagick release. We can work on other parts of the code in the meantime. – jdmneon Oct 02 '22 at 18:52
  • For continue the development, you may convert the Tiff files to PNG format (16 bits Grayscale PNG). In case there is a bug in ImageMagick, you may use FFmpeg (in command line). Example: `ffmpeg -i mystery.tif mystery.png` – Rotem Oct 02 '22 at 19:29
  • The bug in ImageMagick was fixed in the latest release – jdmneon Oct 08 '22 at 19:27