Calculate source RGBA value from overlay - c#

The title looks a bit weird, so here is a sample:
You will probably be familiar with the Windows 7 Explorer.
When you select something with the pressed left mouse button, you get a blue half transparent rectangle.
So the question is now:
It is possible to calculate the source RGBA color that was used to draw this rectangle?
The only things I know is the RGB value from the background and the RGB value with the blue overlay. Here is a screenshot from what I mean:

No, it ist not possible from a single image.
You need the target data over a black and a white image (best case).
After that, you need to use the first forumular in this article:
http://en.wikipedia.org/wiki/Alpha_compositing

Related

Emgu GetAverage Color of black color of Image

I am new at Emgu cv. My target is to compare two photos which are the same photo but the brightness is slightly different and gets the dark color or dark spot percentage of ROI.
I saw GetAverage method but how can I get color percentage of image by specify color. eg black is 80%, white 20%.
What is the mask parameter in the GetAverage method?
I read the documentation but I don't understand.
My idea is I will change both photos to grayscale and set ROI then get the average value. I don't know its the correct way to get my target.
So How can I done this?
Update
Below are ROI images of grayscale.
Left photo average intensity: 37.4879
Right photo average intensity: 40.9773
I add some scratch to the right photo.
Left photo average intensity: 37.4879
Right photo average intensity:
40.7638
Why scratch photo intensity is lower than without scratch photo. By right, It should be greater right? because it have more gray color right? Why?
Or
I added scratch, it means more black, So gray color reduction?
What is the mask parameter in the GetAverage method?
The mask parameter in the overloaded GetAverage(mask) allow you to find the average color of the masked area only according to the official documentation.
Regarding the concept of mask (which is related to the concept of ROI), it allows you to analyze or elaborate (depending on the context) only the location of the image where the mask is non-zero. I invite you to deepen on this here. If you find the documentation not clear enough, try to understand how masks works in Photoshop, the concept is the same and surely you will find many more explanations and much more intuitive.
Why scratch photo intensity is lower than without scratch photo. By
right, It should be greater right? because it have more gray color
right? Why?
No, it shouldn't be grather. Here as indicated by the documentation, GetAverage() returns to you the average color with the structure you originale image have (TColor), Gray (grayscale) in this case.
As you can see here the grayscale value for darker color tents to 0, on the contrary the white corresponds to 255.
So adding scratch implies that your image darkens.
If you try apply GetAverage() to an image with a different color type (Bgr, Bgra, Hsv, Hls, ...), it will return you the average as color of that type.
Last thing,
I saw GetAverage method but how can I get color percentage of image by
specify color. eg black is 80%, white 20%.
you cannot with this approach. As said above GetAverage() returns to you a Gray color. The only thing I can suggest is that you manually convert the result obtained into a percentage of color with something like this:
private double ConvertToPercentage(double valueToConvert, int rangeStartAt, int rangeEndAt) {
return valueToConvert/(rangeEndAt - rangeStartAt) * 100;
}
Obviously this piece of code only works with single values colors (not rgb for example, which has 3 values). You can very easily re-implement something like this in case you need it.

SharpDX and setting one color as transparent (directx9)

I have a C# capture application that captures screenshot of an application and draws it to another window. Now I would like to set one color (or range) of the screenshot to be transparent and then draw it like that.
I am drawing the screenshot as sprite to 3d (directx9) surface using SharpDX.
How can I do this transparency?
When converting your screenshot to a texture, your colors should be defined by four values R,G,B,A. Alpha channel is responsible for transparency, so you should check if a color has a value / range that you want to cut out and change it's alpha value to 0.
It was a bit odd situation. My alpha modified pictures did not show up when draw:ing.
It was because I did not specify alphablend mode in .Begin-statement.
And I did not find any other way to do it than going through the whole bitmap and changing the alpha value one pixel at the time.

How can I draw legible text on a bitmap (Winforms)?

