How to get the red component from an image C# - c#

How to get the red component from an image, in C# (windows forms).
I want to get the Red component in a new matrix.
I tried something like this with a bmp:
byte[,] MatrixImage= new byte[256, 256]; // is my matrix where every pixel of my image is stored
byte[,] MatrixRed= new byte[256, 256];
Bitmap bmp = new Bitmap(256, 256);
for (int i = 0; i < 256; i++)
{
for (int j = 0; j < 256; j++)
{
var red = Color.FromArgb(MatrixImage[i, j], 0, 0);
bmp.SetPixel(i, j, red);
var getRedPixel = bmp.GetPixel(i, j);
MatrixRed[i, j] = (byte)getRedPixel;
}
}

GetPixel Returns a Color object so you're probably looking for this :
MatrixRed[i, j] = (byte)(getRedPixel.R);

Related

How to convert gray image into colormap

enter image description here
i have a color feet image, it transform to grayscale, and then to colormap (jet)
i did it in emgucv, but the color doesnt look so diferent, i need that the result image like the second (only 4-6 color, red, orange*, yellow, green, blue, purple*)
it is possible?
i already try on emgucv and read something gdi+, but i dont understand, i just begin to program
i try this but the color are the same, just more pixel, no gradient
int divideWith = 48;
byte[] table = new byte[256];
for (int i = 0; i < 256; i++)
{
table[i] = (byte)(divideWith * (i / divideWith));
}
Mat mat = ConvertBitmapToMat(rgb);
//_Stopwatch.Reset();
int rows = mat.Rows;
int cols = mat.Cols;
byte[,,] data = mat.ToImage<Bgr, byte>().Data;
byte[,,] resData = new byte[rows, cols, 3];
Parallel.For(0, rows, i =>
{
for (int j = 0; j < cols; j++)
{
resData[i, j, 0] = table[data[i, j, 0]];
resData[i, j, 1] = table[data[i, j, 1]];
resData[i, j, 2] = table[data[i, j, 2]];
}
});
Image<Bgr, byte> resImg = new Image<Bgr, byte>(resData);
Bitmap im = resImg.ToBitmap();
pictureBox2.Image = im;

Return raw image bits in a BMP file

I have C# dll that I am using in Matlab to return the raw 8-bit grey scale bits that are stored in a BMP file. (Yes, I know Matlab can read the BMP, but this is a test, for a real system where the data will come from a video frame grabber).
Right now, the code below is returning 1078 bits of header information, that needs to be stripped, and it is also flipping the axis of the image (it comes out in mirror image). What is the best way to fix this (least lines, performance is not not a concern).
image = Image.FromFile(this.imagePath);
ImageConverter converter = new ImageConverter();
data = (byte[])converter.ConvertTo(image, typeof(byte[]));
Frame f = new Frame(data);
public class Frame
{
public byte[,] frameData = new byte[1024, 1024];
public Frame(byte[] data)
{
Buffer.BlockCopy(data, 0, frameData, 0, 1024 * 1024 * sizeof(byte));
}
}
Do you mean something like this:
BitArray bits = new BitArray(returnBytes);
BitArray flippedBits = new BitArray(bits);
for (int i = 0; i < bits.Length; i += width) {
for (int j = 0, k = width - 1; j < width; ++j, --k) {
flippedBits[i + j] = bits[i + k];
}
}
If you need to mirror picture upside-down, use this code:
BitArray bits = new BitArray(returnBytes);
BitArray flippedBits = new BitArray(bits);
for (int i = 0, j = bits.Length - width; i < bits.Length; i += width, j -= width) {
for (int k = 0; k < width; ++k) {
flippedBits[i + k] = bits[j + k];
}
}
Source:
Algorithm to vertically flip a bitmap in a byte array

How to draw an image?

