EDN Admin
Well-known member
In Form1 I have this code:
lightningsRegions = new List<string>();
LR = new List<Lightnings_Extractor.Lightnings_Region>();
int fp = 0;
List<int> rise, fall;
rise = new List<int>();
fall = new List<int>();
for (int i = 35; i < averagesOriginal.Count - 35; i++)
{
if (averagesOriginal <= resultings[i - 35])
{
if (averagesOriginal[i + 1] > resultings[i - 35 + 1])
{ if (fp == 0)
{
fp = i;
}
rise.Add(i-padBeforeAndAfter);
}
}
}
for (int i = fp + 1; i < averagesOriginal.Count - 35; i++) {
if ((i - 35) < 0)
{
}
else
{
if (averagesOriginal > resultings[i - 35])
{
if (averagesOriginal[i + 1] <= resultings[i - 35 + 1])
{
fall.Add(i+padBeforeAndAfter);
}
}
}
}
if ((rise.Count - fall.Count) == 1)
{
rise.RemoveAt(rise.Count - 1);
}
if (fall.Count != rise.Count)
{
throw new Exception("uneven falls and rise....we should deal with it..but not for now");
}
for (int i = 0; i < fall.Count; i++)
{
Lightnings_Extractor.Lightnings_Region LReg = new Lightnings_Extractor.Lightnings_Region();
LReg.start = rise;
LReg.end = fall;
LR.Add(LReg); }
for (int i = 0; i < LR.Count; i++)
{
int len = LR.end - LR.start;
lightningsRegions.Add("Lightning " + i.ToString() +
" Length " + len.ToString() + " [" + LR.start.ToString() + " - "
+ LR.end.ToString() + "]");
}
In this code im creating a List of int wich are going to be the frames/images i will save later to the hard disk and also creating the a List of strings wich will be the directories on my hard disk and then i will save the correct images/frames to each directory.
This is the code where im creating the directories and the List of int :for (int i = 0; i < lightningsRegions.Count; i++)
{
Directory.CreateDirectory(_outputDir + "\" + automaticModeDirectory + "\" + lightningsRegions);
}
_fts = new List<int>();
for (int i = 0; i < LR.Count; i++)
{
for (int j = LR.start; j <= LR.end; j++)
{
_fts.Add(j);
}
}
f.FramesToSave = _fts;
Now the results on the hard disk are for example the first two directories are like this:
This is a directory name on the hard disk:Lightning 0 Length 8 [88 - 96]
Inside i see the correct images/frames that have been saved:
000088.bmp 000089.bmp 000090.bmp 000091.bmp 000092.bmp 000093.bmp 000094.bmp 000095.bmp 000096.bmp
Then I have the next directory wich is:Lightning 1 Length 7 [93 - 100]
But inside this directory i have the images/frames:
000097.bmp 000098.bmp 000099.bmp 000100.bmp
Now when im looking on the images and on the graph of them I see that this two lightnings are one near the other there is no any frames space between them.
So what it should be on my hard disk is instead two directories only one:Lightning 0 Length 8 [88 - 100]
And inside all the images/frames 000088.bmp untill 000100.bmp
The last directory for example is:Lightning 14 Length 9 [1004 - 1013]
And inside i see the images 1004 to 1013 and since the directory before should contain images that are not starting as 1003 so this directory should stay alone should not to be merge.
The directory before this one is [852 - 864] so thats why the last directory in this case should not be merge.
Thats the general idea to detect wich files in the Lists rise and fall or in the List LR wich contain all the fall and rise wich are should be merged as one and wich should not.
And then to create the correct string :
lightningsRegions.Add("Lightning " + i.ToString() + " Length " + len.
ToString() + " [" + LR.start.ToString() + " - " + LR.end.ToStr
ing() + "]");
Now rise and fall are int Lists and LR is a List of a class type wich contain the end and start for example in LR in index 0 i will see:
end 96
start 88
Then im finding the numbers between and then in the other class im extracting/saving this frames.
This is the code in the other class where im saving the frames/images:int ISampleGrabberCB.BufferCB(double sampleTime, IntPtr pBuffer, int bufferLen)
{
if (Form1.ExtractAutomatic == true)
{
if (this.Secondpass)
{
//extract ranges
LightningsDirectories = Form1.lightningsRegions;
parseLightningsDirectories = new List<DirectoryStruct>();
string pattern = @"[(d+)D+(d+)";
Regex expression = new Regex(pattern);
foreach (string Directory in LightningsDirectories)
{
Match match = expression.Match(Directory);
DirectoryStruct newrow = new DirectoryStruct();
newrow.name = Directory;
newrow.lowrange = int.Parse(match.Groups[1].Value);
newrow.highrange = int.Parse(match.Groups[2].Value);
parseLightningsDirectories.Add(newrow);
}
}
using (var bitmap = new Bitmap(_width, _height, _width * 3, PixelFormat.Format24bppRgb, pBuffer))
{
if (this.Secondpass)
{
if (FramesToSave != null && FramesToSave.Contains(_frameId)) {
DirectoryStruct[] myrow = parseLightningsDirectories.Where(row =>
(row.lowrange <= _frameId) && (row.highrange >= _frameId)).Select(row => row).ToArray();
bitmap.RotateFlip(RotateFlipType.Rotate180FlipX);
var myrows = parseLightningsDirectories.Where(row =>
(row.lowrange <= _frameId) && (row.highrange >= _frameId)).Select(row => row);
bitmap.Save(Path.Combine(_outFolder + "\" + myrow[0].name, _frameId.ToString("D6") + ".bmp"));
Since in this class the variable frameId wich is int is moving forward all the time according to the frames so as it now ni the first directory on the hard disk i see [88 - 96] in the next directory i see [93 -100] but its not logic since file 93 94 95 and 96 already extracted in the first directory and frameId already is after this frames they cant be extracted to the hard disk again.
Note: that I added an int variable called padBeforeAndAfter wich is set to 3.
Since I wanted to extract from each lightning the 3 frames before the rise and 3 frames after the fall !
Thats why the directories looks like this and I think thats why the files the images are not in the correct directory since frameId cant extract the same images.
So I want to merge the same near by files/images/frames to one directory each time.
And this is where im stuck I got no clue how to do it.
danieli
View the full article
lightningsRegions = new List<string>();
LR = new List<Lightnings_Extractor.Lightnings_Region>();
int fp = 0;
List<int> rise, fall;
rise = new List<int>();
fall = new List<int>();
for (int i = 35; i < averagesOriginal.Count - 35; i++)
{
if (averagesOriginal <= resultings[i - 35])
{
if (averagesOriginal[i + 1] > resultings[i - 35 + 1])
{ if (fp == 0)
{
fp = i;
}
rise.Add(i-padBeforeAndAfter);
}
}
}
for (int i = fp + 1; i < averagesOriginal.Count - 35; i++) {
if ((i - 35) < 0)
{
}
else
{
if (averagesOriginal > resultings[i - 35])
{
if (averagesOriginal[i + 1] <= resultings[i - 35 + 1])
{
fall.Add(i+padBeforeAndAfter);
}
}
}
}
if ((rise.Count - fall.Count) == 1)
{
rise.RemoveAt(rise.Count - 1);
}
if (fall.Count != rise.Count)
{
throw new Exception("uneven falls and rise....we should deal with it..but not for now");
}
for (int i = 0; i < fall.Count; i++)
{
Lightnings_Extractor.Lightnings_Region LReg = new Lightnings_Extractor.Lightnings_Region();
LReg.start = rise;
LReg.end = fall;
LR.Add(LReg); }
for (int i = 0; i < LR.Count; i++)
{
int len = LR.end - LR.start;
lightningsRegions.Add("Lightning " + i.ToString() +
" Length " + len.ToString() + " [" + LR.start.ToString() + " - "
+ LR.end.ToString() + "]");
}
In this code im creating a List of int wich are going to be the frames/images i will save later to the hard disk and also creating the a List of strings wich will be the directories on my hard disk and then i will save the correct images/frames to each directory.
This is the code where im creating the directories and the List of int :for (int i = 0; i < lightningsRegions.Count; i++)
{
Directory.CreateDirectory(_outputDir + "\" + automaticModeDirectory + "\" + lightningsRegions);
}
_fts = new List<int>();
for (int i = 0; i < LR.Count; i++)
{
for (int j = LR.start; j <= LR.end; j++)
{
_fts.Add(j);
}
}
f.FramesToSave = _fts;
Now the results on the hard disk are for example the first two directories are like this:
This is a directory name on the hard disk:Lightning 0 Length 8 [88 - 96]
Inside i see the correct images/frames that have been saved:
000088.bmp 000089.bmp 000090.bmp 000091.bmp 000092.bmp 000093.bmp 000094.bmp 000095.bmp 000096.bmp
Then I have the next directory wich is:Lightning 1 Length 7 [93 - 100]
But inside this directory i have the images/frames:
000097.bmp 000098.bmp 000099.bmp 000100.bmp
Now when im looking on the images and on the graph of them I see that this two lightnings are one near the other there is no any frames space between them.
So what it should be on my hard disk is instead two directories only one:Lightning 0 Length 8 [88 - 100]
And inside all the images/frames 000088.bmp untill 000100.bmp
The last directory for example is:Lightning 14 Length 9 [1004 - 1013]
And inside i see the images 1004 to 1013 and since the directory before should contain images that are not starting as 1003 so this directory should stay alone should not to be merge.
The directory before this one is [852 - 864] so thats why the last directory in this case should not be merge.
Thats the general idea to detect wich files in the Lists rise and fall or in the List LR wich contain all the fall and rise wich are should be merged as one and wich should not.
And then to create the correct string :
lightningsRegions.Add("Lightning " + i.ToString() + " Length " + len.
ToString() + " [" + LR.start.ToString() + " - " + LR.end.ToStr
ing() + "]");
Now rise and fall are int Lists and LR is a List of a class type wich contain the end and start for example in LR in index 0 i will see:
end 96
start 88
Then im finding the numbers between and then in the other class im extracting/saving this frames.
This is the code in the other class where im saving the frames/images:int ISampleGrabberCB.BufferCB(double sampleTime, IntPtr pBuffer, int bufferLen)
{
if (Form1.ExtractAutomatic == true)
{
if (this.Secondpass)
{
//extract ranges
LightningsDirectories = Form1.lightningsRegions;
parseLightningsDirectories = new List<DirectoryStruct>();
string pattern = @"[(d+)D+(d+)";
Regex expression = new Regex(pattern);
foreach (string Directory in LightningsDirectories)
{
Match match = expression.Match(Directory);
DirectoryStruct newrow = new DirectoryStruct();
newrow.name = Directory;
newrow.lowrange = int.Parse(match.Groups[1].Value);
newrow.highrange = int.Parse(match.Groups[2].Value);
parseLightningsDirectories.Add(newrow);
}
}
using (var bitmap = new Bitmap(_width, _height, _width * 3, PixelFormat.Format24bppRgb, pBuffer))
{
if (this.Secondpass)
{
if (FramesToSave != null && FramesToSave.Contains(_frameId)) {
DirectoryStruct[] myrow = parseLightningsDirectories.Where(row =>
(row.lowrange <= _frameId) && (row.highrange >= _frameId)).Select(row => row).ToArray();
bitmap.RotateFlip(RotateFlipType.Rotate180FlipX);
var myrows = parseLightningsDirectories.Where(row =>
(row.lowrange <= _frameId) && (row.highrange >= _frameId)).Select(row => row);
bitmap.Save(Path.Combine(_outFolder + "\" + myrow[0].name, _frameId.ToString("D6") + ".bmp"));
Since in this class the variable frameId wich is int is moving forward all the time according to the frames so as it now ni the first directory on the hard disk i see [88 - 96] in the next directory i see [93 -100] but its not logic since file 93 94 95 and 96 already extracted in the first directory and frameId already is after this frames they cant be extracted to the hard disk again.
Note: that I added an int variable called padBeforeAndAfter wich is set to 3.
Since I wanted to extract from each lightning the 3 frames before the rise and 3 frames after the fall !
Thats why the directories looks like this and I think thats why the files the images are not in the correct directory since frameId cant extract the same images.
So I want to merge the same near by files/images/frames to one directory each time.
And this is where im stuck I got no clue how to do it.
danieli
View the full article