Spectral Saliency map from Matlab code to C#.net, - c#

SSM screenshot
Hi, I have implemeted the Spectral Saliency map from Matlab code to C#.net,
But result is some strange.
Result map is not correctly mapped to original image.
What's wrong with me?
Original Matlab code(Octave) is here:
%% Read image from file
inImg = im2double(rgb2gray(imread('1.jpg')));
inImg = imresize(inImg, 64/size(inImg, 2));
%% Spectral Residual
myFFT = fft2(inImg);
myLogAmplitude = log(abs(myFFT));
myPhase = angle(myFFT);
mySpectralResidual = myLogAmplitude - imfilter(myLogAmplitude, fspecial('average', 3), 'replicate');
saliencyMap = abs(ifft2(exp(mySpectralResidual + i*myPhase))).^2;
%% After Effect
saliencyMap = mat2gray(imfilter(saliencyMap, fspecial('gaussian', [10, 10], 2.5)));
imshow(saliencyMap);
My Code is here : C#.net + AForge.net -------------
Windows10, VS2017, AForge & Accord.net
// 이미지 로드
image2 = (Bitmap)pictureBox1.Image;
// 박스 블러
// logAmp smoothing
Accord.Imaging.Filters.FastBoxBlur filtBox1 =
new Accord.Imaging.Filters.FastBoxBlur(3, 3);
// apply the filter
filtBox1.ApplyInPlace(image2);
// 크기줄이기 64x
AForge.Imaging.Filters.ResizeBilinear filtResize =
new AForge.Imaging.Filters.ResizeBilinear(64, 64);
// apply the filter
Bitmap smallImage = filtResize.Apply(image2);
// 2D FFT -------------------------------------------
//cv::merge(planes, 2, complexImg);
AForge.Imaging.ComplexImage complexImage =
AForge.Imaging.ComplexImage.FromBitmap(smallImage);
//double[,] logAmpl = new double[64, 64];
for (int x = 0; x < 64; x++)
{
for (int y = 0; y < 64; y++)
{
//Calculating elementwise complex conjugate of the shifted image 2d vector
complexImage.Data[x, y].Im = 0;
}
}// for y
//cv::dft(complexImg, complexImg);
complexImage.ForwardFourierTransform();
// Calc Log AmpliTude of magnitude
double[,] logAmpl = new double[64, 64];
for (int x = 0; x < 64; x++)
{
for (int y = 0; y < 64; y++)
{
//Calculating elementwise complex conjugate of the shifted image 2d vector
logAmpl[x, y] = Math.Log(Complex.Multiply(
complexImage.Data[x, y], complexImage.Data[x, y]).Magnitude);
}
}// for y
Bitmap logAmp = logAmpl.ToBitmap();
// extract Phase, int[,] array = new int[4, 2];
double[,] myPhase = new double[64, 64];
for (int x = 0; x < 64; x++)
{
for (int y = 0; y < 64; y++)
{
//Calculating elementwise complex conjugate of the shifted image 2d vector
myPhase[x, y] = complexImage.Data[x, y].Phase;
}
}// for y
// calc Spectral Residual -------------------------
// logAmp smoothing
Accord.Imaging.Filters.FastBoxBlur filtBox =
new Accord.Imaging.Filters.FastBoxBlur(3, 3);
// apply the filter
Bitmap smoothAmp= filtBox.Apply(logAmp);
// subtraction
AForge.Imaging.Filters.Subtract filter =
new AForge.Imaging.Filters.Subtract(smoothAmp);
// apply the filter
Bitmap specResi = filter.Apply(logAmp);
// Calc Saliency map -----------------
// residual for Re
AForge.Imaging.ComplexImage totalSpec =
AForge.Imaging.ComplexImage.FromBitmap(specResi);
// + j*im
for (int x = 0; x < 64; x++)
{
for (int y = 0; y < 64; y++)
{
totalSpec.Data[x, y].Im = myPhase[x, y];
}
}// for y
// exp of re + j*im
for (int x = 0; x < 64; x++)
{
for (int y = 0; y < 64; y++)
{
totalSpec.Data[x, y] = Complex.Exp(totalSpec.Data[x, y]);
}
}// for y
// ifft2
//complexImage.ForwardFourierTransform();
totalSpec.ForwardFourierTransform();
// .^2 of ifft2
for (int x = 0; x < 64; x++)
{
for (int y = 0; y < 64; y++)
{
totalSpec.Data[x, y] =
Complex.Multiply(totalSpec.Data[x, y], totalSpec.Data[x, y]);
}
}// for y
// abs .^2
Bitmap salMap= totalSpec.ToBitmap();
// 가우시안 블러
Accord.Imaging.Filters.GaussianBlur filt2 =
new Accord.Imaging.Filters.GaussianBlur(2.5, 9);
// apply the filter
filt2.ApplyInPlace(salMap);
// create filter
Accord.Imaging.Filters.ContrastStretch filtNorm =
new Accord.Imaging.Filters.ContrastStretch();
// process image
filtNorm.ApplyInPlace(salMap);
//ImageBox.Show(salMap);
pictureBox3.Image = salMap;
What;s wrong with me?