How can I create a 256x256 color space image using graphics draw? Currently I'm using pointers to loop through each pixel location and setting it. Blue goes from 0...255 on X and Green goes from 0...255 on Y. The image is initialized as so.
Bitmap image = new Bitmap(256, 256);
imageData = image.LockBits(new Rectangle(0, 0, 256, 256),
ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
for (int row = 0; row < 256; row++)
{
byte* ptr = (byte*)imageData.Scan0 + (row * 768);
for (int col = 0; col < 256; col++)
{
ptr[col * 3] = (byte)col;
ptr[col * 3 + 1] = (byte)(255 - row);
ptr[col * 3 + 2] = 0;
}
}
I have a slider that goes 0...255 on Red. On each scroll, it goes through this loop and updates the image.
for (int row = 0; row < 256; row++)
{
byte* ptr = (byte*)imageData.Scan0 + (row * 768);
for (int col = 0; col < 256; col++)
{
ptr[col * 3 + 2] = (byte)trackBar1.Value;
}
}
I've figured out how to use a ColorMatrix instead for the scrolling part but how can I initialize the image without using pointers or SetPixel?
First of all, add PictureBox control to the Form.
Then, this code will assign different color to each pixel based on the index in the loop and assign the image to the control:
Bitmap image = new Bitmap(pictureBox3.Width, pictureBox3.Height);
SolidBrush brush = new SolidBrush(Color.Empty);
using (Graphics g = Graphics.FromImage(image))
{
for (int x = 0; x < image.Width; x++)
{
for (int y = 0; y < image.Height; y++)
{
brush.Color = Color.FromArgb(x, y, 0);
g.FillRectangle(brush, x, y, 1, 1);
}
}
}
pictureBox3.Image = image;
For some reason there's no SetPixel or DrawPixel like I expected, but the FillRectangle will do exactly the same thing when you give it 1x1 dimensions to fill.
Note that it will work fine for small images but the larger the image the slower it will be.
If you don't want to use pointers or SetPixel you'll have to build the gradient in a byte array and then Marshal.Copy it to your bitmap:
int[] b = new int[256*256];
for (int i = 0; i < 256; i++)
for (int j = 0; j < 256; j++)
b[i * 256 + j] = j|i << 8;
Bitmap bmp = new Bitmap(256, 256, PixelFormat.Format32bppRgb);
BitmapData bits = bmp.LockBits(new Rectangle(0, 0, 256, 256),
ImageLockMode.ReadWrite, PixelFormat.Format32bppRgb);
Marshal.Copy(b, 0, bits.Scan0, b.Length);
This would create you a white image of 256x256
Bitmap image = new Bitmap(256, 256);
using (Graphics g = Graphics.FromImage(image)){
g.FillRectangle(Brushes.White, 0, 0, 256, 256);
}

Float array to Image

I have a float array representing a grayscale picture that I want to convert to an image in C#. How do I go about doing so?
Bitmap newBitmap = new Bitmap(original.Width, original.Height);
for (int j = 0; j < original.Height; j++)
{
for (int i = 0; i < original.Width; i++)
{
Color newColor = Color.FromArgb((int)grayScale[i + j * original.Width], (int)grayScale[i + j * original.Width], (int)grayScale[i + j * original.Width]);
newBitmap.SetPixel(i, j, newColor);
}
}
Image img = (Image)newBitmap;
Microsoft forum for converting
Example in codeproject

Image Processing in C# - A smart solution?

I have an image with letters in it, the letters are in two colors black and blue, I want to read the blue colored letters from the image.
Can anyone suggest me a method to do this in C#. Iam studying GDI+,but still didn't get any logic to develop this program..
I tried OCRing it, but the issue with common OCRs is that they dont recognize the color difference.
I only want to read the Blue characters....
Any guidance is highly appreciated.
Try this one ;) But that's unsafe code.
void RedAndBlue()
{
OpenFileDialog ofd;
int imageHeight, imageWidth;
if (ofd.ShowDialog() == DialogResult.OK)
{
Image tmp = Image.FromFile(ofd.FileName);
imageHeight = tmp.Height;
imageWidth = tmp.Width;
}
else
{
// error
}
int[,] bluePixelArray = new int[imageWidth, imageHeight];
int[,] redPixelArray = new int[imageWidth, imageHeight];
Rectangle rect = new Rectangle(0, 0, tmp.Width, tmp.Height);
Bitmap temp = new Bitmap(tmp);
BitmapData bmpData = temp.LockBits(rect, ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
int remain = bmpData.Stride - bmpData.Width * 3;
unsafe
{
byte* ptr = (byte*)bmpData.Scan0;
for (int j = 0; j < bmpData.Height; j++)
{
for (int i = 0; i < bmpData.Width; i++)
{
bluePixelArray[i, j] = ptr[0];
redPixelArray[i, j] = ptr[2];
ptr += 3;
}
ptr += remain;
}
}
temp.UnlockBits(bmpData);
temp.Dispose();
}
Modify the color of the image to gray scaled then use OCR
public Bitmap MakeGrayscale(Bitmap original)
{
//make an empty bitmap the same size as original
Bitmap newBitmap = new Bitmap(original.Width, original.Height);
for (int i = 0; i < original.Width; i++)
{
for (int j = 0; j < original.Height; j++)
{
//get the pixel from the original image
Color originalColor = original.GetPixel(i, j);
//create the grayscale version of the pixel
int grayScale = (int)((originalColor.R * .3) + (originalColor.G * .59)
+ (originalColor.B * .11));
//create the color object
Color newColor = Color.FromArgb(grayScale, grayScale, grayScale);
//set the new image's pixel to the grayscale version
newBitmap.SetPixel(i, j, newColor);
}
}
return newBitmap;
}
You could probably modify the palette to have only black and white on the image

Categories

Resources