Get all pixel data from Bitmap object - c#

I want to fill up a double[400] array with pixel data as seen in the below code.
I have been searching for the answer and I still haven't found one. My attempt is:
Bitmap bm = (Bitmap) Image.FromFile(fileName);
numberVisual.Image = bm;
long overallIteration = 0;
for (long i = 0; i <= 20/*bm.Width*/; ++i)
{
for (long h = 0; h <= 20/*bm.Height*/; ++h)
{
input[/*(i * h)*/overallIteration] =
bm.GetPixel((int)h, (int)i) != Color.White ? 0 : 1;
++overallIteration;
}
}
It gives off an exception on
bm.GetPixel System.ArgumentOutOfRangeException: Parameter must be
positive and < Width.
All the bitmaps I am using are 20x20px.

Please check the height and the width of the image you are loading, if your image is 20x20, you have to iterate from 0 to 19 and not from 0 to 20
Right code
for (long i = 0; i < 20; ++i)
{
for (long h = 0; h < 20; ++h)
{
Wrong code
for (long i = 0; I <= 20; ++i)
{
for (long h = 0; h <= 20; ++h)
{

Related

Processing image stops mid-loop for no reason c#

I have an image, and I want to take each square of 256X256 pixels, find the mean color, and draw that square with said color.
Problem: it seems that after the first square the processing suddenly stops, but after following the program I can see the indexes are just fine. I have no idea if the problem lies with file writing in my computer system, or a wrong use of the "Bitmap" class functions.
original:
result:
code:
public const int big =256;
public const int small = 16;
static void Main(string[] args)
{
Bitmap bt = new Bitmap(#"C:\Users\mishe\Desktop\00_sorted images - training\general shores\agulhas_oli_2016146_lrg.jpg");
Bitmap bt2 = bt;
Color MeanColor;
double r = 0;
double g = 0;
double b = 0;
int i = 0;
int j = 0;
//big loop to go over all image
for (i = 0; i < bt.Height-257; i+=256)
{
for (j = 0; j < bt.Width-257; j+=256)
{
/////////////////////////////
//small loop on 1 square to get the mean color of the area
for (int x = i; x < big; x++)
{
for (int y = j; y < big; y++)
{
r += bt.GetPixel(x, y).R;
g += bt.GetPixel(x, y).G;
b += bt.GetPixel(x, y).B;
}
}
/////////////////////////////
r = r / Math.Pow(big, 2);
g = g / Math.Pow(big, 2);
b = b / Math.Pow(big, 2);
MeanColor = Color.FromArgb((int)r, (int)g, (int)b);
/////////////////////////////
//small loop on the same square to set the color
for (int x = i; x < big; x++)
{
for (int y = j; y < big; y++)
{
bt2.SetPixel(x, y, MeanColor);
}
}
/////////////////////////////
}
}
bt2.Save(#"C:\Users\mishe\Desktop\compressed image.jpg", ImageFormat.Jpeg);
}
This line:
//small loop on 1 square to get the mean color of the area
for (int x = i; x < big; x++)
After the first square, x will be 256, so it won't do the small loop.
I think you want:
for (int x = i; x < i + big; x++)
Or your small loop could be:
for (int x = 1; x < big; x++)
and then add the large and small values inside the loop:
r += bt.GetPixel(i + x, j + y).R;

How to scan all pixel in square C#?

I get height and width of an element on the website.
Example: Element easelCanvas with height: 1000px and width: 600px.
Note: I using Excel to easy imagine.
It must scan like this:
I do with code:
int width = 600, height = 1000;
for (int i = 0; i < width; i += 60)
{
for (int j = 0; j < height; j += 50)
{
}
}
First rows it scan from at position(0, 0) to final and go like:
I have creates one sample canvas it might be helpful to you ..
var canvas=document.getElementById("myCanvas");
var ctx=canvas.getContext("2d");
var index = 0;
for (var i = 0; i < canvas.height; i += 60)
{
for (var j = 0; j < canvas.width; j += 50)
{
ctx.strokeText("index"+index,j,i+15);
index++;
}
}
JsBin Link

3x3 Average filter in C#

I've written code to smooth an image,it's using 3x3 averaging filter. But seem it's not working
The picture output almost black
public void Smooth(Bitmap bmpInput){
Bitmap temp;
Color c;
float sum = 0;
for (int i = 0; i < bmpInput.Width; i++)
{
for (int j = 0; j < bmpInput.Height; j++)
{
c = bmpInput.GetPixel(i, j);
byte gray = (byte)(.333 * c.R + .333 * c.G + .333 * c.B);
bmpInput.SetPixel(i, j, Color.FromArgb(gray, gray, gray));
}
}
temp = bmpInput;
for (int i = 0; i <= bmpInput.Width - 3; i++)
for (int j = 0; j <= bmpInput.Height - 3; j++)
{
for (int x = i; x <= i + 2; x++)
for (int y = j; y <= j + 2; y++)
{
c = bmpInput.GetPixel(x,y);
sum = sum + c.R ;
}
int color = (int)Math.Round(sum/9,10);
temp.SetPixel(i + 1, j + 1, Color.FromArgb(color, color, color));
sum = 0;
}
bmpInput = temp;
}
The variable tempstill refers to the exact same bitmap. You have to assign tempto new Bitmap image and work with this. Alternatively, you can store the new pixel values in a temporary array and transfer the contents of this array back to the image afterwards.

Short Time Fourier Transform in C#

I'm writing the following code that implements the stft (http://en.wikipedia.org/wiki/Short-time_Fourier_transform) through the Exocortex library (http://www.exocortex.org/dsp/), where "window" is a 512 Hamming window, "left" is the left channel from an audio stream passed into a double array, "frames" is the number of frames I split the audio file into (say around 600) and the "offset" variable performs the overlap.
I've also added the ability of zero padding in the Exocortex FFT class:
if( data.Length < length ) {
for (int k = data.Length; k < length; k++)
{
data[k].Re = 0;
data[k].Im = 0;
}
The problem is that for frames=600 this code runs for about 7000msec! Am I missing something here?
Exocortex.DSP.ComplexF[] x_frame = new Exocortex.DSP.ComplexF[2048];
int offset = 0;
for (int m = 0; m < frames-1; m++)
{
for (int i = 0; i < 512; i++)
{
x_frame[i].Re = (float)(left[i + offset] * window[i]);
x_frame[i].Im = 0;
}
Exocortex.DSP.Fourier.FFT(x_frame, 2048, FourierDirection.Forward);
offset += 256;
}

Populating a color array with every 8th pixel in an image. C#

I have an image that is 512x280 pixels. I want to populate a 64x35 array with every 8th pixel in the matrix.
Here is what I have right now:
Color[,] imgArray = new Color[b.Width, b.Height];
for (int y = 0; y < 35; y++)
{
for (int x = 0; x < 64; x++)
{
imgArray[x, y] = b.GetPixel(x, y);
}
}
But that will get just the top corner of the image. How would I change the loop so it grabs every 8th pixel to fill the array with?
edit: I think I may have gotten it. Can someone read this and assure me that it is correct?
Color[,] imgArray = new Color[64, 35];
for (int y = 0; y < 280; y+=8)
{
for (int x = 0; x < 512; x+=8)
{
imgArray[x, y] = b.GetPixel(x, y);
}
}
Simply multiply the coordinates by 8 when you get the pixels:
Color[,] imgArray = new Color[64, 35];
for (int y = 0; y < 35; y++) {
for (int x = 0; x < 64; x++) {
imgArray[x, y] = b.GetPixel(x * 8, y * 8);
}
}

Categories