ImageMagick advanced transparency control - c

I'm trying to put overlay (watermark) on base image. Assuming it looks like this:
opaque rectangle with "fully transparent" text on it. I.e. all pixels of text is transparent, as well as background under it.
1 way:
I do opacity 60% in Photoshop:
And just compose it in code:
MagickWand* overlay = NewMagickWand();
MagickReadImage(overlay, overlaypath);
MagickCompositeImage(wand, overlay, OverCompositeOp, 100, 100);
//all open/destroy code is omitted
And result is rectangle is semi-transparent while text is fully transparent:
So far so good, but I also wanted to regulate overlay opacity via application config, so I tried second way:
2 way:
100% in Photoshop (as on very first image in this post) and trying to set transparency via MagickWand:
MagickReadImage(overlay, overlaypath);
MagickSetImageOpacity(overlay, 0.6);
MagickCompositeImage(wand, overlay, OverCompositeOp, 100, 100);
Result is totally blank rectangle, semi-transparent though
It looks like MagickSetImageOpacity set up every pixel alpha channel to the same 0.6 value regardless of its current value. What I need is currentAlpha -= givenAlpha for every pixel of overlay wand. Is this possible without iterate every wand pixel by hand?

The correct way to achieve the currentAlpha -= givenAlpha would be to invoke a PixelIterator, and iterator through each pixel, and apply the givenAlpha with PixelSetAlpha. But for you needs, you may be able to apply the same effects with MagickColorizeImage.
PixelWand *pColorize = NewPixelWand();
PixelWand *pGivenAlpha = NewPixelWand();
double userOpacity = 0.6;
// May need to be adjusted to "white" or "black" depending on your mask
PixelSetColor(pColorize, "transparent");
PixelSetAlpha(pGivenAlpha, userOpacity);
MagickWand *overlay = NewMagickWand();
MagickReadImage(overlay, overlaypath);
MagickColorizeImage(overlay, pColorize, pGivenAlpha); // Apply opacity filter
MagickCompositeImage(wand, overlay, OverCompositeOp, 100, 100);

Related

CN1: Gradient with alpha channel

I would like to have a gradient which goes from black to transparent (not white). How can I achieve this?
From my attempt below I assume the gradient style color's alpha value is not considered:
gui_Footer.allStyles.apply {
backgroundType = Style.BACKGROUND_GRADIENT_LINEAR_VERTICAL
border = RoundRectBorder.create().topOnlyMode(true).cornerRadius(1f)
backgroundGradientEndColor = ColorUtil.BLACK
backgroundGradientStartColor = ColorUtil.argb(0, 255, 255, 255)
}
Gradients in Codename One ignore the alpha byte. While we could technically add support for alpha gradients it's not something that's planned at this time. You can probably generate such an image by manipulating the RGB data but it would be more efficient to just generate an RGB image of a gradient and draw it scaled.
Notice that this is generally the most efficient approach since the GPU works by drawing textures very efficiently. If an image is a power of 2 (e.g. 256x128 pixels) it can fit perfectly in a texture and it's drawn very fast. Much faster than our builtin gradients.

Detect Eye using HSV value in Open CV

I want to detect an Eye, I have some code where I can detect blue color object, so if I made changes(how I can?) then it would be possible for me to detect an eye. As the below color has its own specific range value so, if I specify the eye color HSV value then can I detect EYE with this method.
In this below code I am going to detect BLUE Color Object, please tell me that where I do changes in my code so that I could get EYE using Open CV.
IplImage* GetThresholdedImage(IplImage* img)
{
// Convert the image into an HSV image
IplImage* imgHSV = cvCreateImage(cvGetSize(img), 8, 3);
cvCvtColor(img, imgHSV, CV_BGR2HSV);
IplImage* imgThreshed = cvCreateImage(cvGetSize(img), 8, 1);
//For detecting BLUE color i have this HSV value,
cvInRangeS(imgHSV, cvScalar(112, 100, 100), cvScalar(124, 255, 255), imgThreshed);//this will not recognize the yellow color
cvReleaseImage(&imgHSV);
return imgThreshed;
}
Eye detection is much easier with Haar classifier.
link here
Such a simple method may work at extracting a blue object using some thresholding but even if it could be adapted using a different colour black? blue? green? Everyone has different eye colours. I don't see a non hacky method working for you using blob extraction like this based on a HSV threshold value. This method works well on large blocks of the same colour, i.e. removing a blue background.
Look more at shape, everyone has different coloured eyes but the shape is circular/ellipse ish. There are varients of the Hough Transform for detecting circles.
...the Hough transform has been extended to identifying positions of
arbitrary shapes, most commonly circles or ellipses.

Making a color completely transparent in OpenCV

