Opencv: Converting hue image to RGB image - colors

I am trying to show the hue component of the image from my webcam. I have split apart the image into the hue component but I can't figure out how to show the hue component as the pure colors. For example if one pixel of the image was B=189 G=60 R=60 then in HSV, H=0. I don't want the draw image to be the the gray values of hue but the RGB equivalent of the hue or H=0 -> B=0 G=0 R=255
IplImage *image, *imageHSV, *imageHue;
image = cvQueryFrame(capture); //image from webcam
imageHSV = cvCreateImage( cvGetSize(image), IPL_DEPTH_8U, 3 );
imageHue = cvCreateImage( cvGetSize(image), IPL_DEPTH_8U, 1 );
cvCvtColor( image, imageHSV, CV_BGR2HSV );
cvSplit( imageHSV, imageHue, 0, 0, 0 );
I have a feeling there is a simple solution so any help is appreciated.

If I understand you correctly, you want to "correctly" visualize just the Hue component. You can create another imageSat and imageVal, one-channel each and filled with 255 (maximum). Then cvMerge your imageHue with the other two, to create a new HSV image, and convert that back to RGB/BGR for final display.

Related

How to convert png pure black & white (no greyscale) with Sharp.js

I need to convert a png with color into a png that is only black and white. I'm currently processing images Sharp.js. But I haven't been able to find a way to generate a monochromatic image.
I found a greyscale option.
const sharp = require('sharp');
sharp('color-image.png')
.toGreyscale()
.toFile('b-w-image.png')
.then(() => {
console.log('Huzzah!')
});
But this doesn't work for my needs. I'm looking for a pure black and white image with no shades of grey.
I haven't found anything in the documentation that allows you to specify the level of greyscale to allow for only a B/W image.
Is there a method available that converts the image into pure black and white?
This will get you an image with two colors. Increase the contrast using the linear option.
const sharp = require('sharp');
sharp('ben.png')
.greyscale() // make it greyscale
.linear(1.5, 0) // increase the contrast
.png({colors:2}) // reduce image to two colors
.toFile('b-w-image.png')
.then(() => {
console.log('Huzzah!')
});

SwiftUI tint for image but preserving Black and White

I need to shift the color in an image, e.g. from gray to green. But only the parts that are not white and/or black...
For UIKit I had a handy extension:
// colorize image with given tint color
// this is similar to Photoshop's "Color" layer blend mode
// this is perfect for non-greyscale source images, and images that have both highlights and shadows that should be preserved
// white will stay white and black will stay black as the lightness of the image is preserved
func tint(tintColor: UIColor) -> UIImage {
return modifiedImage { context, rect in
// draw black background - workaround to preserve color of partially transparent pixels
context.setBlendMode(.normal)
UIColor.black.setFill()
context.fill(rect)
// draw original image
context.setBlendMode(.normal)
context.draw(self.cgImage!, in: rect)
// tint image (loosing alpha) - the luminosity of the original image is preserved
context.setBlendMode(.color)
tintColor.setFill()
context.fill(rect)
// mask by alpha values of original image
context.setBlendMode(.destinationIn)
context.draw(self.cgImage!, in: rect)
}
}
is there any way to generate the same functionality with the tint options in SwiftUI?
".colorMultiply" colors white as well.
".saturation(0.5)" directly generates a grayscale image.
You can continue to use your extension, like
var body: some View {
Image(uiImage: UIImage(named: "some")!.tint(tintColor: UIColor.red))
}
I was looking for the wrong keyword.
The actually way to do this in SwiftUI is hueRotation!
It only works with coloured images and not with grayscale images though.
See example below:
Color.blue
.hueRotation(.degrees(-45.0))

how to fill color to image in canvas using fabric.js

I have added three images to which I need to fill colour based on certain values. I have used fabric js to fill the canvas with an image from URL
I had tried using hue rotation to fill the image but one image was red, another yellow and another one green. need to fill all 3 images with the same colour
var canvas = new fabric.Canvas('c');
fabric.Image.fromURL('leftspeedometer.png', function(img){
img.set('left', 70);
img.set('top', 100);
canvas.add(img);
});

MagickWand: transparent SVG part rendered as white background

