Fix processing byt video in parallelo (mashalled)

This commit is contained in:
Samuele E. Locatelli
2021-02-09 19:58:01 +01:00
parent f1b3a90c2e
commit 7501ee38ec
3 changed files with 254 additions and 28 deletions
+40 -7
View File
@@ -61,6 +61,9 @@
this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.label4 = new System.Windows.Forms.Label();
this.progBar = new System.Windows.Forms.ToolStripProgressBar();
this.lblStats = new System.Windows.Forms.ToolStripStatusLabel();
this.chkParallel = new System.Windows.Forms.CheckBox();
this.menuStrip1.SuspendLayout();
this.statusStrip1.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.pBoxOrig)).BeginInit();
@@ -132,8 +135,10 @@
// statusStrip1
//
this.statusStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.toolStripConnectionStatus});
this.statusStrip1.Location = new System.Drawing.Point(0, 793);
this.toolStripConnectionStatus,
this.progBar,
this.lblStats});
this.statusStrip1.Location = new System.Drawing.Point(0, 673);
this.statusStrip1.Name = "statusStrip1";
this.statusStrip1.Size = new System.Drawing.Size(1290, 22);
this.statusStrip1.TabIndex = 2;
@@ -160,6 +165,7 @@
this.panelCmd.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)));
this.panelCmd.BackColor = System.Drawing.SystemColors.ControlDark;
this.panelCmd.Controls.Add(this.chkParallel);
this.panelCmd.Controls.Add(this.lblPoint);
this.panelCmd.Controls.Add(this.lblReadTemp);
this.panelCmd.Controls.Add(this.chkReadTemp);
@@ -171,7 +177,7 @@
this.panelCmd.Controls.Add(this.labelTitle);
this.panelCmd.Location = new System.Drawing.Point(0, 23);
this.panelCmd.Name = "panelCmd";
this.panelCmd.Size = new System.Drawing.Size(192, 767);
this.panelCmd.Size = new System.Drawing.Size(192, 647);
this.panelCmd.TabIndex = 6;
//
// chkPointSetup
@@ -331,9 +337,10 @@
// lblReadTemp
//
this.lblReadTemp.AutoSize = true;
this.lblReadTemp.Location = new System.Drawing.Point(156, 107);
this.lblReadTemp.Location = new System.Drawing.Point(103, 106);
this.lblReadTemp.MinimumSize = new System.Drawing.Size(80, 0);
this.lblReadTemp.Name = "lblReadTemp";
this.lblReadTemp.Size = new System.Drawing.Size(27, 13);
this.lblReadTemp.Size = new System.Drawing.Size(80, 13);
this.lblReadTemp.TabIndex = 8;
this.lblReadTemp.Text = "? °C";
this.lblReadTemp.TextAlign = System.Drawing.ContentAlignment.TopRight;
@@ -341,9 +348,11 @@
// lblPoint
//
this.lblPoint.AutoSize = true;
this.lblPoint.Font = new System.Drawing.Font("Microsoft Sans Serif", 7F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.lblPoint.Location = new System.Drawing.Point(8, 107);
this.lblPoint.MinimumSize = new System.Drawing.Size(80, 0);
this.lblPoint.Name = "lblPoint";
this.lblPoint.Size = new System.Drawing.Size(28, 13);
this.lblPoint.Size = new System.Drawing.Size(80, 13);
this.lblPoint.TabIndex = 9;
this.lblPoint.Text = "(0,0)";
//
@@ -395,11 +404,32 @@
this.label4.TabIndex = 11;
this.label4.Text = "RECOLORED image";
//
// progBar
//
this.progBar.Name = "progBar";
this.progBar.Size = new System.Drawing.Size(100, 16);
//
// lblStats
//
this.lblStats.Name = "lblStats";
this.lblStats.Size = new System.Drawing.Size(12, 17);
this.lblStats.Text = "-";
//
// chkParallel
//
this.chkParallel.AutoSize = true;
this.chkParallel.Location = new System.Drawing.Point(7, 301);
this.chkParallel.Name = "chkParallel";
this.chkParallel.Size = new System.Drawing.Size(60, 17);
this.chkParallel.TabIndex = 10;
this.chkParallel.Text = "Parallel";
this.chkParallel.UseVisualStyleBackColor = true;
//
// MainForm
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(1290, 815);
this.ClientSize = new System.Drawing.Size(1290, 695);
this.Controls.Add(this.label4);
this.Controls.Add(this.label3);
this.Controls.Add(this.label2);
@@ -467,6 +497,9 @@
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.ToolStripProgressBar progBar;
private System.Windows.Forms.ToolStripStatusLabel lblStats;
private System.Windows.Forms.CheckBox chkParallel;
}
}
+74 -15
View File
@@ -15,7 +15,17 @@ namespace ThermalImageStreamerDemo
/// <summary>
/// Ultima immagine recuperata
/// </summary>
protected ThermalImage lastImage { get; set; }
protected ThermalImage lastImageOrig { get; set; }
/// <summary>
/// Ultima immagine post trasformazione
/// </summary>
protected Bitmap lastTransfBn { get; set; }
/// <summary>
/// Ultima immagine ricolorata
/// </summary>
protected Bitmap lastTransfCol { get; set; }
private bool liveView
{
get
@@ -56,6 +66,12 @@ namespace ThermalImageStreamerDemo
protected SetPoints origPoints = new SetPoints();
protected SetPoints destPoints = new SetPoints();
protected double msLastCam = 0;
protected double msLastTra = 0;
protected double msLastCol = 0;
protected Stopwatch sw = new Stopwatch();
private Camera _camera;
readonly Timer _timerRefreshUi = new Timer();
@@ -77,12 +93,37 @@ namespace ThermalImageStreamerDemo
{
takePicture();
calculateTarget();
displayImages();
updateStatDisplay();
updateTempDisplay();
}
// avanzo prog bar
progBar.Increment(4);
if (progBar.Value >= progBar.Maximum)
{
progBar.Value = 0;
}
}
private void updateStatDisplay()
{
lblStats.Text = $"Camera: {msLastCam}ms | Persp {msLastTra}ms | Color {msLastCol}ms";
}
private void displayImages()
{
if (lastImageOrig != null)
{
pBoxOrig.Image = lastImageOrig.Image;
pBoxTraBn.Image = lastTransfBn;
pBoxTraCol.Image = lastTransfCol;
}
}
private void takePicture()
{
sw.Reset();
sw.Start();
if (!IsDirty) return;
IsDirty = false;
if (_camera == null) return;
@@ -90,8 +131,7 @@ namespace ThermalImageStreamerDemo
_camera.GetImage().EnterLock();
try
{
lastImage = (ThermalImage)_camera.GetImage();
pBoxOrig.Image = lastImage.Image;
lastImageOrig = (ThermalImage)_camera.GetImage();
}
catch (Exception exception)
@@ -103,6 +143,9 @@ namespace ThermalImageStreamerDemo
_camera.GetImage().ExitLock();
}
sw.Stop();
msLastCam = sw.ElapsedMilliseconds;
#if false
ThermalImage myImg = (ThermalImage)_stream.GetImage();
myImg.TemperatureUnit = TemperatureUnit.Celsius;
@@ -259,9 +302,9 @@ namespace ThermalImageStreamerDemo
double ratio = 1;
try
{
if (lastImage != null && lastImage.Width > 0)
if (lastImageOrig != null && lastImageOrig.Width > 0)
{
ratio = (double)pBoxOrig.Width / lastImage.Width;
ratio = (double)pBoxOrig.Width / lastImageOrig.Width;
}
}
catch
@@ -295,12 +338,12 @@ namespace ThermalImageStreamerDemo
private void updateTempDisplay()
{
if (lastImage != null)
if (lastImageOrig != null)
{
double pointTemperature = 0;
if (readTemp)
{
pointTemperature = lastImage.GetValueAt(lastPoint).Value;
pointTemperature = lastImageOrig.GetValueAt(lastPoint).Value;
lblReadTemp.Text = $"{pointTemperature:N2} °C";
}
else
@@ -369,16 +412,32 @@ namespace ThermalImageStreamerDemo
/// </summary>
private void calculateTarget()
{
try
if (lastImageOrig != null)
{
var pTransf = new PerspectiveTransform(origPoints.Coords, destPoints.Coords);
var transfImg = pTransf.convertImage((Bitmap)pBoxOrig.Image, dimX, dimY);
pBoxTraBn.Image = transfImg;
}
catch
{ }
}
try
{
sw.Reset();
sw.Start();
var pTransf = new PerspectiveTransform(origPoints.Coords, destPoints.Coords);
var rTrasf = new ReColorize(lastImageOrig.MinSignalValue, lastImageOrig.MaxSignalValue);
var transfImg = pTransf.convertImage((Bitmap)lastImageOrig.Image, dimX, dimY);
lastTransfBn = transfImg;
sw.Stop();
msLastTra= sw.ElapsedMilliseconds;
sw.Reset();
sw.Start();
// ora calcolo ricolorazione
lastTransfCol = rTrasf.process(lastTransfBn, chkParallel.Checked);
sw.Stop();
msLastCol = sw.ElapsedMilliseconds;
}
catch(Exception exc)
{
}
}
}
private void txtAB_TextChanged(object sender, EventArgs e)
{
// ricalcolo in proporzione altro valore...
+140 -6
View File
@@ -4,12 +4,13 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
namespace ThermoCamUtils
{
public class ReColorize
public class ReColorize
{
protected int minVal = 0;
protected int maxVal = 500;
public ReColorize(int minValue, int maxValue)
@@ -18,14 +19,147 @@ namespace ThermoCamUtils
maxVal = maxValue;
}
public Bitmap process(Bitmap original)
public Bitmap process(Bitmap original, bool doParallel)
{
Bitmap answ = original;
double currTemp = 0;
//Bitmap imgColor = new Bitmap(original.Width, original.Height);
Bitmap imgColor = original.Clone(new Rectangle(0, 0, original.Width, original.Height), original.PixelFormat);
int currVal = 0;
if (doParallel)
{
#if false
// recupero matrice di punti con valore RGB
Parallel.For(0, original.Width - 1, x =>
{
Parallel.For(0, original.Height - 1, y =>
{
// esecuo ricalcolo
currVal = original.GetPixel(x, y).R;
imgColor.SetPixel(x, y, Rescale(ref currVal));
});
});
#endif
BitmapData bitmapData = imgColor.LockBits(new Rectangle(0, 0, imgColor.Width, imgColor.Height), ImageLockMode.ReadWrite, imgColor.PixelFormat);
return answ;
int bytesPerPixel = Bitmap.GetPixelFormatSize(imgColor.PixelFormat) / 8;
int byteCount = bitmapData.Stride * imgColor.Height;
byte[] pixels = new byte[byteCount];
IntPtr ptrFirstPixel = bitmapData.Scan0;
Marshal.Copy(ptrFirstPixel, pixels, 0, pixels.Length);
int heightInPixels = bitmapData.Height;
int widthInBytes = bitmapData.Width * bytesPerPixel;
for (int y = 0; y < heightInPixels; y++)
{
int currentLine = y * bitmapData.Stride;
for (int x = 0; x < widthInBytes; x = x + bytesPerPixel)
{
//int oldBlue = pixels[currentLine + x];
//int oldGreen = pixels[currentLine + x + 1];
//int oldRed = pixels[currentLine + x + 2];
//// calculate new pixel value
//pixels[currentLine + x] = (byte)oldBlue;
//pixels[currentLine + x + 1] = (byte)oldGreen;
//pixels[currentLine + x + 2] = (byte)oldRed;
// prendo il rosso
int oldRed = pixels[currentLine + x + 0];
currVal = pixels[currentLine + x + 2];
var newCol = Rescale(ref currVal);
pixels[currentLine + x] = (byte)newCol.B;
pixels[currentLine + x + 1] = (byte)newCol.G;
pixels[currentLine + x + 2] = (byte)newCol.R;
//currVal = original.GetPixel(x, y).R;
//imgColor.SetPixel(x, y, Rescale(ref currVal));
}
}
// copy modified bytes back
Marshal.Copy(pixels, 0, ptrFirstPixel, pixels.Length);
imgColor.UnlockBits(bitmapData);
}
else
{
// recupero matrice di punti con valore RGB
for (int x = 0; x < original.Width; x++)
{
for (int y = 0; y < original.Height; y++)
{
// esecuo ricalcolo
currVal = original.GetPixel(x, y).R;
imgColor.SetPixel(x, y, Rescale(ref currVal));
}
}
}
return imgColor;
}
/// <summary>
/// Calcola colore su scala dato rapporto valore B/N su min/max
/// </summary>
/// <param name="ValOrig"></param>
/// <returns></returns>
protected Color Rescale(ref int ValOrig)
{
int R = 0;
int G = 0;
int B = 0;
#if false
// calcolo retta interpolazione Y = A * X + B
double M = 255 / (maxVal - minVal);
double Q = 255 - M * maxVal;
double Value = M * currTemp + Q;
#endif
if (ValOrig < 0)
{
R = 0;
G = 0;
B = 0;
}
else if (ValOrig <= 51)
{
R = 0;
G = 0;
B = (int)(ValOrig * 5);
}
else if (ValOrig <= 102)
{
R = (int)((ValOrig - 51) * 5);
G = 0;
B = 255;
}
else if (ValOrig <= 153)
{
R = 255;
G = 0;
B = (int)(255 - ((ValOrig - 102) * 5));
}
else if (ValOrig <= 204)
{
R = 255;
G = (int)((ValOrig - 153) * 5);
B = 0;
}
else if (ValOrig <= 255)
{
R = 255;
G = 255;
B = (int)((ValOrig - 204) * 5);
}
else
{
R = 255;
G = 255;
B = 255;
}
return Color.FromArgb(R, G, B);
}
}
}