When i read image with GetPixel() color value is always black. My image is a png image.
I tried convert the image to bitmap before, but don't had success.
I believe which the problem isn't my code, because whether I open png image in Paint and only save it. The code read image correctly.
I load image like bellow
myImage = new Bitmap(fileName);
I need read image here
private void LoadImageMap(Bitmap value){
for (int col = 0; col < value.Width; col++)
{
for (int row = 0; row < value.Height; row++)
{
var color = value.GetPixel(col, row);
var color is black, always.
Sample of the image...
PNG image there is transparency and when pixel is full transparency GetPixel() result with zero value to RGB colors.
Then my code needed of the one if to validate this case.
the solution was like bellow:
var color = value.GetPixel(x, y);
if(color.A.Equals(0))
{
color = Color.White;
}
PNG use ARGB colors where A represent the alpha channel and whether alpha is zero this pixel has full transparence.
Since you passed in the bitmap to your code, you need to actually use that variable when calling GetPixel. Also, ref keyword is not needed in this case.
private void LoadImageMap(Bitmap value)
{
for (int col = 0; col < value.Width; col++)
{
for (int row = 0; row < value.Height; row++)
{
var color = value.GetPixel(col, row);
}
}
}
Related
I'm currently facing a problem showing a transparent image in a Xamarin.Forms Image view.
The image is retrieved from the gallery, and converted to PNG format.
Pixels are iterated and with some of them, their alpha value is adjusted.
Bitmap is converted to SKBitmapImageSource and shown in an Image view.
Result (top), and original (bottom), taken on Android:
Screenshot
The goal is to show the Image with a transparent background, but I can't get it to work. It keeps showing with a black background. Loading a transparent PNG file from internet works, so something in the process of conversion or image processing must go wrong.
Image retrieval and conversion:
SKBitmap source = SKBitmap.Decode(file.GetStream());
SKData data = SKImage.FromBitmap(source).Encode(SKEncodedImageFormat.Png, 100);
SKBitmap converted = SKBitmap.Decode(data);
SKBitmap result = ImageProcessor.AddTransparency(converted, 0.7f);
Transparency added:
public static SKBitmap AddTransparency(SKBitmap bitmapSource, float treshold)
{
if (bitmapSource == null)
{
throw new ArgumentNullException(nameof(bitmapSource), $"{nameof(bitmapSource)} is null.");
}
var bitmapTarget = bitmapSource.Copy();
// Calculate the treshold as a number between 0 and 255
int value = (int)(255 * treshold);
// loop trough every pixel
int width = bitmapTarget.Width;
int height = bitmapTarget.Height;
for (int row = 0; row < height; row++)
{
for (int col = 0; col < width; col++)
{
var color = bitmapTarget.GetPixel(col, row);
if (color.Red > value && color.Green > value && color.Blue > value)
{
bitmapTarget.SetPixel(col, row, color.WithAlpha(0x00));
}
}
}
return bitmapTarget;
}
Conversion to imagesource:
return SKBitmapImageSource.FromStream(SKImage.FromBitmap((SKBitmap)value).Encode().AsStream);
The problem is the AlphaType being set incorrectly. For the way you're doing the alpha conversion, the AlphaType should be set to AlphaType.Premul
Since it's a readonly property, copy the bitmap to a new one and set the correct alpha type
I have a colour image of type Image<Hsv, Byte>, and another image of type Image<Gray, Byte> of the same size that is all black with some all-white shapes. From the black and white image, I found the contours of the shapes using findContours(). What I want is to create a new image or modify the original colour image I have to show only what corresponds to inside the contours, with everything else being transparent, without having to check pixel by pixel values of the two images (did this, it takes too long). Any possible way to do this?
For example, I have the original image, the black and white image, and the final product I need.
I'm completely new to emgucv, so, I'm not saying this is the best approach; but it seems to work.
Create a new draw surface
Draw the original image
Change the white pixels in the mask image to transparent pixels
Draw the transparent mask on top of the original image
The result image looks like your desired outcome.
void Main()
{
var path = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.Desktop),
"images");
var original = new Image<Bgr, Byte>(Path.Combine(path, "vz7Oo1W.png"));
var mask = new Image<Bgr, Byte>(Path.Combine(path, "vIQUvUU.png"));
var bitmap = new Bitmap(original.Width, original.Height);
using (Graphics g = Graphics.FromImage(bitmap))
{
g.DrawImage(original.Bitmap, 0, 0);
g.DrawImage(MakeTransparent(mask.Bitmap), 0, 0);
}
bitmap.Save(Path.Combine(path, "new.png"));
}
public static Bitmap MakeTransparent(Bitmap image)
{
Bitmap b = new Bitmap(image);
var tolerance = 10;
for (int i = b.Size.Width - 1; i >= 0; i--)
{
for (int j = b.Size.Height - 1; j >= 0; j--)
{
var col = b.GetPixel(i, j);
col.Dump();
if (255 - col.R < tolerance &&
255 - col.G < tolerance &&
255 - col.B < tolerance)
{
b.SetPixel(i, j, Color.Transparent);
}
}
}
return b;
}
I have an application that creates an image from a text inputed by the user. In that image, I want to detect where is the beginning of the letters in the top so I can stamp my brand's logo and crop image accordingly.
This is what I tried so far, but can't get it to work...
Bitmap myBitmap = new Bitmap(Image.FromFile(#"C:\Users\me\Desktop\test_textimage.jpg"));
for (int j = 0; j < 2000; j++) // loops Y axis
{
for (int i = 0; i < 3500; i++) // loops X axis
{
System.Drawing.Color color = myBitmap.GetPixel(i, j);
if (color == System.Drawing.Color.Black)
{ // breakpoint here... it never hits...
}
}
}
I am 100% sure the image is black font into white background. Tested into photoshop.
Can anyone help? Thanks.
I'm doing a skin detection method in c# using EmguCV. For skin detection I'm referring this article. I'm new in EmguCV. I just want to know how to get or set every pixel value of image that is capturing via webcam. If skin pixel matched it become white else black. I just want RGB value of pixel without degrading the performance of application.
to get or set every pixel value of image you do it easily as following
Image<Bgr, Byte> img = ....
for (i = 0; i < img.Height; i++)
{
for (k = 0; k < img.Width; k++)
{
// Get
// Color ( R, G, B, alpha)
Color c = img[i, k];
// Set
img[i,k] = new Bgr();
}
}
it will be write inplace
Hi all I have written a sample code to find out the black color pixels from an image, now I would like to save all those pixels to an array and would like to save that particular image with the save pixels can some one help me
Assume my image that I am getting the pixels is as follows
I will read all the black pixels and would like to save them, from that I would like to re-image the Fallout with transparent background. This is what I have written
for (int i = 0; i < b.Width; i++)
{
for (int j = 0; j < b.Height; j++)
{
Color pixelColor = b.GetPixel(i, j);
Response.Write("The color is " + pixelColor);
if (pixelColor.ToArgb() == Color.Black.ToArgb())
{
//Will get black color here
}
}
}
Can some one help me out for the remaining.
Create a second bitmap with the same size
Bitmap newBitmap = new Bitmap(oldBitmap.Width, oldBitmap.Height);
You can compare the colors directly
if (pixeColor == Color.Black) {
Inside the if { }
newBitmap.SetPixel(...);
That's it, but if you just wan't a transparent version, you can use Bitmap.MakeTransparent(Color);
PS: GetPixel() and SetPixel() are extremely slow, try using unsafe bitmap access