i try to take surf algorithm in matlab and to convert it in c#.
The algorithm in matlab returns array of coordinates. The size of the array is [10,4].
In c# i wrote a code that doesnt returns the right information in the array.
Am i missed something when i converted this code?
private static void Main(string[] args)
{
Bitmap img1 = new Bitmap(Image.FromFile(#"C:\Users\cbencham\source\repos\3.jpg"));
Bitmap img2 = new Bitmap(Image.FromFile(#"C:\Users\cbencham\source\repos\4.jpg"));
//Get image dimensions
int width = img1.Width;
int height = img1.Height;
//Declare the double array of grayscale values to be read from "bitmap"
double[,] im1 = new double[width, height];
//Loop to read the data from the Bitmap image into the double array
int i, j;
for (i = 0; i < width; i++)
{
for (j = 0; j < height; j++)
{
Color pixelColor = img1.GetPixel(i, j);
double b = pixelColor.GetBrightness(); //the Brightness component
//Note that rows in C# correspond to columns in MWarray
im1.SetValue(b, i, j);
}
}
//Get image dimensions
width = img2.Width;
height = img2.Height;
//Declare the double array of grayscale values to be read from "bitmap"
double[,] im2 = new double[width, height];
//Loop to read the data from the Bitmap image into the double array
for (i = 0; i < width; i++)
{
for (j = 0; j < height; j++)
{
Color pixelColor = img2.GetPixel(i, j);
double b = pixelColor.GetBrightness(); //the Brightness component
//Note that rows in C# correspond to columns in MWarray
im2.SetValue(b, i, j);
}
}
MLApp.MLApp matlab = new MLApp.MLApp();
matlab.Execute(#"cd C:\Users\cbencham\source\repos");
object result = null;
matlab.Feval("surf", 1, out result, im1, im2);
// TODO: convert result to double Array [10,4]
for (i = 0; i < 10; i++)
{
for (j = 0; j < 4; j++)
{
Console.Write(arr[i, j]);
}
Console.WriteLine();
}
Console.ReadLine();
}
}
}
You can't convert an object array to a double array. But for your purpose you don't need to. You just need to cast it to and object array and print its contents.
var arr = result as object[,];
or
if (result is object[,] arr)
{
for (i = 0; i < arr.GetLength(0); i++)
{
for (j = 0; j < arr.GetLength(1); j++)
{
Console.Write(arr[i, j]);
}
Console.WriteLine();
}
Console.ReadLine();
}
Related
I have a 2D array containing Temperature data from a numerically solved heat transfer problem in C#. To visualize the temperature distribution I used the "Bitmap"; Where the lowest Temperature is showed by the color Blue while the hottest is represented by Red!
The problem is that it takes too much time to generate the bitmap for a 300x300 image size! While i'm trying to work with larger ones which makes it impossible!
Is there any more efficient way to make it work?
Any help would be greatly appreciated
Here's a little bit of my code and a a generated bitmap:enter image description here
//RGB Struct
struct RGB
{
public Int32 num;
public int red;
public int green;
public int blue;
public RGB(Int32 num)
{
int[] color = new int[3];
int i = 2;
while (num > 0)
{
color[i] = num % 256;
num = num - color[i];
num = num / 256;
i--;
}
this.red = color[0];
this.green = color[1];
this.blue = color[2];
this.num = (256 * 256) * color[0] + 256 * color[1] + color[2];
}
}
//Create Color Array
Int32 red = 16711680;
Int32 blue = 255;
Int32[,] decimalColor = new Int32[Nx, Ny];
for (int i = 0; i < Nx; i++)
{
for (int j = 0; j < Ny; j++)
{
double alpha = (T_new[i, j] - T_min) / (T_max - T_min);
double C = alpha * (red - blue);
decimalColor[i, j] = Convert.ToInt32(C) + blue;
}
}
//Bitmap Result
Bitmap bmp = new Bitmap(Nx, Ny);
for (int i = 0; i < Nx; i++)
{
for (int j = 0; j < Ny; j++)
{
RGB rgb = new RGB(decimalColor[i, j]);
bmp.SetPixel(i,j,Color.FromArgb(rgb.red,rgb.green,rgb.blue));
}
}
pictureBox1.Image = bmp;
Hello guys is it possible to limit the array size just like this example
Now i want only to show 6 of them.
What i have done so far is this
CustomClass
const int MAX = 104; // = 8 decks * 52 cards / 4cardsoneround
const int Y = 6;
int[,] arrayRoad = new int[Y, X];
public int[,] GetRoad(int x) {
arrayRoad = new int[x, 6];
return arrayRoad;
}
Now I'm displaying it on my MainClass like this
ScoreBoard bsb = new ScoreBoard();
private void Road()
{
bsb.makeRoad(history); // Road
int[,] arrayRoad = bsb.GetRoad(6); //<--- sample value 6
string s = "";
for (int y = 0; y < arrayRoad.GetLength(0); y++)
{
//just 27 for now
for (int x = 0; x < 28; x++)
{
s += string.Format("{0:D2}",arrayRoad[y, x]);
s += ".";
}
s += "\n";
}
Debug.Log(s);
}
The problem with this code is that it's giving me an Array out of index
Is this possible??
Updated
public int[,] GetRoad(int x = MAX,int y = Y) {
arrayRoad = new int[y, x];
return arrayRoad;
}
Where in my Max = 104 and Y = 6
int[,] arrayRoad = bsb.GetRoad(12,6); //12 rows and 6 in height
string s = "";
for (int y = 0; y < arrayRoad.GetLength(0); y++)
{
for (int x = 0; x < arrayRoad.GetLength(1); x++)
{
s += string.Format("{0:D2}",arrayRoad[y, x]);
s += ".";
}
s += "\n";
}
Debug.Log(s);
}
I have all this value earlier before i perform the update code
Now when i perform the updated code here's what I got
The expected result must be this
Inside of that black marker those twelve columns only must be shown because i declared on my
int[,] arrayRoad = bsb.GetRoad(12,6);
Note this:
public int[,] GetBigEyeRoad(int x) {
arrayRoad = new int[x, 6]; // <-------------
return arrayBigEyeRoad;
There you are fixing the length of the second dimension of the array to 6.
for (int x = 0; x < 28; x++)
{
s += string.Format("{0:D2}",arrayBigEyeRoad[y, x]); // <------------
There, you are trying to access indices up to 28 on the second dimension of the array. The Out of Range error comes from that.
What I did here was copy my old array to the new one just like this code below
int[,] arrayBigRoadResult = new int[6, x];
//copy manually the element references inside array
for (int i = 0; i < 6; i++)
{
for (int j = 0; j < x; j++)
{
arrayBigRoadResult[i, j] = arrayBigRoad[i, j];
}
}
return arrayBigRoadResult;
then by calling it just like this
int[,] arrayRoad = bsb.GetRoad(12);
It would only display 12 columns and 6 rows :)
I wrote an algorithm on Matlab. Hence I wanna use it on .Net. I perfectly converted .m file to .dll for using it on .Net with Matlab Library Compiler. First, I tried converting Matlab function to .dll without any parameter and it worked well on .Net. However, when I wanna use that function with parameter, I'm getting some error below. The parameter is basically image. I call the function on Matlab like this I = imread('xxx.jpg'); Detect(I);So my c# code like this
static void Main(string[] args)
{
DetectDots detectDots = null;
Bitmap bitmap = new Bitmap("xxx.jpg");
//Get image dimensions
int width = bitmap.Width;
int height = bitmap.Height;
//Declare the double array of grayscale values to be read from "bitmap"
double[,] bnew = new double[width, height];
//Loop to read the data from the Bitmap image into the double array
int i, j;
for (i = 0; i < width; i++)
{
for (j = 0; j < height; j++)
{
Color pixelColor = bitmap.GetPixel(i, j);
double b = pixelColor.GetBrightness(); //the Brightness component
bnew.SetValue(b, i, j);
}
}
MWNumericArray arr = bnew;
try
{
detectDots = new DetectDots();
detectDots.Detect(arr);
Console.ReadLine();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
I used the same image that I called xxx.jpg which is 5312 x 2988.
But I'm getting this error below.
... MWMCR::EvaluateFunction error ...
Index exceeds matrix dimensions.
Error in => DetectDots.m at line 12.
... Matlab M-code Stack Trace ...
at
file C:\Users\TAHAME~1\AppData\Local\Temp\tahameral\mcrCache9.0\MTM_220\MTM\DetectDots.m, name DetectDots, line 12.
The important thing is, it says "Index exceeds matrix dimensions", is it true way converting Bitmap to MWArray? What is the problem?
I realised that rows in C# correspond to columns in MWarray. So I change small things on the code.
Instead of double[,] bnew = new double[width, height]; I use it double[,] bnew = new double[height, width];
and instead of bnew.SetValue(b, i, j); I use it bnew.SetValue(b, j, i);
Someone may use the whole code about Bitmap to MWArray below
Bitmap bitmap = new Bitmap("001-2.bmp");
//Get image dimensions
int width = bitmap.Width;
int height = bitmap.Height;
//Declare the double array of grayscale values to be read from "bitmap"
double[,] bnew = new double[height, width];
//Loop to read the data from the Bitmap image into the double array
int i, j;
for (i = 0; i < width; i++)
{
for (j = 0; j < height; j++)
{
Color pixelColor = bitmap.GetPixel(i, j);
double b = pixelColor.GetBrightness(); //the Brightness component
//Note that rows in C# correspond to columns in MWarray
bnew.SetValue(b, j, i);
}
}
MWNumericArray arr = bnew;
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.
I want to convert an image into a multidimensional array with 0's and 1's.
The image would be:
I wan't to store this image into an array with 'black' being stored as 1 and 'white' being stored as 0 in the array. The array would be of 8 rows and 6 columns:
int[,] imgArray = new int[8,6];
Is there a way to achieve this in C#?
If you are using a Bitmap, you can populate the array like this:
var bitmap = new Bitmap(#"C:\MyImage.png");
var imgArray = new int[bitmap.Width,bitmap.Height];
var blackArgb = Color.Black.ToArgb();
var whiteArgb = Color.White.ToArgb();
for (var i = 0; i < bitmap.Width; ++i)
{
for (var j = 0; j < bitmap.Height; ++j)
{
var pixelCol = bitmap.GetPixel(i, j);
if (pixelCol.ToArgb() == blackArgb)
{
imgArray[i, j] = 1;
}
else if (pixelCol.ToArgb() == whiteArgb)
{
imgArray[i, j] = 0;
}
else
throw new InvalidOperationException("Pixel color must be black or white");
}
}