Combining HSB back to RGB

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Combining HSB back to RGB

Post by fmw42 »

Hi,

I would very much like to separate an RGB image to HSB, process the B and/or S channels and then recombine them back to RGB looking like an enhanced version of the original. I have been able to do the first two steps successfully, but I can find no way to reverse the process and go from HSB channels back to a single proper RGB colorspace image.

This seems to work properly to get 3 H,S,B channel images from an RBG image
convert fred1_rgb.jpg -colorspace HSB -separate fred1_hsb_%d.png

But the following puts the images back as if they were RBG channels
convert fred1_hsb_0.png fred1_hsb_1.png fred1_hsb_2.png -combine fred1_hsb.png
as doing

identify -verbose fred1_hsb.png reports the colorspace as RGB

and there does not seem to be a way to force the colorspace to HSB such as

convert fred1_hsb_0.png fred1_hsb_1.png fred1_hsb_2.png -colorspace HSB -combine fred1_hsb2.png
convert fred1_hsb2.png -colorspace RGB fred1_rgb2.png

It would be nice to be able to set the colorspace to HSB when combining so that one could could convert back to RGB.

In general, it would be nice to be able to transform back and forth between and two color spaces or at least RBG <-> any other 3-channel colorspace and RBG <-> CYMK

Fred Weinhaus
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Combining HSB back to RGB

Post by anthony »

I was suprised that -colorspace HSB -separate actually worked as it conflicted with my understanding of how IM represnts images in memory!!!!

Thinking about this has caused me to re-examine how IM stores images, and consequently re-write the major IM examples sections...

Color Space (image representation in memory)
http://www.imagemagick.org/Usage/basics/#colorspace

Color Channels (separating and combining color channels)
http://www.imagemagick.org/Usage/channels/#channels

Summery...

Using the builtin rose image....

Code: Select all

   convert rose: -colorspace HSB -separate rose_HSB_%d.gif
   convert rose_HSB_?.gif -colorspace HSB -combine x:
   convert rose_HSB_?.gif  -combine x:
Neither of the combined images comes out correctly.

The problem here is that -combine only combines Red, Green, Blue grey-scale channel images into a RGB image.

But if you use -colorspace HSB operator on the input images, then it re-organizes the greyscale images into HSB grey-scale equivelents. These naturally do not combine to produce the original RGB image!

This can be classed as a bug, and it is a consequence of how combine was first developed. That is -combine is basied on this example channel coping example...

Code: Select all

    convert rose: -channel R -separate separate_red.gif
    convert rose: -channel G -separate separate_green.gif
    convert rose: -channel B -separate separate_blue.gif

    convert separate_red.gif  \
            separate_green.gif -compose CopyGreen -composite \
            separate_blue.gif  -compose CopyBlue  -composite \
            rose_composed.gif
This brings up the solution...

Code: Select all

    convert rose: -colorspace HSB -separate rose_HSB_%d.gif

    convert separate_HSB_0.gif -colorspace HSB \
            separate_HSB_0.gif -compose CopyRed   -composite \
            separate_HSB_1.gif -compose CopyGreen -composite \
            separate_HSB_2.gif -compose CopyBlue  -composite \
            -colorspace RGB   rose_HSB_combined.gif
The first line just creates an image the right dimentions, though the image should probably be cleared, incase not all the channels are being copyed into it. It then copes the RGB-grayscale channel images into this correctly colorspaced image.

This WORKS. But requires you to know the colorspace the greyscale images represent, which requires some type of input from the user. That is the color space, without it changing the input greyscale images.

I have submitted a proposal to get this situation fixed properly.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
KypKool
Posts: 1
Joined: 2015-11-30T06:31:56-07:00
Authentication code: 1151

Re: Combining HSB back to RGB

Post by KypKool »

Hi Old post but I found the solution at the same time.
I have an HSV/HSB image coded on 0 255.
What you need to do is
convert image_hsv.png -colorspace RGB -separate myimage_channels_%d.png
convert myimage_channels_*.png -set colorspace HSB -combine -colorspace sRGB out.png
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Combining HSB back to RGB

Post by fmw42 »

I am not sure why you are raising this extremely old post. The correct procedure has been resolved and known for a long time (>7yrs). See http://www.imagemagick.org/Usage/color_ ... bine_other
Post Reply