C
C#2y ago
Whiteboy

✅ Process image using Emgu.CV, WPF app .NET 7.0

So i have to check if the image i upload is in Grayscale or not and based on it make further progress here is how i upload image:
private void BtnOpenNewFile_Click(object sender, RoutedEventArgs e)
{
var openFileDialog = new OpenFileDialog
{
Filter = "Image Files (*.bmp;*.jpg;*.jpeg;*.gif;*.png)|*.bmp;*.jpg;*.jpeg;*.gif;*.png"
};

if (openFileDialog.ShowDialog() == true)
{
Mat image = new Mat(openFileDialog.FileName, ImreadModes.Color);
string fileName = Path.GetFileNameWithoutExtension(openFileDialog.FileName);

ImageWindow imageWindow = new ImageWindow();

if (image.Height > 1000 || image.Width > 1000)
{
imageWindow.Height = 1000;
imageWindow.Width = 1000;
}
else
{
imageWindow.Height = image.Height + 200;
imageWindow.Width = image.Width + 50;
}

imageWindow.Title = fileName;
imageWindow.ImgMainImage.Source = image.ToBitmapSource();
imageWindow.Show();
}
}
private void BtnOpenNewFile_Click(object sender, RoutedEventArgs e)
{
var openFileDialog = new OpenFileDialog
{
Filter = "Image Files (*.bmp;*.jpg;*.jpeg;*.gif;*.png)|*.bmp;*.jpg;*.jpeg;*.gif;*.png"
};

if (openFileDialog.ShowDialog() == true)
{
Mat image = new Mat(openFileDialog.FileName, ImreadModes.Color);
string fileName = Path.GetFileNameWithoutExtension(openFileDialog.FileName);

ImageWindow imageWindow = new ImageWindow();

if (image.Height > 1000 || image.Width > 1000)
{
imageWindow.Height = 1000;
imageWindow.Width = 1000;
}
else
{
imageWindow.Height = image.Height + 200;
imageWindow.Width = image.Width + 50;
}

imageWindow.Title = fileName;
imageWindow.ImgMainImage.Source = image.ToBitmapSource();
imageWindow.Show();
}
}
and here is how i try to convert it but no matter if i upload gray image or not it says the image is already in grayscale, (i checked using ImageJ)
9 Replies
Whiteboy
WhiteboyOP2y ago
private void BtnConvert2Grey_Click(object sender, RoutedEventArgs e)
{
BitmapSource bitmapSource = (BitmapSource)ImgMainImage.Source;
int width = bitmapSource.PixelWidth;
int height = bitmapSource.PixelHeight;
var grayImage = new Image<Gray, byte>(width, height);

// Check if image is already grayscale
if (grayImage.NumberOfChannels == 1)
{
MessageBox.Show("It's already in grayscale");
return;
}

int stride = width * ((bitmapSource.Format.BitsPerPixel + 7) / 8);
byte[] pixels = new byte[height * stride];
bitmapSource.CopyPixels(pixels, stride, 0);

// Convert bitmap to gray image
grayImage.Bytes = pixels;

// Convert to grayscale
grayImage = grayImage.Convert<Gray, byte>();

// Set converted image as source of ImgMainImage
ImgMainImage.Source = grayImage.ToBitmapSource();

MessageBox.Show("Image converted to grayscale");
}
private void BtnConvert2Grey_Click(object sender, RoutedEventArgs e)
{
BitmapSource bitmapSource = (BitmapSource)ImgMainImage.Source;
int width = bitmapSource.PixelWidth;
int height = bitmapSource.PixelHeight;
var grayImage = new Image<Gray, byte>(width, height);

// Check if image is already grayscale
if (grayImage.NumberOfChannels == 1)
{
MessageBox.Show("It's already in grayscale");
return;
}

int stride = width * ((bitmapSource.Format.BitsPerPixel + 7) / 8);
byte[] pixels = new byte[height * stride];
bitmapSource.CopyPixels(pixels, stride, 0);

// Convert bitmap to gray image
grayImage.Bytes = pixels;

// Convert to grayscale
grayImage = grayImage.Convert<Gray, byte>();

// Set converted image as source of ImgMainImage
ImgMainImage.Source = grayImage.ToBitmapSource();

MessageBox.Show("Image converted to grayscale");
}
Anton
Anton2y ago
gray means the values in all color channels are equal you can just go through all pixels' colors and check if they're equal I'm not wrong, right?
Whiteboy
WhiteboyOP2y ago
@AntonC i can but it's slow as shit i hsould be able to use Emgu.CV lib to do it
Anton
Anton2y ago
I don't know what that is
Anton
Anton2y ago
Write a simple parallel program using Parallel.ForEach
In this article, learn how to enable data parallelism in .NET. Write a Parallel.ForEach loop over any IEnumerable or IEnumerable data source.
Anton
Anton2y ago
if it's still ot fast enough, one could use simd, but I've never needed to use this https://learn.microsoft.com/en-us/dotnet/standard/simd
SIMD-accelerated types in .NET
This article describes SIMD-enable types in .NET and demonstrates how to use hardware SIMD operations in C# and .NET.
Anton
Anton2y ago
But who knows, maybe that library supports this stuff in a simpler way
Whiteboy
WhiteboyOP2y ago
it does and it's mandatory i use it for this method but ty for trying to help but i think im doing wrong type of image by doing bitmapSource bacuse it's in system.drawing and i need to use type from emgu
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server