I am trying to read SVG images using ImageMagick (6.8.8-7) and get valid transparent pixels.
The code below is working well with PNG format, but for SVG i can only have some white background.
So i tried to add MagickSetBackgroundColor, and MagickSetImageBackgroundColor with some merging layers but i still can't make it works.
Below an extract from the PoC:
MagickWandGenesis();
m_wand = NewMagickWand();
MagickReadImage(m_wand, file_name);
hasAlfa = MagickGetImageAlphaChannel(m_wand);
fprintf(stderr, "alpha channel detection: %d\n", hasAlfa);
if (hasAlfa == MagickTrue) {
PixelWand *color;
MagickWand *new_wand;
imagedata = malloc(w*h*4);
color = NewPixelWand();
PixelSetColor(color, "none");
MagickSetBackgroundColor(m_wand, color);
MagickSetImageBackgroundColor(m_wand, color);
new_wand = MagickMergeImageLayers(m_wand, MergeLayer);
DestroyMagickWand(m_wand);
m_wand = new_wand;
mrc = MagickExportImagePixels(m_wand, 0, 0, (size_t)w, (size_t)h, "RGBA", CharPixel, imagedata);
fprintf(stderr, "R:%d G:%d B:%d A:%d\nR:%d G:%d B:%d A:%d\n", imagedata[0], imagedata[1], imagedata[2], imagedata[3], imagedata[4], imagedata[5], imagedata[6], imagedata[7]);
}
Result using a PNG image:
size: 9 x 11
alpha channel detection: 1
R:0 G:0 B:0 A:0
R:0 G:0 B:0 A:0
Result using a SVG image:
size: 640 x 1000
alpha channel detection: 1
R:255 G:255 B:255 A:255
R:255 G:255 B:255 A:255
Any clues ?
I hope, I understood your question. To convert transparence from svg to png a pixel has to be defined as transparent.
You can define transparency in svg using "opacity", but in svg transparency is only given for next layer (not alpha-layer, that shines through all objects like in alpha).
If you want alpha transparency on an png, it's really the best way to define (i.e. white), and set that to alpha transperency

Gradient text fill using GD - PHP

I need a function that renders gradient on a text using GD
something like
function gradientText($text,$font,$color1,$color2)
{
..
}
I suggest you try to build that function based your own needs.
You will want to center the text vertically/horizontal, change font size, etc...
Start on this function by Christopher Kramer, code is also below this answer...
http://www.php.net/manual/en/function.imagefill.php#93920
then you can use imagettfbbox if you want to use custom font files.
http://www.php.net/manual/en/function.imagettfbbox.php
Here is a sample image I generated using those 2 functions.
Pasting Chris' code of gradient here for reference:
<?php
function gradient($w=100, $h=100, $c=array('#FFFFFF','#FF0000','#00FF00','#0000FF'), $hex=true) {
/*
Generates a gradient image
Author: Christopher Kramer
Parameters:
w: width in px
h: height in px
c: color-array with 4 elements:
$c[0]: top left color
$c[1]: top right color
$c[2]: bottom left color
$c[3]: bottom right color
if $hex is true (default), colors are hex-strings like '#FFFFFF' (NOT '#FFF')
if $hex is false, a color is an array of 3 elements which are the rgb-values, e.g.:
$c[0]=array(0,255,255);
*/
$im=imagecreatetruecolor($w,$h);
if($hex) { // convert hex-values to rgb
for($i=0;$i<=3;$i++) {
$c[$i]=hex2rgb($c[$i]);
}
}
$rgb=$c[0]; // start with top left color
for($x=0;$x<=$w;$x++) { // loop columns
for($y=0;$y<=$h;$y++) { // loop rows
// set pixel color
$col=imagecolorallocate($im,$rgb[0],$rgb[1],$rgb[2]);
imagesetpixel($im,$x-1,$y-1,$col);
// calculate new color
for($i=0;$i<=2;$i++) {
$rgb[$i]=
$c[0][$i]*(($w-$x)*($h-$y)/($w*$h)) +
$c[1][$i]*($x *($h-$y)/($w*$h)) +
$c[2][$i]*(($w-$x)*$y /($w*$h)) +
$c[3][$i]*($x *$y /($w*$h));
}
}
}
return $im;
}
function hex2rgb($hex)
{
$rgb[0]=hexdec(substr($hex,1,2));
$rgb[1]=hexdec(substr($hex,3,2));
$rgb[2]=hexdec(substr($hex,5,2));
return($rgb);
}
// usage example
$image=gradient(300, 300, array('#000000', '#FFFFFF', '#FF0000', '#0000FF'));
header('Content-type: image/png');
imagepng($image);
imagedestroy($image);
Using GD
http://planetozh.com/blog/my-projects/images-php-gd-gradient-fill/ offers a class to create a gradient with GD.
Gradient can be linear (horizontal or vertical), radial, rectangle, diamond. That's the same options you would find on Adobe Photoshop.
The class methods fill rectangular areas with a gradient, so you could achieve a rather great gradient effect with the following method:
create a gradient rectangle with this class
write your text in the specified font
mix them:
you could cut the gradient picture with the shape of the text picture
you could apply the gradient picture as a pattern for the text picture
Using ImageMagick
Instead to use GD, I would use ImageMagick.
See http://www.imagemagick.org/Usage/fonts/#gradient for a sample of how to use ImageMagick to achieve that, and http://www.imagemagick.org/Usage/canvas/#gradient for all the gradients options.

Resources