How to set pixels from original image to a new image? - c#

usage
public UserControlTester()
{
InitializeComponent();
GetSetPixelsRandom();
}
code
private void GetSetPixelsRandom()
{
Bitmap img = new Bitmap(#"d:\\downloaded images\\test.gif");
for (int i = 0; i < img.Width; i++)
{
for (int j = 0; j < img.Height; j++)
{
Color pixel = img.GetPixel(i, j);
pixels.Add(pixel);
}
}
Bitmap img1 = new Bitmap(512,512);
for (int i = 0; i < img1.Width; i++)
{
for (int j = 0; j < img1.Height; j++)
{
for (int c = 0; c < pixels.Count; c++)
{
img1.SetPixel(i, j, pixels[c]);
}
}
}
img1.Save(#"d:\\downloaded images\\test3.gif");
}
Not sure if this is the right way. the inner loop with the variable c take a lot of time because there are more then 290,000 pixels.

Assuming you want to resize the image, this can be done easily. The Bitmap constructor has rescaling build in:
Bitmap img = new Bitmap(#"d:\\downloaded images\\test.gif");
Bitmap img1 = new Bitmap(img, new Size(512, 512));
img1.Save(#"d:\\downloaded images\\test3.gif");
If you do not want to resize but indeed take random pixels take a look at what at MickyD mentioned.
Note that in your provided example every pixel in the new image is effectively set to the last pixel in the pixels list because you loop over every pixel in pixels for every pixel in the new image.

Related

How to Mirror An Image [duplicate]

This question already has answers here:
How to mirror an image using array
(2 answers)
Closed 7 years ago.
This is my original image,
I want to mirror it and have it appear like this,
I want to mirror my image so that the original and the mirrored image appear side by side. The only thing is that I don't know how to extend the range of my original image in order to add its mirrored image right beside it.
Using my code (below) I have managed to do create the mirrored image, only I can't make both the original and the mirrored appear side by side.
My code,
int Height = TransformedPic.GetLength(0);
int Width = TransformedPic.GetLength(1);
for (int i = 0; i < Height / 2; i++)
{
for (int j = 0; j < Width; j++)
{
var Temporary = TransformedPic[i, j];
TransformedPic[i, j] = TransformedPic[Height - 1 - i, j];
TransformedPic[Height - 1 - i, j] = Temporary;
}
}
TransformedPic is the variable in which the original image is saved under
Step 1 : Mirror an Image.
To do this apply a negative sacaletransform. Like
new ScaleTransform() { ScaleX = -1 };
Then merge two image side by side.
you can see it here, how you can merge Two images.
Here is another way :
//Get Your Image from Picturebox
Bitmap image1 = new Bitmap(pictureBox1.Image);
//Clone it to another bitmap
Bitmap image2 = (Bitmap)image1.Clone();
//Mirroring
image2.RotateFlip(RotateFlipType.RotateNoneFlipX);
//Merge two images in bitmap image,
Bitmap bitmap = new Bitmap(image1.Width + image2.Width, Math.Max(image1.Height, image2.Height));
using (Graphics g = Graphics.FromImage(bitmap))
{
g.DrawImage(image1, 0, 0);
g.DrawImage(image2, image1.Width, 0);
}
//Show them in a picturebox
pictureBox2.Image = bitmap;
This is how it's done with a BitMap, you can draw the image from the graphics and redraw the graphics object with the modified one.
public Bitmap MirrorImage(Bitmap source)
{
Bitmap mirrored = new Bitmap(source.Width, source.Height);
for(int i = 0; i < source.Height; i++)
for(int j = 0; j < source.Width; j++)
mirrored.SetPixel(i, j, source.GetPixel(source.Width - j - 1, i);
return mirrored;
}

Unable to read pixels from bitmap using Xamarin

I thought trying to get pixel data would be a simple thing to do, but it has become a big headache.
My original goal is to go through every pixel in the bitmap and then do some modification with the data. However every time I try to get the pixel I get 0 as a result. It feels like the image is not going to the bitmap. Am I missing something in my code or the code in general wrong?
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.Main);
var imageView1 = FindViewById<ImageView>(Resource.Id.imageView1);
imageView1.SetImageResource(Resource.Drawable.pic1);
Bitmap b = BitmapFactory.DecodeResource(Resources,Resource.Drawable.pic1);
b = Bitmap.CreateBitmap(b);
imageView1.SetImageBitmap(b);
for (int i = 0; i < width; i++)
{
for (int j = 0; j < height; j++)
{
int pixel = b.GetPixel(i, j);
int A = Color.GetAlphaComponent(pixel);
int R = Color.GetRedComponent(pixel);
int G = Color.GetGreenComponent(pixel);
int B = Color.GetBlueComponent(pixel);
}
}
}
Needed to use
Bitmap b = BitmapFactory.DecodeResource(Resources,Resource.Drawable.pic1);
for (int i = 0; i < width; i++)
{
for (int j = 0; j < height; j++)
{
Color pixel = new Color(b.GetPixel(i, j));
int A = Color.GetAlphaComponent(pixel);
int R = Color.GetRedComponent(pixel);
int G = Color.GetGreenComponent(pixel);
int B = Color.GetBlueComponent(pixel);
}
}

How to draw in WPF by pixel

Necessary to draw by pixels array with data (large). How can I do that?
I tried a Canvas and Rectangle on it - the computer hanged himself ...
Tried the following options below (DrawingContext) - computer still hangs himself, but a little bit less.
Please recommend options with the least load on the computer.
int width = 800; - the size of the array (large!!!)
int size = 1; - it is desirable to be able to make the fragment is not only one but several pixels
protected override void OnRender(System.Windows.Media.DrawingContext drawingContext)
{
Random rnd = new Random();
int width = 800;
int size = 1;
CellType[,] types = new CellType[width, width];
for (int i = 0; i < width; i++)
{
for (int j = 0; j < width; j++)
{
int r = rnd.Next(0, 100);
if (r >= 70) types[j,i] = CellType.IsOccupied;
else types[j, i] = CellType.IsEmpty;
}
}
for (int i = 0; i < width; i++)
{
for (int j = 0; j < width; j++)
{
Brush brush = Brushes.Black;
switch (types[j, i])
{
case CellType.IsEmpty: brush = Brushes.Green;
break;
case CellType.IsOccupied: brush = Brushes.Black;
break;
}
drawingContext.DrawRectangle(brush,
new Pen(brush, 1),
new Rect(j * size, i * size, size, size));
}
}
base.OnRender(drawingContext);
}
For how long does it freeze? A couple of years ago I did something quite similar in Silverlight. It was a bit slow but still acceptable. Try using WriteableBitmap with SetPixel or FillRectangle methods, but please keep in mind that drawing pixel by pixel in a loop will always take some time.
Forgot to mention SetPixel and FillRectangle may need WriteableBitmapEx, available here:
http://writeablebitmapex.codeplex.com/

Work with matrix in emgu cv

I have a matrix (2D) of an image in EMGU cv,how can I fill the rest of the matrix with zeros but keep a certain area(rectangle) with the original data?
Method 1
One way to achieve what your after is to access the Data of the Matrix directly and set the values to '0' the following code will set the lower quarter of 'My_Matrix' to 0 where all other values will remain 20.
Matrix<double> My_Matrix_Image = new Matrix<double>(8,10);
My_Matrix_Image.SetValue(20); //set all values of Matrix to 20
for(int i = 4; i< My_Matrix_Image.Height; i++)
{
for (int j = 5; j < My_Matrix_Image.Width; j++)
{
My_Matrix_Image.Data[i,j] = 0;
}
}
To achieve what you wanted from your comment you must still take the same approach My_Matrix_Image would contain your image data values from 0-255 I will give solutions for both greyscale and colour images. I will keep the first 30 rows of the image and set the rest to zero the image will be 100x100 pixels in size. Change j = 30 to alter the amount of rows.
First Colour Image Matrix there will be 3 channels Red, Green and Blue:
for(int i = 0; i< My_Matrix_Image.Height; i++)
{
for (int j = 30; j < My_Matrix_Image.Width; j++)
{
My_Matrix_Image.Data[i,j,0] = 0; //RED
My_Matrix_Image.Data[i,j,1] = 0; //GREEN
My_Matrix_Image.Data[i,j,2] = 0; //BLUE
}
}
Greyscale Image Matrices are easier as there will be 1 channel:
for(int i = 0; i< My_Matrix_Image.Height; i++)
{
for (int j = 30; j < My_Matrix_Image.Width; j++)
{
My_Matrix_Image.Data[i,j,0] = 0;
}
}
Now lets assume you want to keep the Middle 40 Rows you will have to add an additional loop remember this is the only way with a Matrix I have only provided the colour image example as you can see it starts getting a little messy and Method 2 may be better:
for(int i = 0; i< My_Matrix_Image.Height; i++)
{
//LOOP 1 SET THE FRONT ROWS
for (int j = 0; j<40; j++)
{
My_Matrix_Image.Data[i,j,0] = 0; //RED
My_Matrix_Image.Data[i,j,1] = 0; //GREEN
My_Matrix_Image.Data[i,j,2] = 0; //BLUE
}
// LOOP 2 SET THE BACK ROWS
for (int j = 60; j < My_Matrix_Image.Width; j++)
{
My_Matrix_Image.Data[i,j,0] = 0; //RED
My_Matrix_Image.Data[i,j,1] = 0; //GREEN
My_Matrix_Image.Data[i,j,2] = 0; //BLUE
}
}
Method 2
Now lets assume you do want to keep a rectangle of data. Creating 6 Loops is complex and inefficient so here is what you could do.
//Make a Blank Copy of your Image this will be automatically full of zeros
Matrix<double> My_Image_Copy = My_Image_Matrix.CopyBlank();
//Now copy the data you want to keep from your original image into you blank copy
for(int i = 40; i< 60; i++)
{
for (int j = 40; j < 60; j++)
{
My_Image_Copy.Data[i,j,0] = My_Matrix_Image.Data[i,j,0]; //RED
My_Image_Copy.Data[i,j,1] = My_Matrix_Image.Data[i,j,1]; //GREEN
My_Image_Copy.Data[i,j,2] = My_Matrix_Image.Data[i,j,2]; //BLUE
}
}
The above code will copy the centre 20x20 pixels from an image you can obviously change this to copy whole rows by using for(int i = 0; i< My_Matrix_Image.Height; i++)
Much better I'm sure you will agree.
Alternative
Now while you are using a Matrix to store you data using a Image construct makes coding a little simpler. While this may not be relevant to you it may be to others.
If you use Image or alternative to store your Image data then this can be achieved by:
Image<Gray, Byte> My_Image = new Image<Gray, byte>(openfile.FileName);
Image<Gray, Byte> My_Image_Copy = My_Image.CopyBlank();
Rectangle Store_ROI = my_image.ROI; //Only need one as both images same size
My_Image.ROI = new Rectangle(50, 50, 100, 100);
My_Image_Copy.ROI = new Rectangle(50, 50, 100, 100);
My_Image_Copy = My_Image.Copy(); //This will only copy the Region Of Interest
//Reset the Regions Of Interest so you will now operate on the whole image
My_Image.ROI = Store_ROI;
My_Image_Copy.ROI = Store_ROI;
Now this the same as Method 2 but you don't need to sort write out loops where errors can occur.
Hope this correction answers your question,
Cheers
Chris
// Rectangle's parameters
static int x = 3;
static int y = 3;
static int width = 2;
static int height = 2;
Rectangle specificArea = new Rectangle(x, y, width, height);
// Your image matrix; 20x20 just a sample
Matrix<int> imageMatrix = new Matrix<int>(20, 20);
public Matrix<int> cropMatrix()
{
// Crop a specific area from image matrix
Matrix<int> specificMatrix = imageMatrix.GetSubRect(specificArea);
// Matrix with full of zeros and same size with imageMatrix
Matrix<int> croppedMatrix = imageMatrix.CopyBlank();
for (int i = x; i < x+width; i++)
{
for (int j = y; j < y+height; j++)
{
// Set croppedMatrix with saved values
croppedMatrix[i, j] = specificMatrix[i-x, j-y];
}
}
return croppedMatrix;
}

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