4 Star 27 Fork 10

Yonghe/GeoFlying

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
MatrixFuncs.cs 35.58 KB
一键复制 编辑 原始数据 按行查看 历史
Yonghe 提交于 2020-10-02 09:33 . first
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Drawing;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using MapProj;
namespace GeoFly
{
/// <summary>
/// 主要用于放置一些公用函数
/// </summary>
public static class MatrixFuncs
{
public static double[,] Float2Double(float[,] data)
{
int rows=data.GetLength(0);
int cols=data.GetLength(1);
double[,] result=new double[rows,cols];
for(int row=0;row<rows;row++)
{
for(int col=0;col<cols;col++)
{
result[row,col]=(double)data[row,col];
}
}
return result;
}
public static double[,] Int2Double(int[,] data)
{
int rows=data.GetLength(0);
int cols=data.GetLength(1);
double[,] result=new double[rows,cols];
for(int row=0;row<rows;row++)
{
for(int col=0;col<cols;col++)
{
result[row,col]=(double)data[row,col];
}
}
return result;
}
public static double[,] ReGridBilinear(double[,] data, double[] latsSource, double[] lonsSource, double[] newlats, double[] newlons)
{
int nlat = newlats.Length;
int nlon = newlons.Length;
double[,] result = new double[nlat, nlon];
int[] lonIndice = new int[nlon];
double dlat = latsSource[1] - latsSource[0];
double dlon = lonsSource[1] - lonsSource[0];
for (int i = 0; i < nlon; i++) {
double lon = newlons[i];
lonIndice[i] = (int)((lon - lonsSource[0]) / dlon);
}
for (int ilat = 0; ilat < nlat; ilat++) {
double lat = newlats[ilat];
int latIndex = (int)((lat - latsSource[0]) / dlat);
for (int ilon = 0; ilon < nlon; ilon++) {
//Calculate bilinear interpolation using nearest four grid cells
double lon = newlons[ilon];
int lonIndex = lonIndice[ilon];
double A = data[latIndex, lonIndex];
double B = A;
if (lonIndex + 1 < lonsSource.Length)
B = data[latIndex, lonIndex + 1];
double C = A;
double D = A;
if (latIndex + 1 < latsSource.Length) {
C = data[latIndex + 1, lonIndex];
if (lonIndex + 1 < lonsSource.Length)
D = data[latIndex + 1, lonIndex + 1];
}
double[] abcd = new double[]{ A, B, C, D };
bool flag = false;
for (int ind = 0; ind < 4; ind++) {
if (abcd[ind] < -9990) {
result[ilat, ilon] = Math.Max(A, Math.Max(B, Math.Max(C, D)));
flag = true;
}
}
if (flag)
continue;
double lonA = lonsSource[lonIndex];
double lonB = lonA;
if (lonIndex + 1 < lonsSource.Length)
lonB = lonsSource[lonIndex + 1];
double E = LinearInter(A, B, lonA, lonB, lon);
double F = LinearInter(C, D, lonA, lonB, lon);
double latA = latsSource[latIndex];
double latB = latA;
if (latIndex + 1 < latsSource.Length)
latB = latsSource[latIndex + 1];
double vvv = LinearInter(E, F, latA, latB, lat);
result[ilat, ilon] = vvv;
}
}
return result;
}
public static float[,] ReGridBilinear(double[,] data, double[] latsSource, double[] lonsSource, float[] newlats, float[] newlons)
{
int nlat = newlats.Length;
int nlon = newlons.Length;
float[,] result = new float[nlat, nlon];
int[] lonIndice = new int[nlon];
double dlat = latsSource[1] - latsSource[0];
double dlon = lonsSource[1] - lonsSource[0];
for (int i = 0; i < nlon; i++) {
double lon = newlons[i];
lonIndice[i] = (int)((lon - lonsSource[0]) / dlon);
}
for (int ilat = 0; ilat < nlat; ilat++) {
double lat = newlats[ilat];
int latIndex = (int)((lat - latsSource[0]) / dlat);
for (int ilon = 0; ilon < nlon; ilon++) {
//Calculate bilinear interpolation using nearest four grid cells
double lon = newlons[ilon];
int lonIndex = lonIndice[ilon];
double A = data[latIndex, lonIndex];
double B = A;
if (lonIndex + 1 < lonsSource.Length)
B = data[latIndex, lonIndex + 1];
double C = A;
double D = A;
if (latIndex + 1 < latsSource.Length) {
C = data[latIndex + 1, lonIndex];
if (lonIndex + 1 < lonsSource.Length)
D = data[latIndex + 1, lonIndex + 1];
}
double[] abcd = new double[]{ A, B, C, D };
bool flag = false;
for (int ind = 0; ind < 4; ind++) {
if (abcd[ind] < -9990) {
result[ilat, ilon] = (float)Math.Max(A, Math.Max(B, Math.Max(C, D)));
flag = true;
}
}
if (flag)
continue;
double lonA = lonsSource[lonIndex];
double lonB = lonA;
if (lonIndex + 1 < lonsSource.Length)
lonB = lonsSource[lonIndex + 1];
double E = LinearInter(A, B, lonA, lonB, lon);
double F = LinearInter(C, D, lonA, lonB, lon);
double latA = latsSource[latIndex];
double latB = latA;
if (latIndex + 1 < latsSource.Length)
latB = latsSource[latIndex + 1];
double vvv = LinearInter(E, F, latA, latB, lat);
result[ilat, ilon] = (float)vvv;
}
}
return result;
}
private static float AbsDist(float x0, float y0, float x1, float y1)
{
return Math.Abs(x0 - x1) + Math.Abs(y0 - y1);
}
public static float[,] ReGridNearest(double[,] data, double[] latsSource, double[] lonsSource, float[] newlats, float[] newlons)
{
int nlat = newlats.Length;
int nlon = newlons.Length;
float[,] result = new float[nlat, nlon];
int[] lonIndice = new int[nlon];
float dlat = (float)(latsSource[1] - latsSource[0]);
float dlon = (float)(lonsSource[1] - lonsSource[0]);
for (int i = 0; i < nlon; i++) {
float lon = newlons[i];
lonIndice[i] = (int)((lon - lonsSource[0]) / dlon);// Search(lon, lonsSource);
}
for (int ilat = 0; ilat < nlat; ilat++) {
float lat = newlats[ilat];
int latIndex = -(int)((lat - latsSource[0]) / dlat);// Search(lat, latsSource);
//latIndex=latsSource.Length-1-latIndex;
for (int ilon = 0; ilon < nlon; ilon++) {
//Calculate bilinear interpolation using nearest four grid cells
float lon = newlons[ilon];
//lonIndex=Search(lon,lons1);
int lonIndex = lonIndice[ilon];
float A = (float)data[latIndex, lonIndex];
float ADist = AbsDist(lat, lon, (float)latsSource[latIndex], (float)lonsSource[lonIndex]);
float B = A;
float BDist = ADist;
if (lonIndex + 1 < lonsSource.Length) {
B = (float)data[latIndex, lonIndex + 1];
BDist = AbsDist(lat, lon, (float)latsSource[latIndex], (float)lonsSource[lonIndex + 1]);
}
float C = A;
float CDist = ADist;
float D = A;
float DDist = ADist;
if (latIndex + 1 < latsSource.Length) {
C = (float)data[latIndex + 1, lonIndex];
CDist = AbsDist(lat, lon, (float)latsSource[latIndex + 1], (float)lonsSource[lonIndex]);
if (lonIndex + 1 < lonsSource.Length) {
D = (float)data[latIndex + 1, lonIndex + 1];
DDist = AbsDist(lat, lon, (float)latsSource[latIndex + 1], (float)lonsSource[lonIndex + 1]);
}
}
float[] aaa = new float[]{ A, B, C, D };
float[] bbb = new float[]{ ADist, BDist, CDist, DDist };
float min = 1e20f;
float minValue = 0;
for (int i = 0; i < 4; i++) {
if (min > bbb[i]) {
min = bbb[i];
minValue = aaa[i];
}
}
result[nlat - 1 - ilat, ilon] = minValue;
}
}
return result;
}
public static float[,] ReGridLambert(double[,] data, double[] latsSource, double[] lonsSource, float startLat, float startLon, float cellSize, int rowCount, int colCount
, float refLat, float refLon, float stdLat1, float stdLat2)
{
Array.Reverse(latsSource);
float[,] result = new float[rowCount, colCount];
MapProj.GaussKruger proj = new GaussKruger();
double[] values0 = proj.ToXY_Lambert(startLon, startLat, refLon, refLat, stdLat1, stdLat2);
float[] values=Array.ConvertAll<double,float>(values0,d=>(float)d);
float lux = values[0];
float luy = values[1];
float X0 = 0;
float Y0 = 0;
for (int row = 0; row < rowCount; row++) {
for (int col = 0; col < colCount; col++) {
X0 = lux + cellSize * col;
Y0 = luy - cellSize * row;
values0= proj.ToLonLa_Lambert(X0, Y0, refLon, refLat, stdLat1, stdLat2);
float[] vv=Array.ConvertAll<double,float>(values0,d=>(float)d);
float lat = vv[1];
float lon = vv[0];
result[row, col] = (float)Interp(lat, lon, data, latsSource, lonsSource);
}
}
return result;
}
private static float Interp(float curLat, float curLon, double[,] data, double[] latsSource, double[] lonsSource)
{
float dlat = (float)(latsSource[1] - latsSource[0]);
float dlon = (float)(lonsSource[1] - lonsSource[0]);
int latIndex = -(int)((curLat - latsSource[0]) / dlat);
int lonIndex = (int)((curLon - lonsSource[0]) / dlon);
if (dlat < 0) {
dlat *= -1;
}
if (lonIndex + 1 >= lonsSource.Length) {
return -9999;
}
float A = (float)data[latIndex, lonIndex];
float B = (float)data[latIndex, lonIndex + 1];
float C = (float)data[latIndex + 1, lonIndex];
float D = (float)data[latIndex + 1, lonIndex + 1];
float lonA = (float)lonsSource[lonIndex];
float lonB = (float)lonsSource[lonIndex + 1];
float E = (float)LinearInter(A, B, lonA, lonB, curLon);
float F = (float)LinearInter(C, D, lonA, lonB, curLon);
float latA = (float)latsSource[latIndex];
float latB = (float)latsSource[latIndex + 1];
if (A < -9990 || B < -9990 || C < -9990 || D < -9990)
return Math.Max(A, Math.Max(B, Math.Max(C, D)));
return (float)LinearInter(E, F, latA, latB, curLat);
}
private static double LinearInter(double AValue, double BValue, double ACoor, double BCoor, double ECoor)
{
if (BCoor - ACoor == 0)
return (AValue + BValue) / 2;
return (AValue * (BCoor - ECoor) + BValue * (ECoor - ACoor)) / (BCoor - ACoor);
}
public static double[,] Smooth3x3(double[,] matrix)
{
int rows = matrix.GetLength(0);
int cols = matrix.GetLength(1);
double[,] m = new double[rows, cols];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (i == 0 || j == 0 || i == rows - 1 || j == cols - 1)
m[i, j] = matrix[i, j];
else {
m[i, j] = matrix[i, j] + matrix[i, j - 1] + matrix[i, j + 1] +
matrix[i + 1, j] + matrix[i + 1, j - 1] + matrix[i + 1, j + 1] +
matrix[i - 1, j] + matrix[i - 1, j - 1] + matrix[i - 1, j + 1];
m[i, j] /= 9;
}
}
}
return m;
}
/// <summary>
/// 从文本文件中读入所有数据行,数据行需要进一步分解为可用的数据
/// </summary>
/// <param name="FileName">文本文件名</param>
/// <returns>数据行列表</returns>
public static List<string> FileRead(string FileName)
{
//检查文件
if (!File.Exists(FileName)) {
throw new Exception("文件" + FileName + "不存在!");
}
ProgressBar bar = new ProgressBar();
bar.Show();
bar.Text = "正在读取数据";
List<string> slist = new List<string>();
StreamReader sr = new StreamReader(FileName);
while (!sr.EndOfStream) {
string s = sr.ReadLine();
if (s != "")
slist.Add(s);
}
sr.Close();
bar.Close();
return slist;
}
/// <summary>
/// 将矩阵转化为黑白灰度位图
/// </summary>
/// <param name="matrix">要转化的矩阵</param>
/// <param name="rows">总行数</param>
/// <param name="cols">总列数</param>
/// <returns></returns>
public static Bitmap BitmapFromMatrix(double[,] matrix)
{
//找出最大值和最小值
double max = -99999999;
double min = 99999999;
int rows = matrix.GetLength(0);
int cols = matrix.GetLength(1);
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (matrix[i, j] < -9990)
continue;
if (matrix[i, j] > max)
max = matrix[i, j];
if (matrix[i, j] < min)
min = matrix[i, j];
}
}
double scope = max - min;
Bitmap bitmap = new Bitmap(cols, rows);
BitmapData bmData = bitmap.LockBits(new Rectangle(0, 0, cols, rows), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
IntPtr ptr = bmData.Scan0;
int n = bmData.Stride * bitmap.Height;
byte[] rgb = new byte[n];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (matrix[i, j] < -9990) {
rgb[(i * cols + j) * 4 + 3] = 0;
} else {
byte value = (byte)((matrix[i, j] - min) * 255.0 / scope);
rgb[(i * cols + j) * 4] = value;
rgb[(i * cols + j) * 4 + 1] = value;
rgb[(i * cols + j) * 4 + 2] = value;
rgb[(i * cols + j) * 4 + 3] = 255;
}
}
}
System.Runtime.InteropServices.Marshal.Copy(rgb, 0, ptr, n);
bitmap.UnlockBits(bmData);
return bitmap;
}
public static Bitmap BitmapFromMatrix(int[,] matrix)
{
//找出最大值和最小值
double max = -99999999;
double min = 99999999;
int rows = matrix.GetLength(0);
int cols = matrix.GetLength(1);
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (matrix[i, j] < -9990)
continue;
if (matrix[i, j] > max)
max = (int)matrix[i, j];
if (matrix[i, j] < min)
min = (int)matrix[i, j];
}
}
double scope = max - min;
Bitmap bitmap = new Bitmap(cols, rows);
BitmapData bmData = bitmap.LockBits(new Rectangle(0, 0, cols, rows), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
IntPtr ptr = bmData.Scan0;
int n = bmData.Stride * bitmap.Height;
byte[] rgb = new byte[n];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (matrix[i, j] < -9990) {
rgb[(i * cols + j) * 4 + 3] = 0;
} else {
byte value = (byte)((matrix[i, j] - min) * 255.0 / scope);
rgb[(i * cols + j) * 4] = value;
rgb[(i * cols + j) * 4 + 1] = value;
rgb[(i * cols + j) * 4 + 2] = value;
rgb[(i * cols + j) * 4 + 3] = 255;
}
}
}
System.Runtime.InteropServices.Marshal.Copy(rgb, 0, ptr, n);
bitmap.UnlockBits(bmData);
return bitmap;
}
/// <summary>
/// 将矩阵转化为黑-蓝-绿-红-白渐变的彩色图像
/// </summary>
/// <param name="matrix"></param>
/// <param name="rows"></param>
/// <param name="cols"></param>
/// <returns></returns>
public static Bitmap BitmapFromMatrix5(double[,] matrix)
{
//找出最大值和最小值
double max = -99999999;
double min = 99999999;
int rows = matrix.GetLength(0);
int cols = matrix.GetLength(1);
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (matrix[i, j] < -9990)
continue;
if (matrix[i, j] > max)
max = matrix[i, j];
if (matrix[i, j] < min)
min = matrix[i, j];
}
}
double scope = max - min;
Bitmap bitmap = new Bitmap(cols, rows);
BitmapData bmData = bitmap.LockBits(new Rectangle(0, 0, cols, rows), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
IntPtr ptr = bmData.Scan0;
int n = bmData.Stride * bitmap.Height;
byte[] rgb = new byte[n];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (matrix[i, j] < -9990) {
rgb[(i * cols + j) * 4 + 3] = 0;
continue;
}
double gray = (matrix[i, j] - min) / scope * 4;
byte r, g, b;
if (gray < 1) {
r = 0;
g = 0;
b = (byte)(gray * 255);
} else if (gray < 2) {
gray -= 1.0;
r = 0;
g = (byte)(gray * 255);
b = (byte)((1 - gray) * 255);
} else if (gray < 3) {
gray -= 2.0;
r = (byte)(gray * 255);
g = (byte)((1 - gray) * 255);
b = 0;
} else {
gray -= 3.0;
r = 255;
g = (byte)(gray * 255);
b = (byte)(gray * 255);
}
rgb[(i * cols + j) * 4] = b;
rgb[(i * cols + j) * 4 + 1] = g;
rgb[(i * cols + j) * 4 + 2] = r;
rgb[(i * cols + j) * 4 + 3] = 255;
}
}
System.Runtime.InteropServices.Marshal.Copy(rgb, 0, ptr, n);
bitmap.UnlockBits(bmData);
return bitmap;
}
/// <summary>
/// 将矩阵转化为黑-蓝-绿-红-白渐变的彩色图像
/// </summary>
/// <param name="matrix"></param>
/// <param name="rows"></param>
/// <param name="cols"></param>
/// <returns></returns>
public static Bitmap BitmapFromMatrix5(int[,] matrix)
{
//找出最大值和最小值
double max = -99999999;
double min = 99999999;
int rows = matrix.GetLength(0);
int cols = matrix.GetLength(1);
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (matrix[i, j] < -9990)
continue;
if (matrix[i, j] > max)
max = matrix[i, j];
if (matrix[i, j] < min)
min = matrix[i, j];
}
}
double scope = max - min;
Bitmap bitmap = new Bitmap(cols, rows);
BitmapData bmData = bitmap.LockBits(new Rectangle(0, 0, cols, rows), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
IntPtr ptr = bmData.Scan0;
int n = bmData.Stride * bitmap.Height;
byte[] rgb = new byte[n];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (matrix[i, j] < -9990) {
rgb[(i * cols + j) * 4 + 3] = 0;
continue;
}
double gray = (matrix[i, j] - min) / scope * 4;
byte r, g, b;
if (gray < 1) {
r = 0;
g = 0;
b = (byte)(gray * 255);
} else if (gray < 2) {
gray -= 1.0;
r = 0;
g = (byte)(gray * 255);
b = (byte)((1 - gray) * 255);
} else if (gray < 3) {
gray -= 2.0;
r = (byte)(gray * 255);
g = (byte)((1 - gray) * 255);
b = 0;
} else {
gray -= 3.0;
r = 255;
g = (byte)(gray * 255);
b = (byte)(gray * 255);
}
rgb[(i * cols + j) * 4] = b;
rgb[(i * cols + j) * 4 + 1] = g;
rgb[(i * cols + j) * 4 + 2] = r;
rgb[(i * cols + j) * 4 + 3] = 255;
}
}
System.Runtime.InteropServices.Marshal.Copy(rgb, 0, ptr, n);
bitmap.UnlockBits(bmData);
return bitmap;
}
/// <summary>
/// 生成黑-蓝-绿-黄-红-白渐变的彩色
/// </summary>
/// <param name="min"></param>
/// <param name="max"></param>
/// <param name="value"></param>
/// <returns></returns>
public static Color ToColor6(double min, double max, double value)
{
if (max <= min)
return Color.Black;
if (max < min)
throw new Exception("Max value must larger than min value:min=" + min + " max=" + max);
value = (value > max) ? max : value;
value = (value < min) ? min : value;
double scope = max - min;
double gray = (value - min) / scope * 5;
byte r, g, b;
if (gray < 1) { //从黑至蓝
r = 0;
g = 0;
b = (byte)(gray * 255);
} else if (gray < 2) { //从蓝到绿
gray -= 1.0;
r = 0;
g = (byte)(gray * 255);
b = (byte)((1 - gray) * 255);
} else if (gray < 3) { //从绿至黄
gray -= 2.0;
r = (byte)(gray * 255);
g = 255;
b = 0;
} else if (gray < 4) { //从黄至红
gray -= 3.0;
r = 255;
g = (byte)((1 - gray) * 255);
b = 0;
} else {
gray -= 4.0;
r = 255;
g = (byte)(gray * 255);
b = (byte)(gray * 255);
}
return Color.FromArgb(r, g, b);
}
/// <summary>
/// 将矩阵转化为黑-蓝-绿-黄-红-白渐变的彩色图像
/// </summary>
/// <param name="matrix"></param>
/// <param name="rows"></param>
/// <param name="cols"></param>
/// <returns></returns>
public static Bitmap BitmapFromMatrix6(double[,] matrix)
{
//找出最大值和最小值
double max = -99999999;
double min = 99999999;
int rows = matrix.GetLength(0);
int cols = matrix.GetLength(1);
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (matrix[i, j] < -9990)
continue;
if (matrix[i, j] > max)
max = matrix[i, j];
if (matrix[i, j] < min)
min = matrix[i, j];
}
}
return BitmapFromMatrix6(matrix, max, min);
}
public static Bitmap BitmapFromMatrix_Pallete(double[,] matrix, double max, double min,int count)
{
int rows = matrix.GetLength(0);
int cols = matrix.GetLength(1);
double scope = max - min;
double step=scope/count;
Color[] colors=new Color[count];
for(int i=0;i<count;i++)
{
colors[i]= ToColor6(min,max,step*i+min);
}
Bitmap bitmap = new Bitmap(cols, rows);
BitmapData bmData = bitmap.LockBits(new Rectangle(0, 0, cols, rows), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
IntPtr ptr = bmData.Scan0;
int n = bmData.Stride * bitmap.Height;
byte[] rgb = new byte[n];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (matrix[i, j] < -9998) {
rgb[(i * cols + j) * 4 + 3] = 0;
continue;
}
int index=(int)((matrix[i,j]-min)/step);
if(index<0)
{
rgb[(i * cols + j) * 4 + 3] = 0;
continue;
}
if(index>=count)
index=count-1;
Color color =colors[index];
rgb[(i * cols + j) * 4] =color.B;
rgb[(i * cols + j) * 4 + 1] = color.G;
rgb[(i * cols + j) * 4 + 2] = color.R;
rgb[(i * cols + j) * 4 + 3] = 255;
}
}
System.Runtime.InteropServices.Marshal.Copy(rgb, 0, ptr, n);
bitmap.UnlockBits(bmData);
return bitmap;
}
public static Bitmap BitmapFromMatrix6(double[,] matrix, double max, double min)
{
//找出最大值和最小值
int rows = matrix.GetLength(0);
int cols = matrix.GetLength(1);
double scope = max - min;
Bitmap bitmap = new Bitmap(cols, rows);
BitmapData bmData = bitmap.LockBits(new Rectangle(0, 0, cols, rows), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
IntPtr ptr = bmData.Scan0;
int n = bmData.Stride * bitmap.Height;
byte[] rgb = new byte[n];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (matrix[i, j] < -9998) {
rgb[(i * cols + j) * 4 + 3] = 0;
continue;
}
double gray = (matrix[i, j] - min) / scope * 5;
byte r, g, b;
if (scope == 0) {
r = 0;
g = 0;
b = 0;
} else {
if (gray < 1) { //从黑至蓝
r = 0;
g = 0;
b = (byte)(gray * 255);
} else if (gray < 2) { //从蓝到绿
gray -= 1.0;
r = 0;
g = (byte)(gray * 255);
b = (byte)((1 - gray) * 255);
} else if (gray < 3) { //从绿至黄
gray -= 2.0;
r = (byte)(gray * 255);
g = 255;
b = 0;
} else if (gray < 4) { //从黄至红
gray -= 3.0;
r = 255;
g = (byte)((1 - gray) * 255);
b = 0;
} else {
gray -= 4.0;
r = 255;
g = (byte)(gray * 255);
b = (byte)(gray * 255);
}
}
rgb[(i * cols + j) * 4] = b;
rgb[(i * cols + j) * 4 + 1] = g;
rgb[(i * cols + j) * 4 + 2] = r;
rgb[(i * cols + j) * 4 + 3] = 255;
}
}
System.Runtime.InteropServices.Marshal.Copy(rgb, 0, ptr, n);
bitmap.UnlockBits(bmData);
return bitmap;
}
/// <summary>
/// 将矩阵转化为黑-蓝-绿-黄-红-白渐变的彩色图像
/// </summary>
/// <param name="matrix"></param>
/// <param name="rows"></param>
/// <param name="cols"></param>
/// <returns></returns>
public static Bitmap BitmapFromMatrix6(int[,] matrix)
{
//找出最大值和最小值
double max = -99999999;
double min = 99999999;
int rows = matrix.GetLength(0);
int cols = matrix.GetLength(1);
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (matrix[i, j] < -9990)
continue;
if (matrix[i, j] > max)
max = matrix[i, j];
if (matrix[i, j] < min)
min = matrix[i, j];
}
}
double scope = max - min;
Bitmap bitmap = new Bitmap(cols, rows);
BitmapData bmData = bitmap.LockBits(new Rectangle(0, 0, cols, rows), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
IntPtr ptr = bmData.Scan0;
int n = bmData.Stride * bitmap.Height;
byte[] rgb = new byte[n];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (matrix[i, j] < -9990) {
rgb[(i * cols + j) * 4 + 3] = 0;
continue;
}
double gray = (matrix[i, j] - min) / scope * 5;
byte r, g, b;
if (gray < 1) { //从黑至蓝
r = 0;
g = 0;
b = (byte)(gray * 255);
} else if (gray < 2) { //从蓝到绿
gray -= 1.0;
r = 0;
g = (byte)(gray * 255);
b = (byte)((1 - gray) * 255);
} else if (gray < 3) { //从绿至黄
gray -= 2.0;
r = (byte)(gray * 255);
g = 255;
b = 0;
} else if (gray < 4) { //从黄至红
gray -= 3.0;
r = 255;
g = (byte)((1 - gray) * 255);
b = 0;
} else {
gray -= 4.0;
r = 255;
g = (byte)(gray * 255);
b = (byte)(gray * 255);
}
rgb[(i * cols + j) * 4] = b;
rgb[(i * cols + j) * 4 + 1] = g;
rgb[(i * cols + j) * 4 + 2] = r;
rgb[(i * cols + j) * 4 + 3] = 255;
}
}
System.Runtime.InteropServices.Marshal.Copy(rgb, 0, ptr, n);
bitmap.UnlockBits(bmData);
return bitmap;
}
/// <summary>
/// 将矩阵转变为三色混合图像
/// </summary>
/// <param name="matrix"></param>
/// <param name="color1"></param>
/// <param name="color2"></param>
/// <param name="color3"></param>
/// <returns></returns>
public static Bitmap BitmapFromMatrix3(double[,] matrix, Color color1, Color color2, Color color3)
{
//找出最大值和最小值
double max = -99999999;
double min = 99999999;
int rows = matrix.GetLength(0);
int cols = matrix.GetLength(1);
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (matrix[i, j] < -9990)
continue;
if (matrix[i, j] > max)
max = (int)matrix[i, j];
if (matrix[i, j] < min)
min = (int)matrix[i, j];
}
}
byte r1 = color1.R;
byte g1 = color1.G;
byte b1 = color1.B;
byte r2 = color2.R;
byte g2 = color2.G;
byte b2 = color2.B;
byte r3 = color3.R;
byte g3 = color3.G;
byte b3 = color3.B;
double scope = max - min;
Bitmap bitmap = new Bitmap(cols, rows);
BitmapData bmData = bitmap.LockBits(new Rectangle(0, 0, cols, rows), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
IntPtr ptr = bmData.Scan0;
int n = bmData.Stride * bitmap.Height;
byte[] rgb = new byte[n];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (matrix[i, j] < -9990) {
rgb[(i * cols + j) * 4 + 3] = 0;
continue;
}
byte r, g, b;
double gray = (matrix[i, j] - min) / scope * 2;
if (gray < 1) {
r = (byte)(gray * (r2 - r1) + r1);
g = (byte)(gray * (g2 - g1) + g1);
b = (byte)(gray * (b2 - b1) + b1);
} else {
gray -= 1;
r = (byte)(gray * (r3 - r2) + r2);
g = (byte)(gray * (g3 - g2) + g2);
b = (byte)(gray * (b3 - b2) + b2);
}
rgb[(i * cols + j) * 4] = b;
rgb[(i * cols + j) * 4 + 1] = g;
rgb[(i * cols + j) * 4 + 2] = r;
rgb[(i * cols + j) * 4 + 3] = 255;
}
}
System.Runtime.InteropServices.Marshal.Copy(rgb, 0, ptr, n);
bitmap.UnlockBits(bmData);
return bitmap;
}
public static Bitmap BitmapFromMatrix3(int[,] matrix, Color color1, Color color2, Color color3)
{
//找出最大值和最小值
double max = -99999999;
double min = 99999999;
int rows = matrix.GetLength(0);
int cols = matrix.GetLength(1);
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (matrix[i, j] < -9990)
continue;
if (matrix[i, j] > max)
max = (int)matrix[i, j];
if (matrix[i, j] < min)
min = (int)matrix[i, j];
}
}
byte r1 = color1.R;
byte g1 = color1.G;
byte b1 = color1.B;
byte r2 = color2.R;
byte g2 = color2.G;
byte b2 = color2.B;
byte r3 = color3.R;
byte g3 = color3.G;
byte b3 = color3.B;
double scope = max - min;
Bitmap bitmap = new Bitmap(cols, rows);
BitmapData bmData = bitmap.LockBits(new Rectangle(0, 0, cols, rows), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
IntPtr ptr = bmData.Scan0;
int n = bmData.Stride * bitmap.Height;
byte[] rgb = new byte[n];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (matrix[i, j] < -9990) {
rgb[(i * cols + j) * 4 + 3] = 0;
continue;
}
byte r, g, b;
double gray = (matrix[i, j] - min) / scope * 2;
if (gray < 1) {
r = (byte)(gray * 2 * (r2 - r1) + r1);
g = (byte)(gray * 2 * (g2 - g1) + g1);
b = (byte)(gray * 2 * (b2 - b1) + b1);
} else {
gray -= 1;
r = (byte)(gray * 2 * (r3 - r2) + r2);
g = (byte)(gray * 2 * (g3 - g2) + g2);
b = (byte)(gray * 2 * (b3 - b2) + b2);
}
rgb[(i * cols + j) * 4] = b;
rgb[(i * cols + j) * 4 + 1] = g;
rgb[(i * cols + j) * 4 + 2] = r;
rgb[(i * cols + j) * 4 + 3] = 255;
}
}
System.Runtime.InteropServices.Marshal.Copy(rgb, 0, ptr, n);
bitmap.UnlockBits(bmData);
return bitmap;
}
/// <summary>
/// 将矩阵转变为二色混合图像
/// </summary>
/// <param name="matrix"></param>
/// <param name="color1"></param>
/// <param name="color2"></param>
/// <returns></returns>
public static Bitmap BitmapFromMatrix2(double[,] matrix, Color color1, Color color2)
{
//找出最大值和最小值
double max = -99999999;
double min = 99999999;
int rows = matrix.GetLength(0);
int cols = matrix.GetLength(1);
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (matrix[i, j] < -9990)
continue;
if (matrix[i, j] > max)
max = (int)matrix[i, j];
if (matrix[i, j] < min)
min = (int)matrix[i, j];
}
}
byte r1 = color1.R;
byte g1 = color1.G;
byte b1 = color1.B;
byte r2 = color2.R;
byte g2 = color2.G;
byte b2 = color2.B;
double scope = max - min;
Bitmap bitmap = new Bitmap(cols, rows);
BitmapData bmData = bitmap.LockBits(new Rectangle(0, 0, cols, rows), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
IntPtr ptr = bmData.Scan0;
int n = bmData.Stride * bitmap.Height;
byte[] rgb = new byte[n];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (matrix[i, j] < -9990) {
rgb[(i * cols + j) * 4 + 3] = 0;
continue;
}
byte r, g, b;
double gray = (matrix[i, j] - min) / scope;
r = (byte)(gray * (r2 - r1) + r1);
g = (byte)(gray * (g2 - g1) + g1);
b = (byte)(gray * (b2 - b1) + b1);
rgb[(i * cols + j) * 4] = b;
rgb[(i * cols + j) * 4 + 1] = g;
rgb[(i * cols + j) * 4 + 2] = r;
rgb[(i * cols + j) * 4 + 3] = 255;
}
}
System.Runtime.InteropServices.Marshal.Copy(rgb, 0, ptr, n);
bitmap.UnlockBits(bmData);
return bitmap;
}
public static Bitmap BitmapFromMatrix2(int[,] matrix, Color color1, Color color2)
{
//找出最大值和最小值
double max = -99999999;
double min = 99999999;
int rows = matrix.GetLength(0);
int cols = matrix.GetLength(1);
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (matrix[i, j] < -9990)
continue;
if (matrix[i, j] > max)
max = (int)matrix[i, j];
if (matrix[i, j] < min)
min = (int)matrix[i, j];
}
}
byte r1 = color1.R;
byte g1 = color1.G;
byte b1 = color1.B;
byte r2 = color2.R;
byte g2 = color2.G;
byte b2 = color2.B;
double scope = max - min;
Bitmap bitmap = new Bitmap(cols, rows);
BitmapData bmData = bitmap.LockBits(new Rectangle(0, 0, cols, rows), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
IntPtr ptr = bmData.Scan0;
int n = bmData.Stride * bitmap.Height;
byte[] rgb = new byte[n];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (matrix[i, j] < -9990) {
rgb[(i * cols + j) * 4 + 3] = 0;
continue;
}
byte r, g, b;
double gray = (matrix[i, j] - min) / scope;
r = (byte)(gray * (r2 - r1) + r1);
g = (byte)(gray * (g2 - g1) + g1);
b = (byte)(gray * (b2 - b1) + b1);
rgb[(i * cols + j) * 4] = b;
rgb[(i * cols + j) * 4 + 1] = g;
rgb[(i * cols + j) * 4 + 2] = r;
rgb[(i * cols + j) * 4 + 3] = 255;
}
}
System.Runtime.InteropServices.Marshal.Copy(rgb, 0, ptr, n);
bitmap.UnlockBits(bmData);
return bitmap;
}
//深度复制一个矩阵
public static double[,] CopyMatrix(double[,] matrix)
{
int rows = matrix.GetLength(0);
int cols = matrix.GetLength(1);
double[,] m = new double[rows, cols];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
m[i, j] = matrix[i, j];
}
}
return m;
}
//深度复制一个矩阵
public static int[,] CopyMatrix(int[,] matrix)
{
int rows = matrix.GetLength(0);
int cols = matrix.GetLength(1);
int[,] m = new int[rows, cols];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
m[i, j] = matrix[i, j];
}
}
return m;
}
public static void SaveMatrix(int[,] matrix, string FileName)
{
StreamWriter sw = new StreamWriter(FileName);
int rows = matrix.GetLength(0);
int cols = matrix.GetLength(1);
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
sw.Write(matrix[i, j].ToString("0.00") + "\t");
}
sw.WriteLine();
}
sw.Close();
}
public static void SaveMatrix(double[,] matrix, string FileName)
{
StreamWriter sw = new StreamWriter(FileName);
int rows = matrix.GetLength(0);
int cols = matrix.GetLength(1);
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
sw.Write(matrix[i, j].ToString("0.00") + "\t");
}
sw.WriteLine();
}
sw.Close();
}
public static double[,] LogTransform(double[,] matrix)
{
int rows = matrix.GetLength(0);
int cols = matrix.GetLength(1);
double[,] ma = new double[rows, cols];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (matrix[i, j] <= 0)
ma[i, j] = -9999;
else
ma[i, j] = Math.Log(matrix[i, j]);
}
}
return ma;
}
public static double[,] LogTransform(int[,] matrix)
{
int rows = matrix.GetLength(0);
int cols = matrix.GetLength(1);
double[,] ma = new double[rows, cols];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (matrix[i, j] <= 0)
ma[i, j] = -9999;
else
ma[i, j] = Math.Log(matrix[i, j]);
}
}
return ma;
}
public static double[,] A = new double[3, 3] {
{ 0.567, 0.086, -0.002 }, {
0.253,
0.504,
-0.050
}, {
-0.006,
-0.039,
0.244
}
};
public static double[,] B = new double[3, 3] {
{ 0.781, 0, 0 }, {
0.328,
0.637,
0
}, {
0.238,
-0.341,
0.873
}
};
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C#
1
https://gitee.com/sucksis/geo-flying.git
[email protected]:sucksis/geo-flying.git
sucksis
geo-flying
GeoFlying
master

搜索帮助