I've written a photo viewer, and I want to superimpose text over the photo. I want the font or font color to make the text as legible as possible on top of the bitmap, no matter what the underlying bitmap looks like.
My current thinking is to take the region of the bitmap where the text will appear, and make some kind of "overall color" calculation for that area, and then set the font color to be something correspondingly contrasting.
However, this math is way over my head. Has anybody seen a method for making this type of "what's the average color of all of these pixels" calculation? Or is that not even the best approach?
EDIT: I'm moving the second portion of this to another question.
You can use this to calculate average color of a region of bitmap:
How to calculate the average rgb color values of a bitmap
Do you store your image as a Bitmap?
You can also draw an outlined text. For example, white text with black outline. This will make text visible on most of backgrounds:
How to Drawing Text with Outline onto Images?

How can I set an image to have a transparent background in C# without using Bitmap.MakeTransparent()?

I want to set an image to have a transparent background, but I do not want to replace all pixels of a specific colour with transparency.
To be more specific, the image is a thumbnail image for a folder, obtained via IShellItemImageFactory.GetImage. This gives me a Bitmap, as displayed in Windows Explorer thumbnail view, but the background is solid white.
I can use Bitmap.MakeTransparent on it, and that will work in most cases, but in any cases where the thumbnail image contains white itself (for example, a folder that contains images, which include white colours).
Incidently, this is the first time in over 10 years as a developer that, after googling my question, I have not found an answer anywhere, and I've actually had to ask it myself. (I think that means I just levelled up! Yippee, I am now a level 2 developer...)
Use flood-fill algorithm to fill pixels of the same color from the OUTSIDE as you need it. It is something similar to magic wand in photoshop.
http://en.wikipedia.org/wiki/Flood_fill
What I would do is flood-fill with some obscure color (Magenta always does it for me), then replace that color with transparent (I don't know if flood filling with transparent pixels is feasible).
So what you're getting from IShellItemImageFactory.GetImage is a composite image that contains the original image on a white background? I suspect the white background is there if the image doesn't have the same aspect ratio as the thumbnail size. For example, if you ask for a 96x96 thumbnail of a 640x480 image, there's going to be some white space at top and bottom.
If that's the case, you have a problem. You can't differentiate between white pixels that are contained in the image, and white pixels that are added by GetImage.
There are a few things you could do. You could load the image and resize it yourself. That's probably the easiest. You'd want to maintain your own thumbnail cache then.
Or you could examine the image returned by GetImage to look for white space on the sides. Basically, if every pixel on a row (or column) is white, then make that row (or column) transparent. It's a little more complicated than that (the NBA logo, for example). But that's essentially what you'd want to do.

How to resolve anti-aliasing when changing color of an image in C#?

I am processing images to change their color from black to red, blue, green etc based on the requirement. I use SetPixel methods to change color of each pixel of the image from black to say red.
It works mostly fine except the borders and some curves within the image. Let's say I've circled image filled with black color. Circled image color is changed but still when I zoom, I can see blackish dots around border which is not completely replaced with red color. I tried to dig around and found that it has something to do with anti-aliasing.
Has anything faced similar problem or have thoughts/suggestions on how to fix this issue?
Many thanks in advnace for your help!
Regards,
Tanush
It can be related with anti-aliasing. Anti-aliasing essence is that the more pixel is closer to the edge (boundary of something) the more pixel color is blended with background color (or we can say that it is more 'transparent').
So the problem may be that you need not only to replace source color to destination color, but also pixels which were blended from source color to background color.
To achieve this you need:
1) Run edge detection algorithm of some kind - it may be simple or advanced as you want.
2) If pixel is near edge and pixel is near other pixel of your source color, then calculate it's opacity (1-transparency) factor- which will be
opacity = (pixel_color-background_color)/(source_color-background_color)
3) Now calculate your color to which you must replace current anti-aliased pixel:
new_color = background_color * (1-opacity) + opacity * target_color
And put this new_color instead of antialiased pixel.
In summary:
You need to detect antialiased pixels and replace them with your version of antialiased pixels.
Hardest part of algorithm is detection of antialiased pixels - because you can't be sure that you found all edge pixels with 100% probability. Also you can't be sure was pixel antialiased or was just made initially of such color). Because of this you may get some color noise in final product. But in any case it should be better than just sit and wait :)
good luck

Categories