How can I get and set pixel data in XNA? - c#

I have a texture2D where I want to get the color of a specified pixel. Do something with it and put a new color in an other texture2D.
I will need to do this with all the pixels in the texture. How can I do this.
No pixel shader's please. It need to be in C#

The Texture2D class contains the GetData and SetData methods that should do exactly what you want.

I found my problem.
When I was trying to get the color of a pixel the Alpha value of the color was 0. This means that the color would be completely transparent. To solve it I just needed to change the Alpha value to 255.
I think that this happened because I am using an jpg file. Jpg file's do not support Alpha values.

Related

Giving a Material two different colors using a png Mask

I am trying the put a logo onto my object. I have a png image I want to use as a mask and two colors, one for the logo and one for the background.
So far I have managed to create two Materials for the same submesh, with one being the transparent image and the other being the background:
The color of the logo isn't changable though, as it is the image that is being displayed.
Is there a way to use the png as a mask, but change the color that is used?
Will I need to write a shader, or have I missed something in the material inspector?
Thanks in advance :)
Using two materials for the same submesh is a terrible idea, it has very low performance, it is not like having two passes in the same shader.
What you can do is creating a new shader that receives two textures:
One texture will have only 0 in the space for the logo and white in the other places
The other texture will be white in the logo and have 0 in the other pixels
Then you will have two color properties:
One will multiply with your "background" texture
The other will multiply with your "Logo" texture
Then Add both textures and assign them to the Albedo node.
You can do the same using a blend mask:
You will have a mask Map (basically it`s the Alpha channel) and put 0 in all places but where the logo pixels are.
Also you will need 2 colors properties.
Then use the blend node in the ShaderGraph to merge both textures.
Using the Blend mask you don't need the other two textures, only the mask and you can create maps in the ShaderGraph with the color properties.

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 to get colors of particular area of a BMP image in c#?

I want to get the colors of the center bottom part of a BMP image. How this can be done, I need some techniques.
Check out the Bitmap class. It has a GetPixel() method which takes X/Y coordinates and returns the color of that pixel.
http://msdn.microsoft.com/en-us/library/system.drawing.bitmap.getpixel%28v=VS.100%29.aspx
If this isn't enough, you'll have to describe more of what you are trying to accomplish.

Generate image with some transparent parts

Im trying to generate a small square 32 by 32 pixels with a 10 by 10 squareish transparent gap in the middle.
This is what I have so far:
private Image CreatePicture(){
// Create a new Bitmap object, 32 x 32 pixels in size
Bitmap canvas = new Bitmap(32,32,System.Drawing.Imaging.PixelFormat.Format16bppRgb555);
for(int i=10;i<21;i++){
for(int p=10;p<21;p++){
canvas.SetPixel(i,p,Color.Lime);
}
}
canvas.MakeTransparent(Color.Lime);
// return the picture
return canvas;
}
Its a it rough and not going to be the final "optimized version" its just a rough demo script. The problem is the returned image does not transparency instead its just a grey box :(.
Any help appreciated.
Michael
UPDATE:
I have updated the script with the PixelFormat set to an Alpha RGB format which it actually accepts without erroring on runtime. Now though if I remove the "canvas.MakeTransparent(Color.Lime);" line it shows a lime box in the middle with it it just shows a grey box the same colour as the grey background; so it seems as if transparency is being recognised just not implimenting the transparency!
private Bitmap CreatePicture(){
// Create a new Bitmap object, 50 x 50 pixels in size
Bitmap canvas = new Bitmap(82,82,System.Drawing.Imaging.PixelFormat.Format32bppArgb);
for(int i=10;i<71;i++){
for(int p=10;p<71;p++){
canvas.SetPixel(i,p,Color.Lime);
}
}
canvas.MakeTransparent(Color.Lime);
// return the picture
return canvas;
}
[...]Format16bppRgb555
Try to use some format with an alpha channel (...Rgba) here. Also, if the output will be an image later, make sure you use an image format like PNG that supports alpha channels.
It depends on how your going to use or display the image. PNG and Gif are the two file formats that support transparency.
For the bitmap setting of transparency, i used:
Graphics.FromImage(bitmap).FillRectangle(Brushes.Transparent, ...) to set the area I wanted as transparent, then I saved the file out as a PNG to get an image with transparency.
I also tried the MakeTransparent method and ran into problems and the Brushes.Transparent was the solution that worked for my situation.
Hope this gives you a direction to look into.
One last note, I create the Bitmap object with width and height only, I don't specify the pixel format or whatever that's called i.e. bitmap = New Bitmap(width, height);
You probably need to change the image format to Format16bppArgb1555.
Right now, you're using Format32bppArgb or Format16bppRgb555, which is 16 bits per pixel with no alpha information. Transparency requires an alpha channel or alpha bit to be present.
My don't you just SetPixel() to Color.Transparent? Instead of Color.Lime then MakeTransparent? And as others have noted; you need a pixel format with an alpha channel.
The MakeTransparent function does work. You have "grey patches" because your TransparencyKey is not correct. You'll find that if you set your TransparencyKey property to whatever color that grey patch is, the grey patches will indeed become transparent when your image is displayed. The default grey patch I got was labeled as "Control" in color, or basically the default Windows Form background color. Change your TransparencyKey to whatever your grey patch is (in my case Control) and your problem will be solved, no saving of files to the Hard Drive neccessary. Cheers mate.

Categories