Related

Parallize elementwise matrix (2dArray) calculations

This is my first question and I'm relatively new to C#. (Excuse my bad English)
I'm writing a template matching algorithm in C# using the .NET Framework with a WindowsForms Application in Visual Studio. Sadly the new indices and ranges functions from C#8.0, especially the range operator (..), didn't get implemented in the .NET Framework. I know there is a workaround for this as you can see in this thread but it is not supported by Microsoft. So I'm searching for another way to parallelize my elementwise 2dArray (matrix) calculations to make my program faster.
In my program, I'm calculating the differential square (ds) of an area (with the size of my template) inside a 2dArray (my image) and a 2dArray (my template). These values a written to a new 2dAary (DS) which holds all differential squares in the corresponding positions to the image. I can search the indices of DS where the differential square is minimal which is equal to the matching position (highest correspondence between template and image) of the template inside the image.
In python the calculation of DS is very quick using the index range operator (:) and will look like this:
H,W = I.shape # read out Height H & Width W from Image I
h,w = T.shape # read out Height h & Width w from Template T
for i in range(H-h+1):
for j in range(W-w+1):
DS[i,j] = np.sum((I[i:i+h,j:j+w] - T)**2)
But in C# I have to make the calculation of DS elementwise therefore it looks like this and takes for ever:
int Tw = template.Width;
int Th = template.Height;
int Iw = image.Width;
int Ih = image.Height;
int d = 0;
int[,] ds = new int[Tw, Th];
int[,] DS = new int[Iw - Tw + 1, Ih - Th + 1];
for (int y = 0; y < Ih - Th + 1; y++)
{
for (int x = 0; x < Iw - Tw + 1; x++)
{
for (int yT = 0; yT < Th; yT++)
{
for (int xT = 0; xT < Tw; xT++)
{
d = I[x + xT, y + yT] - T[xT, yT];
ds[xt, yt] = d * d;
}
}
int sum = ds.Cast<int>().Sum();
DS[x, y] = sum;
}
}
I know that I could use threads but that would be a little complex for me.
Or maybe I could use CUDA with my Nvidia GPU to speed things up a little.
But I am asking you and myself is there another way to parallelize (optimize) my elementwise 2dArray calculations?
I look forward to any help.
Many thanks in advance!!!
EDIT:
Here I have a working example of my code for a .NET Framework Console App. As you can see I make a lot of elementwise 2d and 3d Array calculations which I would like to process in parallel (or perform them faster in any other way):
using System;
using System.Drawing;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TemplateMatcher_Console
{
class Program
{
public static int[,,] bitmapToMatrix(Bitmap bmp)
{
int[,,] I = new int[bmp.Width, bmp.Height, 3];
for (int y = 0; y < bmp.Height; y++)
{
for (int x = 0; x < bmp.Width; x++)
{
Color pix = bmp.GetPixel(x, y);
I[x, y, 0] = Convert.ToInt32(pix.R);
I[x, y, 1] = Convert.ToInt32(pix.G);
I[x, y, 2] = Convert.ToInt32(pix.B);
}
}
return I;
}
public static int[] indexOfMiniumValue(int[,] matrix)
{
int value = 0;
int minValue = 999999999;
int minFirstIndex = 0;
int minSecondIndex = 0;
int[] ij = new int[2];
for (int i = 0; i < matrix.GetLength(0); i++)
{
for (int j = 0; j < matrix.GetLength(1); j++)
{
value = matrix[i, j];
if (value < minValue)
{
minValue = value;
minFirstIndex = i;
minSecondIndex = j;
}
}
}
ij[0] = minFirstIndex;
ij[1] = minSecondIndex;
return ij;
}
public static void Print2DArray<T>(T[,] matrix)
{
for (int i = 0; i < matrix.GetLength(0); i++)
{
for (int j = 0; j < matrix.GetLength(1); j++)
{
Console.Write(matrix[i, j] + "\t");
}
Console.WriteLine();
}
}
static void Main(string[] args)
{
// Deklaration & Eingabe
Console.WriteLine("Type the filepath for your image and then press Enter");
string im = Console.ReadLine();
Console.WriteLine("\nType the filepath for your template and then press Enter");
string temp = Console.ReadLine();
Bitmap template = new Bitmap(#temp);
Bitmap image = new Bitmap(#im);
int Tw = template.Width;
int Th = template.Height;
int Iw = image.Width;
int Ih = image.Height;
int[,] ds = new int[Tw, Th];
int[,] DS = new int[Iw - Tw + 1, Ih - Th + 1];
int[,,] DS_rgb = new int[Iw - Tw + 1, Ih - Th + 1, 3];
int[] xy = new int[2];
// Verarbeitung
// int[,,] I = Array.ConvertAll(image_I, new Converter<byte, int>(Convert.ToInt32));
int[,,] I = bitmapToMatrix(image);
int[,,] T = bitmapToMatrix(template);
for (int rgb = 0; rgb < 3; rgb++)
{
for (int y = 0; y < Ih - Th + 1; y++)
{
for (int x = 0; x < Iw - Tw + 1; x++)
{
//DS_rgb[x, y, rgb] = (I[x .. x + template.Width, y .. y + template.Height, rgb] - T[0 .. template.Width, 0 .. template.Height, rgb]);
for (int yT = 0; yT < Th; yT++)
{
for (int xT = 0; xT < Tw; xT++)
{
ds[xT, yT] = (I[x + xT, y + yT, rgb] - T[xT, yT, rgb]) * (I[x + xT, y + yT, rgb] - T[xT, yT, rgb]);
}
}
int sum = ds.Cast<int>().Sum();
DS_rgb[x, y, rgb] = sum;
}
}
}
//DS[.., ..] = DS_rgb[.., .., 0] + DS_rgb[.., .., 1] + DS_rgb[.., .., 2];
for (int y = 0; y < Ih - Th + 1; y++)
{
for (int x = 0; x < Iw - Tw + 1; x++)
{
DS[x, y] = DS_rgb[x, y, 0] + DS_rgb[x, y, 1] + DS_rgb[x, y, 2];
}
}
//xy = DS.FindIndex(z => z == Math.Min(DS));
xy = indexOfMiniumValue(DS);
// Ausgabe
// Ausgeben der Matrix DS
/*
Console.WriteLine("\nMatrix with all differtial squares:");
Print2DArray(DS);
*/
Console.WriteLine($"\nPosition of your template in your image (upper left corner): ({xy[0]}, {xy[1]})");
Console.Write("\nPress any key to close the TemplateMatcher console app...");
Console.ReadKey();
}
}
}

c# unity 3d coordinates map sync

I'm working on a procedurally generated map/game. I have 3D chunks with 20x20x20 points in a 3D array. I'm using marching cubes algorithm to generate the chunks. I try to manipulate the density with adding and subtracting the values but in the same time trying to sync the neighbour chunks edge together the with chunk im modifying.
Here is the code i tough it will work, but its not working:
`for (int i = 0; i < 9; i++)
{
Chunk nChunk = neighbours[i].GetComponent<Chunk>();
if (i == 4) continue; //skip self
for (int z = 0; z < 20; z += 19)
{
for (int x = 0; x < 20; x += 19)
{
for (int zz = 0; zz < 20; zz += 19)
{
for (int xx = 0; xx < 20; xx += 19)
{
for (int y = 0; y < 20; y++)
{
if (Points[xx, y, zz].wPosition == nChunk.Points[x, y, z].wPosition)
nChunk.Points[x, y, z].density = Points[xx, y, zz].density;
}
}
}
}
}
}`
I want to check the edges and not all Points to save performance. the y coords the height.
Here is how i call this:
//mouse click
if (Input.GetMouseButtonDown(0))
{
Ray raym = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hitm;
if (Physics.Raycast(raym, out hitm))
{
if (hitm.transform.GetComponent<Chunk>())
{
Chunk cScript = hitm.transform.GetComponent<Chunk>();
for (int z = 0; z < 20; z++)
{
for (int y = 1; y < 19; y++)
{
for (int x = 0; x < 20; x++)
{
if (PointInsideSphere(cScript.Points[x, y, z].wPosition, hitm.point, 1f))
cScript.Points[x, y, z].density -= 0.1f;
}
}
}
cScript.SyncNeighbours();
}
}
}
In this way its working good but i want to check only the edges.
for (int i = 0; i < 9; i++)
{
Chunk nChunk = neighbours[i].GetComponent<Chunk>();
if (i == 4) continue; //skip self
for (int z = 0; z < 20; z ++)
{
for (int x = 0; x < 20; x ++)
{
for (int zz = 0; zz < 20; zz ++)
{
for (int xx = 0; xx < 20; xx ++)
{
for (int y = 0; y < 20; y++)
{
if (Points[xx, y, zz].wPosition.x == nChunk.Points[x, y, z].wPosition.x && Points[xx, y, zz].wPosition.y == nChunk.Points[x, y, z].wPosition.y && Points[xx, y, zz].wPosition.z == nChunk.Points[x, y, z].wPosition.z)
nChunk.Points[x, y, z].density = Points[xx, y, zz].density;
}
}
}
}
}
}
for (int j = 0; j < 9; j++)
{
neighbours[j].GetComponent<Chunk>().UpdateChunk();
}
It's solved in different way: When the game creates a chunk it checks points with 0 and 19 positions in 3d array then add them to a list. from that list i can check the points on neighbouring faces and make the densitys equal.

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;

Retinex algorithm implementation

I need to implement Single Scale retinex and multiscale retinex algorithm in C#,
I searched a bit but couldn't find any useful practice projects and artilces with code
As I understood correctly I should:
Convert RGB to YUV
Blur the image using Gaussian blur filter
Use I'(x, y) = 255*log10( I(x, y)/G(x, y) ) + 127.5
I - is illumination, G - Gaussian kernel, I' - the result image
Сonvert back YUV to RGB
This code is not working correctly
public static Image<Bgr, byte> SingleScaleRetinex(this Image<Bgr, byte> img, int gaussianKernelSize, double sigma)
{
var radius = gaussianKernelSize / 2;
var kernelSize = 2 * radius + 1;
var ycc = img.Convert<Ycc, byte>();
var sum = 0f;
var gaussKernel = new float[kernelSize * kernelSize];
for (int i = -radius, k = 0; i <= radius; i++, k++)
{
for (int j = -radius; j <= radius; j++)
{
var val = (float)Math.Exp(-(i * i + j * j) / (sigma * sigma));
gaussKernel[k] = val;
sum += val;
}
}
for (int i = 0; i < gaussKernel.Length; i++)
gaussKernel[i] /= sum;
var gray = new Image<Gray, byte>(ycc.Size);
CvInvoke.cvSetImageCOI(ycc, 1);
CvInvoke.cvCopy(ycc, gray, IntPtr.Zero);
// Размеры изображения
var width = img.Width;
var height = img.Height;
var bmp = gray.Bitmap;
var bitmapData = bmp.LockBits(new Rectangle(Point.Empty, gray.Size), ImageLockMode.ReadWrite, PixelFormat.Format8bppIndexed);
unsafe
{
for (var y = 0; y < height; y++)
{
var row = (byte*)bitmapData.Scan0 + y * bitmapData.Stride;
for (var x = 0; x < width; x++)
{
var color = row + x;
float val = 0;
for (int i = -radius, k = 0; i <= radius; i++, k++)
{
var ii = y + i;
if (ii < 0) ii = 0; if (ii >= height) ii = height - 1;
var row2 = (byte*)bitmapData.Scan0 + ii * bitmapData.Stride;
for (int j = -radius; j <= radius; j++)
{
var jj = x + j;
if (jj < 0) jj = 0; if (jj >= width) jj = width - 1;
val += *(row2 + jj) * gaussKernel[k];
}
}
var newColor = 127.5 + 255 * Math.Log(*color / val);
if (newColor > 255)
newColor = 255;
else if (newColor < 0)
newColor = 0;
*color = (byte)newColor;
}
}
}
bmp.UnlockBits(bitmapData);
CvInvoke.cvCopy(gray, ycc, IntPtr.Zero);
CvInvoke.cvSetImageCOI(ycc, 0);
return ycc.Convert<Bgr, byte>();
}
Look at:
http://www.fer.unizg.hr/ipg/resources/color_constancy
These algorithms are modifications of the Retinex algorithm (with speed improvement) although the author gave them funny names :)
There is a full source code (C++, but it is written very nicely).
Sorry for necro-posting, but it seems that there's a mistake in step 3 of your procedure that can mislead someone passing by.
In order to apply the correction, you want to divide source image by Gauss-filtered copy of it, not the Gaussian kernel itself. Approximately, in pseudo-code:
I_filtered(x,y) = G(x,y) * I(x,y)
I'(x,y) = log(I(x,y) / I_filtered(x,y))
And then apply casting of I'(x,y) to required numeric type (uint8, as I can refer from original post).
More on that topic can be found in this paper:
Ri(x, y) = log(Ii(x, y)) − log(Ii(x, y) ∗ F(x, y))
where Ii
is the input image on the i-th color channel, Ri
is the retinex output image on the i-th
channel and F is the normalized surround function.
.

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