I have a basic png file with two colors in it, green and magenta. What I'm looking to do is to take all the magenta pixels and make them transparent so that I can merge the image into another image.
An example would be if I have an image file of a 2D character on a magenta background. I would remove all the magenta in the background so that it's transparent. From there I would just take the image of the character and add it as a layer in another image so it looks like the character has been placed in an environment.
Thanks in advance.
That's the code i would use,
First, load your image :
IplImage *myImage;
myImage = cvLoadImage("/path/of/your/image.jpg");
Then use a mask like this to select the color, you should refer to the documentation. In the following, I want to select a blue (don't forget that in OpenCV images are in BGR format, therefore 125,0,0 is a blue (it corresponds to the lower bound) and 255,127,127 is blue with a certain tolerance and is the upper bound.
I chose lower and upper bound with a tolerance to take all the blue of your image, but you can select whatever you want...
cvInRangeS(image,
cvScalar(125.0, 0.0, 0.0),
cvScalar(255.0, 127.0, 127.0),
mask
);
Now we have selected the mask, let's inverse it (as we don't want to keep the mask, but to remove it)
cvNot(mask, mask);
And then copy your image with the mask,
IplImage *myImageWithTransparency; //You may need to initialize it before
cvCopy(myImage,myImageWithTransparency,mask);
Hope it could help,
Please refer to the OpenCVDocumentation for further information
Here it is
Julien,

How to set Transparency color for the output of RenderTargetBitmap?

I'm trying to save a Visual object, which has a transparent background, to a bitmap using RenderTargetBitmap...
public static RenderTargetBitmap RenderToBitmap(this Visual Source, int Width, int Height)
{
var Result = new RenderTargetBitmap(Width, Height, 96, 96, PixelFormats.Default);
Result.Render(Source);
return Result;
}
It works, but the transparent pixels are rendered with black color.
What the simplest way to change these transparent pixels to another color?
If you are saving the image as a JPG, transparent will appear black since JPG does not support transparent channel AFAIK. Possible solution: save as a PNG, or paint with a reasonable background color.
I haven't tested this, but in theory it should work.
Use the CopyPixels() method to extract all the pixel data from your RenderTargetBitmap to an array.
Then query the Alpha channel of all those pixels and where they are equal to 0x00 (fully transparent), set the color to whatever you'd like in the background. If you want to be more elegant, you'd have to do the "color math" to properly set the colors in semi-transparent pixels.
Finally, after you have an adjusted array of pixels, create a new BitmapSource from them.
To save that to disk, you'll probably have to create an Image in memory and set its Source to your new Bitmapsource and run your RenderToBitmap again.
Hope it helps.
EDIT:
After posting this, I had
another thought that may be easier.
If you take a clone or snapshot of the
visual element you're trying to save
and put it into a new in-memory panel
(such as a grid or a canvas), you
could simply change the background of
the panel to be the color you want.
Then you'd use the panel as your
source for RenderTargetBitmap.

Use another control as an opacity mask in WPF?

I'm trying to use the OpacityMask property combined with a VisualBrush so that when you drag an image over another control (such as another image, rectangle, or any other control), the part of the image that is over the second control has a different opacity. That is, the image has some non-zero base opacity, and any part of the image that is over another control has a different (again, non-zero) opacity.
Is this possible simply using VisualBrush and OpacityMask? Or is a more complex approach required?
Thanks!
Edit: I'm trying to make the image have some lower opacity (such as 0.5), and the part being dragged over the control have a higher opacity (such as 1.0). I originally left out this detail, which is important to the approach taken.
In addition to ima's answer, I have figured this out using an opacity mask. I use the following code hooked into the LayoutUpdated event for the image.
// Make a visual brush out of the masking control.
VisualBrush brush = new VisualBrush(maskingControl);
// Set desired opacity.
brush.Opacity = 1.0;
// Get the offset between the two controls.
Point offset = controlBeingMasked.TranslatePoint(new Point(0, 0), maskingControl);
// Determine the difference in scaling.
Point scale = new Point(maskingControl.ActualWidth / controlBeingMasked.ActualWidth,
maskingControl.ActualHeight / controlBeingMasked.ActualHeight);
TransformGroup group = new TransformGroup();
// Set the scale of the mask.
group.Children.Add(new ScaleTransform(scale.X, scale.Y, 0, 0));
// Translate the mask so that it always stays in place.
group.Children.Add(new TranslateTransform(-offset.X, -offset.Y));
// Rotate it by the reverse of the control, to keep it oriented correctly.
// (I am using a ScatterViewItem, which exposes an ActualOrientation property)
group.Children.Add(new RotateTransform(-controlBeingMasked.ActualOrientation, 0, 0));
brush.Transform = group;
controlBeingMasked.OpacityMask = brush;
If you want a desired base opacity, use two images; one that's always at the base opacity, and another that uses the opacity mask that sits on top of it. If you want the base opacity to be higher than the masked opacity, then it might be easier to use ima's approach.
One advantage of this solution as opposed to the maskless approach is that if the masking control moves, changes size, etc., this will automatically pick up the change without having to keep another control in sync with it.
Here's how it looks:
(source: yfrog.com)
No masks
Define visual brush for the control
Paint shape right on top of the control with that brush
Drag image between the shape and the control
Set opacity of the brush to achieve desired effect

Resources