How to convert gray image into colormap - c#

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;

Related

How to get the red component from an image 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);

Crop an image by curve split lines for puzzle in winforms

I'm trying to crop an image in windows forms (C#). I did it with straight corners. But I want to split it with curve lines.
A Single image is split into a 3x3 image and then placed in 9 different pictureBoxes. Here is my current code:
Image img = pictureBox1.Image;
int widththird = (int)((double)img.Width / 3.0 + 0.5);
int heightthird = (int)((double)img.Height / 3.0 + 0.5);
Bitmap[,] bmps = new Bitmap[3, 3];
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
bmps[i, j] = new Bitmap(widththird, heightthird);
Graphics g = Graphics.FromImage(bmps[i,j]);
g.DrawImage(img, new Rectangle(0, 0, widththird, heightthird), new
Rectangle(j * widththird, i * heightthird, widththird, heightthird),
GraphicsUnit.Pixel);
g.Dispose();
}
}
pictureBox2.Image = bmps[0, 0];
pictureBox3.Image = bmps[0, 1];
pictureBox4.Image = bmps[0, 2];
pictureBox5.Image = bmps[1, 0];
pictureBox6.Image = bmps[1, 1];
pictureBox7.Image = bmps[1, 2];
pictureBox8.Image = bmps[2, 0];
pictureBox9.Image = bmps[2, 1];
pictureBox10.Image = bmps[2, 2];
Q: How can I can crop/split the image with curves and export the pieces?
My output:
I want it cropped like this:

Extract RGB information from image into an array

I must save a bitmap file, and then access the rgb of each pixel and save the decimal code of each color of pixel (meaning red, green and blue) in a separate matrix (array).
For hidding textfile in bmp lmage l need to save each rgb code in seperate matrix...lm looking for effiecient way,this is my code
This code will be run,but l cant show the result of each matrix in text of lable.how can I see the output of each matrix?
Bitmap bmp1 = new Bitmap(#"D:a.jpg");
pictureBox1.Image = bmp1;
Color col = new Color();
int w = Int32.Parse(bmp1.Width.ToString());
int h = Int32.Parse(bmp1.Height.ToString());
int[,] redstr = new int[w,h];
int[,] greenstr = new int[w, h];
int[,] bluestr = new int[w, h];
int red = 0, green = 0, blue = 0;
for (int i = 0; i < w; i++)
{
for (int j = 0; j < h; j++)
{
col = bmp1.GetPixel(i, j);
red = col.R;
green = col.G;
blue = col.B;
redstr[i, j] = red;
greenstr[i, j] = green;
bluestr[i, j] = blue;
}
}
From the following page ... https://msdn.microsoft.com/en-us/library/system.drawing.bitmap.getpixel.aspx
You can use something like this to access each pixel of an image ...
private void GetPixel_Example(PaintEventArgs e)
{
// Create a Bitmap object from an image file.
Bitmap myBitmap = new Bitmap("YOURFILENAME.jpg");
// Get the color of a pixel within myBitmap.
Color pixelColor = myBitmap.GetPixel(50, 50);
}
then you can check pixelColour for your RGB components.
Evidently you will need to create a 2D array (matrix) with the same dimensions as your image. And if you need to store the separate RGB components, then you will need a 3D array.

Create Bitmap from double two dimentional array

I've a two-dimensional double[,] rawImage array representing a gray level image with each element in the array has a rational value from 0 ~ 1 , and I need
to convert it to Bitmap image, I've used the following code:
private Bitmap ToBitmap(double[,] rawImage)
{
int width = rawImage.GetLength(1);
int height = rawImage.GetLength(0);
Bitmap Image= new Bitmap(width, height);
for (int i = 0; i < height; i++)
for (int j = 0; j < YSize; j++)
{
double color = rawImage[j, i];
int rgb = color * 255;
Image.SetPixel(i, j, rgb , rgb , rgb);
}
return Image;
}
but it seems to be so slow.
I don't know if there is a way to do the above work using pointers of short data type.
How can I write a faster code using pointers to handle this function ?
This should be enough for you. The example is written according to this source code.
private unsafe Bitmap ToBitmap(double[,] rawImage)
{
int width = rawImage.GetLength(1);
int height = rawImage.GetLength(0);
Bitmap Image = new Bitmap(width, height);
BitmapData bitmapData = Image.LockBits(
new Rectangle(0, 0, width, height),
ImageLockMode.ReadWrite,
PixelFormat.Format32bppArgb
);
ColorARGB* startingPosition = (ColorARGB*) bitmapData.Scan0;
for (int i = 0; i < height; i++)
for (int j = 0; j < width; j++)
{
double color = rawImage[i, j];
byte rgb = (byte)(color * 255);
ColorARGB* position = startingPosition + j + i * width;
position->A = 255;
position->R = rgb;
position->G = rgb;
position->B = rgb;
}
Image.UnlockBits(bitmapData);
return Image;
}
public struct ColorARGB
{
public byte B;
public byte G;
public byte R;
public byte A;
public ColorARGB(Color color)
{
A = color.A;
R = color.R;
G = color.G;
B = color.B;
}
public ColorARGB(byte a, byte r, byte g, byte b)
{
A = a;
R = r;
G = g;
B = b;
}
public Color ToColor()
{
return Color.FromArgb(A, R, G, B);
}
}

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